|
|
|
|
@ -2,6 +2,7 @@ import NewNotesButton from '@/components/NewNotesButton'
@@ -2,6 +2,7 @@ import NewNotesButton from '@/components/NewNotesButton'
|
|
|
|
|
import { Button } from '@/components/ui/button' |
|
|
|
|
import { ExtendedKind } from '@/constants' |
|
|
|
|
import { |
|
|
|
|
getEmbeddedNoteBech32Ids, |
|
|
|
|
getReplaceableCoordinateFromEvent, |
|
|
|
|
isMentioningMutedUsers, |
|
|
|
|
isReplaceableEvent, |
|
|
|
|
@ -17,6 +18,7 @@ import { useNostr } from '@/providers/NostrProvider'
@@ -17,6 +18,7 @@ import { useNostr } from '@/providers/NostrProvider'
|
|
|
|
|
import { useUserTrust } from '@/providers/UserTrustProvider' |
|
|
|
|
import { useZap } from '@/providers/ZapProvider' |
|
|
|
|
import client from '@/services/client.service' |
|
|
|
|
import logger from '@/lib/logger' |
|
|
|
|
import { TFeedSubRequest } from '@/types' |
|
|
|
|
import dayjs from 'dayjs' |
|
|
|
|
import { Event, kinds } from 'nostr-tools' |
|
|
|
|
@ -35,9 +37,9 @@ import PullToRefresh from 'react-simple-pull-to-refresh'
@@ -35,9 +37,9 @@ import PullToRefresh from 'react-simple-pull-to-refresh'
|
|
|
|
|
import { toast } from 'sonner' |
|
|
|
|
import NoteCard, { NoteCardLoadingSkeleton } from '../NoteCard' |
|
|
|
|
|
|
|
|
|
const LIMIT = 200 |
|
|
|
|
const ALGO_LIMIT = 500 |
|
|
|
|
const SHOW_COUNT = 10 |
|
|
|
|
const LIMIT = 500 // Increased from 200 to load more events per request
|
|
|
|
|
const ALGO_LIMIT = 1000 // Increased from 500 for algorithm feeds
|
|
|
|
|
const SHOW_COUNT = 50 // Increased from 10 to show more events at once, reducing scroll load frequency
|
|
|
|
|
|
|
|
|
|
const NoteList = forwardRef( |
|
|
|
|
( |
|
|
|
|
@ -88,6 +90,8 @@ const NoteList = forwardRef(
@@ -88,6 +90,8 @@ const NoteList = forwardRef(
|
|
|
|
|
const supportTouch = useMemo(() => isTouchDevice(), []) |
|
|
|
|
const bottomRef = useRef<HTMLDivElement | null>(null) |
|
|
|
|
const topRef = useRef<HTMLDivElement | null>(null) |
|
|
|
|
const consecutiveEmptyRef = useRef(0) // Track consecutive empty results to prevent infinite retries
|
|
|
|
|
const loadMoreTimeoutRef = useRef<NodeJS.Timeout | null>(null) // Throttle loadMore calls to prevent stuttering
|
|
|
|
|
|
|
|
|
|
// Memoize subRequests serialization to avoid expensive JSON.stringify on every render
|
|
|
|
|
const subRequestsKey = useMemo(() => { |
|
|
|
|
@ -212,6 +216,7 @@ const NoteList = forwardRef(
@@ -212,6 +216,7 @@ const NoteList = forwardRef(
|
|
|
|
|
setEvents([]) |
|
|
|
|
setNewEvents([]) |
|
|
|
|
setHasMore(true) |
|
|
|
|
consecutiveEmptyRef.current = 0 // Reset counter on refresh
|
|
|
|
|
|
|
|
|
|
if (showKinds.length === 0) { |
|
|
|
|
setLoading(false) |
|
|
|
|
@ -234,13 +239,57 @@ const NoteList = forwardRef(
@@ -234,13 +239,57 @@ const NoteList = forwardRef(
|
|
|
|
|
onEvents: (events, eosed) => { |
|
|
|
|
if (events.length > 0) { |
|
|
|
|
setEvents(events) |
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch profiles for initial events (reduced batch size for faster initial load)
|
|
|
|
|
// This ensures profiles are ready before user starts scrolling
|
|
|
|
|
// Reduced from 300 to 150 to reduce initial load time
|
|
|
|
|
const initialPubkeys = Array.from( |
|
|
|
|
new Set(events.slice(0, 150).map((ev) => ev.pubkey).filter((p) => p?.length === 64)) |
|
|
|
|
) |
|
|
|
|
if (initialPubkeys.length > 0) { |
|
|
|
|
// Filter out already prefetched pubkeys
|
|
|
|
|
const pubkeysToFetch = initialPubkeys.filter((p) => !prefetchedPubkeysRef.current.has(p)) |
|
|
|
|
if (pubkeysToFetch.length > 0) { |
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
pubkeysToFetch.forEach((p) => prefetchedPubkeysRef.current.add(p)) |
|
|
|
|
// Batch fetch in background (non-blocking)
|
|
|
|
|
client.fetchProfilesForPubkeys(pubkeysToFetch).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
pubkeysToFetch.forEach((p) => prefetchedPubkeysRef.current.delete(p)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch embedded events for initial events
|
|
|
|
|
// Extract embedded event IDs from initial events
|
|
|
|
|
const initialEmbeddedEventIds = new Set<string>() |
|
|
|
|
events.slice(0, 150).forEach((ev) => { |
|
|
|
|
const embeddedIds = extractEmbeddedEventIds(ev) |
|
|
|
|
embeddedIds.forEach((id) => initialEmbeddedEventIds.add(id)) |
|
|
|
|
}) |
|
|
|
|
const eventIdsToFetch = Array.from(initialEmbeddedEventIds).filter( |
|
|
|
|
(id) => !prefetchedEventIdsRef.current.has(id) |
|
|
|
|
) |
|
|
|
|
if (eventIdsToFetch.length > 0) { |
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.add(id)) |
|
|
|
|
// Batch fetch embedded events in background (non-blocking)
|
|
|
|
|
Promise.all(eventIdsToFetch.map((id) => client.fetchEvent(id))).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.delete(id)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (areAlgoRelays) { |
|
|
|
|
setHasMore(false) |
|
|
|
|
} |
|
|
|
|
if (eosed) { |
|
|
|
|
setLoading(false) |
|
|
|
|
setHasMore(events.length > 0) |
|
|
|
|
// CRITICAL FIX: Always set hasMore to true on eosed, even if we have few events
|
|
|
|
|
// The initial load might only return a few events due to filtering or relay limits
|
|
|
|
|
// We should still try to load more on scroll - the loadMore logic will handle stopping
|
|
|
|
|
// Only set to false if we explicitly know there are no more events (handled in loadMore)
|
|
|
|
|
setHasMore(true) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
onNew: (event) => { |
|
|
|
|
@ -297,7 +346,8 @@ const NoteList = forwardRef(
@@ -297,7 +346,8 @@ const NoteList = forwardRef(
|
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
startLogin, |
|
|
|
|
needSort: !areAlgoRelays |
|
|
|
|
needSort: !areAlgoRelays, |
|
|
|
|
useCache: false // Main feeds should always fetch fresh from relays, not use cache
|
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
setTimelineKey(timelineKey) |
|
|
|
|
@ -310,39 +360,199 @@ const NoteList = forwardRef(
@@ -310,39 +360,199 @@ const NoteList = forwardRef(
|
|
|
|
|
} |
|
|
|
|
}, [subRequestsKey, refreshCount, showKinds, showKind1OPs, showKind1Replies, showKind1111, useFilterAsIs]) |
|
|
|
|
|
|
|
|
|
// Use refs to avoid dependency issues and ensure latest values in async callbacks
|
|
|
|
|
const eventsRef = useRef(events) |
|
|
|
|
const showCountRef = useRef(showCount) |
|
|
|
|
const loadingRef = useRef(loading) |
|
|
|
|
const hasMoreRef = useRef(hasMore) |
|
|
|
|
const timelineKeyRef = useRef(timelineKey) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
eventsRef.current = events |
|
|
|
|
}, [events]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
showCountRef.current = showCount |
|
|
|
|
}, [showCount]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
loadingRef.current = loading |
|
|
|
|
}, [loading]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
hasMoreRef.current = hasMore |
|
|
|
|
}, [hasMore]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
timelineKeyRef.current = timelineKey |
|
|
|
|
}, [timelineKey]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
const options = { |
|
|
|
|
const options: IntersectionObserverInit = { |
|
|
|
|
root: null, |
|
|
|
|
rootMargin: '10px', |
|
|
|
|
threshold: 0.1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const loadMore = async () => { |
|
|
|
|
if (showCount < events.length) { |
|
|
|
|
setShowCount((prev) => prev + SHOW_COUNT) |
|
|
|
|
// preload more
|
|
|
|
|
if (events.length - showCount > LIMIT / 2) { |
|
|
|
|
const loadMore = async (): Promise<void> => { |
|
|
|
|
const currentEvents = eventsRef.current |
|
|
|
|
const currentShowCount = showCountRef.current |
|
|
|
|
const currentLoading = loadingRef.current |
|
|
|
|
const currentHasMore = hasMoreRef.current |
|
|
|
|
const currentTimelineKey = timelineKeyRef.current |
|
|
|
|
|
|
|
|
|
// CRITICAL: Throttle loadMore calls to prevent stuttering during rapid scrolling
|
|
|
|
|
if (loadMoreTimeoutRef.current) { |
|
|
|
|
return // Already scheduled, skip
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Show more events immediately if we have them cached
|
|
|
|
|
if (currentShowCount < currentEvents.length) { |
|
|
|
|
// Show more aggressively: increase by SHOW_COUNT, but also check if we should show even more
|
|
|
|
|
const remaining = currentEvents.length - currentShowCount |
|
|
|
|
const increment = Math.min(SHOW_COUNT * 2, remaining) // Show up to 2x SHOW_COUNT if available
|
|
|
|
|
setShowCount((prev) => prev + increment) |
|
|
|
|
// Only preload more if we have plenty cached (more than 3/4 of LIMIT)
|
|
|
|
|
// BUT: Always try to load more if we have very few events (might be due to filtering)
|
|
|
|
|
if (currentEvents.length - currentShowCount > LIMIT * 0.75 && currentEvents.length >= 50) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
// If we have very few events, always try to load more (might be aggressive filtering)
|
|
|
|
|
if (currentEvents.length < 50) { |
|
|
|
|
// Continue to loadMore below even if we have cached events
|
|
|
|
|
// This ensures we keep loading when filtering is aggressive
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!timelineKey || loading || !hasMore) return |
|
|
|
|
setLoading(true) |
|
|
|
|
const newEvents = await client.loadMoreTimeline( |
|
|
|
|
timelineKey, |
|
|
|
|
events.length ? events[events.length - 1].created_at - 1 : dayjs().unix(), |
|
|
|
|
LIMIT |
|
|
|
|
) |
|
|
|
|
setLoading(false) |
|
|
|
|
if (newEvents.length === 0) { |
|
|
|
|
setHasMore(false) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
setEvents((oldEvents) => [...oldEvents, ...newEvents]) |
|
|
|
|
if (!currentTimelineKey || currentLoading || !currentHasMore) return |
|
|
|
|
|
|
|
|
|
// Schedule loadMore with a small delay to throttle rapid calls
|
|
|
|
|
loadMoreTimeoutRef.current = setTimeout(async () => { |
|
|
|
|
loadMoreTimeoutRef.current = null |
|
|
|
|
const latestEvents = eventsRef.current |
|
|
|
|
const latestTimelineKey = timelineKeyRef.current |
|
|
|
|
const latestLoading = loadingRef.current |
|
|
|
|
const latestHasMore = hasMoreRef.current |
|
|
|
|
|
|
|
|
|
if (!latestTimelineKey || latestLoading || !latestHasMore) return |
|
|
|
|
|
|
|
|
|
setLoading(true) |
|
|
|
|
let newEvents: Event[] = [] |
|
|
|
|
try { |
|
|
|
|
const until = latestEvents.length ? latestEvents[latestEvents.length - 1].created_at - 1 : dayjs().unix() |
|
|
|
|
newEvents = await client.loadMoreTimeline( |
|
|
|
|
latestTimelineKey, |
|
|
|
|
until, |
|
|
|
|
LIMIT |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// CRITICAL FIX: Be extremely conservative about stopping the feed
|
|
|
|
|
// Only stop if we're absolutely certain there are no more events
|
|
|
|
|
if (newEvents.length === 0) { |
|
|
|
|
// Check if timeline has more cached refs that we haven't loaded yet
|
|
|
|
|
const hasMoreCached = client.hasMoreTimelineEvents?.(latestTimelineKey, until) ?? false |
|
|
|
|
|
|
|
|
|
if (hasMoreCached) { |
|
|
|
|
// There are more cached events, keep hasMore true and try again
|
|
|
|
|
setLoading(false) |
|
|
|
|
// Retry after a short delay to allow IndexedDB to catch up
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
if (hasMoreRef.current && !loadingRef.current) { |
|
|
|
|
loadMore() |
|
|
|
|
} |
|
|
|
|
}, 300) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// No cached events and network returned empty
|
|
|
|
|
// Be VERY patient - don't stop too early, especially when we have few events
|
|
|
|
|
// This prevents stopping due to temporary relay issues or slow relays
|
|
|
|
|
consecutiveEmptyRef.current += 1 |
|
|
|
|
|
|
|
|
|
// CRITICAL FIX: Only stop if we have MANY consecutive empty results
|
|
|
|
|
// This ensures we don't stop prematurely when relays are slow or filtering is aggressive
|
|
|
|
|
// Even with few visible events, we might have many events that are filtered out
|
|
|
|
|
if (consecutiveEmptyRef.current >= 20) { |
|
|
|
|
// After 20 consecutive empty results, assume we've reached the end
|
|
|
|
|
// Increased from 10 to 20 to be even more patient with slow relays
|
|
|
|
|
setHasMore(false) |
|
|
|
|
} |
|
|
|
|
// Otherwise, keep hasMore true to allow retry on next scroll
|
|
|
|
|
// This ensures the feed continues trying even if relays are slow
|
|
|
|
|
setLoading(false) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Reset consecutive empty counter on success
|
|
|
|
|
consecutiveEmptyRef.current = 0 |
|
|
|
|
|
|
|
|
|
setEvents((oldEvents) => [...oldEvents, ...newEvents]) |
|
|
|
|
|
|
|
|
|
// NEVER automatically set hasMore to false based on result count
|
|
|
|
|
// Only stop when we get consecutive empty results
|
|
|
|
|
// This ensures the feed continues loading even with partial results
|
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch profiles for newly loaded events (throttled to reduce frequency)
|
|
|
|
|
// This ensures profiles are ready before user scrolls to them
|
|
|
|
|
if (newEvents.length > 0) { |
|
|
|
|
// Throttle profile prefetching for newly loaded events to reduce network load
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
const newPubkeys = Array.from( |
|
|
|
|
new Set(newEvents.map((ev) => ev.pubkey).filter((p) => p?.length === 64)) |
|
|
|
|
) |
|
|
|
|
if (newPubkeys.length > 0) { |
|
|
|
|
// Filter out already prefetched pubkeys
|
|
|
|
|
const pubkeysToFetch = newPubkeys.filter((p) => !prefetchedPubkeysRef.current.has(p)) |
|
|
|
|
if (pubkeysToFetch.length > 0) { |
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
pubkeysToFetch.forEach((p) => prefetchedPubkeysRef.current.add(p)) |
|
|
|
|
// Batch fetch in background (non-blocking)
|
|
|
|
|
client.fetchProfilesForPubkeys(pubkeysToFetch).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
pubkeysToFetch.forEach((p) => prefetchedPubkeysRef.current.delete(p)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch embedded events for newly loaded events
|
|
|
|
|
const newEmbeddedEventIds = new Set<string>() |
|
|
|
|
newEvents.forEach((ev) => { |
|
|
|
|
const embeddedIds = extractEmbeddedEventIds(ev) |
|
|
|
|
embeddedIds.forEach((id) => newEmbeddedEventIds.add(id)) |
|
|
|
|
}) |
|
|
|
|
const eventIdsToFetch = Array.from(newEmbeddedEventIds).filter( |
|
|
|
|
(id) => !prefetchedEventIdsRef.current.has(id) |
|
|
|
|
) |
|
|
|
|
if (eventIdsToFetch.length > 0) { |
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.add(id)) |
|
|
|
|
// Batch fetch embedded events in background (non-blocking)
|
|
|
|
|
Promise.all(eventIdsToFetch.map((id) => client.fetchEvent(id))).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.delete(id)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, 100) // Small delay to batch with other profile fetches
|
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
// On error, don't set hasMore to false - might be temporary network issue
|
|
|
|
|
logger.error('[NoteList] Error loading more events', { error }) |
|
|
|
|
consecutiveEmptyRef.current += 1 |
|
|
|
|
// Only stop after MANY consecutive errors - be very patient with network issues
|
|
|
|
|
// This prevents stopping when relays are temporarily down or slow
|
|
|
|
|
if (consecutiveEmptyRef.current >= 25) { |
|
|
|
|
// Increased from 15 to 25 to be even more patient with network issues
|
|
|
|
|
setHasMore(false) |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
setLoading(false) |
|
|
|
|
} |
|
|
|
|
}, 50) // Reduced delay from 100ms to 50ms for more responsive scrolling
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const observerInstance = new IntersectionObserver((entries) => { |
|
|
|
|
if (entries[0].isIntersecting && hasMore) { |
|
|
|
|
if (entries[0].isIntersecting && hasMoreRef.current && !loadingRef.current) { |
|
|
|
|
// Throttle: only trigger if not already loading and not already scheduled
|
|
|
|
|
loadMore() |
|
|
|
|
} |
|
|
|
|
}, options) |
|
|
|
|
@ -357,22 +567,241 @@ const NoteList = forwardRef(
@@ -357,22 +567,241 @@ const NoteList = forwardRef(
|
|
|
|
|
if (observerInstance && currentBottomRef) { |
|
|
|
|
observerInstance.unobserve(currentBottomRef) |
|
|
|
|
} |
|
|
|
|
// Clean up timeout on unmount
|
|
|
|
|
if (loadMoreTimeoutRef.current) { |
|
|
|
|
clearTimeout(loadMoreTimeoutRef.current) |
|
|
|
|
loadMoreTimeoutRef.current = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, [loading, hasMore, events, showCount, timelineKey]) |
|
|
|
|
// Dependencies are handled via refs to avoid stale closures in async callbacks
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
// Prefetch profiles for visible authors in one batched request (IndexedDB + one relay request)
|
|
|
|
|
// CRITICAL: Prefetch profiles for visible authors + upcoming events in one batched request
|
|
|
|
|
// This prevents browser crashes during rapid scrolling by pre-loading profiles before they're needed
|
|
|
|
|
const visiblePubkeysRef = useRef<Set<string>>(new Set()) |
|
|
|
|
const prefetchedPubkeysRef = useRef<Set<string>>(new Set()) |
|
|
|
|
const prefetchTimeoutRef = useRef<NodeJS.Timeout | null>(null) |
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch embedded events (referenced in e tags, a tags, and content)
|
|
|
|
|
// This ensures embedded events are ready before user scrolls to them
|
|
|
|
|
const prefetchedEventIdsRef = useRef<Set<string>>(new Set()) |
|
|
|
|
const prefetchEmbeddedEventsTimeoutRef = useRef<NodeJS.Timeout | null>(null) |
|
|
|
|
|
|
|
|
|
// Helper function to extract all embedded event IDs from an event
|
|
|
|
|
const extractEmbeddedEventIds = useCallback((evt: Event): string[] => { |
|
|
|
|
const eventIds: string[] = [] |
|
|
|
|
|
|
|
|
|
// 1. Extract from 'e' tags (event references)
|
|
|
|
|
evt.tags |
|
|
|
|
.filter((tag) => tag[0] === 'e' && tag[1] && tag[1].length === 64) |
|
|
|
|
.forEach((tag) => { |
|
|
|
|
const eventId = tag[1] |
|
|
|
|
if (eventId && /^[0-9a-f]{64}$/.test(eventId)) { |
|
|
|
|
eventIds.push(eventId) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 2. Extract from 'a' tags (addressable events) - get event ID if present
|
|
|
|
|
evt.tags |
|
|
|
|
.filter((tag) => tag[0] === 'a' && tag[3]) // tag[3] is the event ID for version tracking
|
|
|
|
|
.forEach((tag) => { |
|
|
|
|
const eventId = tag[3] |
|
|
|
|
if (eventId && /^[0-9a-f]{64}$/.test(eventId)) { |
|
|
|
|
eventIds.push(eventId) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 3. Extract from content (nostr: links)
|
|
|
|
|
// Note: getEmbeddedNoteBech32Ids returns hex IDs (despite the name)
|
|
|
|
|
const embeddedNoteIds = getEmbeddedNoteBech32Ids(evt) |
|
|
|
|
embeddedNoteIds.forEach((id) => { |
|
|
|
|
// The function already returns hex IDs, so use them directly
|
|
|
|
|
if (id && /^[0-9a-f]{64}$/.test(id)) { |
|
|
|
|
eventIds.push(id) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return Array.from(new Set(eventIds)) // Deduplicate
|
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
// Throttle profile prefetching to reduce frequency during rapid scrolling
|
|
|
|
|
// Clear any existing timeout
|
|
|
|
|
if (prefetchTimeoutRef.current) { |
|
|
|
|
clearTimeout(prefetchTimeoutRef.current) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debounce profile prefetching by 200ms to reduce frequency during rapid scrolling
|
|
|
|
|
prefetchTimeoutRef.current = setTimeout(() => { |
|
|
|
|
// Prefetch profiles for:
|
|
|
|
|
// 1. Currently visible events (first 60, reduced from 80)
|
|
|
|
|
// 2. Upcoming events that will be visible when scrolling (next 150, reduced from 300)
|
|
|
|
|
// This ensures profiles are ready before they're needed during rapid scrolling
|
|
|
|
|
const visiblePubkeys = Array.from( |
|
|
|
|
new Set(filteredEvents.slice(0, 60).map((ev) => ev.pubkey).filter((p) => p?.length === 64)) |
|
|
|
|
) |
|
|
|
|
const upcomingPubkeys = Array.from( |
|
|
|
|
new Set(events.slice(0, 150).map((ev) => ev.pubkey).filter((p) => p?.length === 64)) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Combine visible and upcoming, but prioritize visible ones
|
|
|
|
|
const allPubkeys = Array.from(new Set([...visiblePubkeys, ...upcomingPubkeys])) |
|
|
|
|
|
|
|
|
|
if (allPubkeys.length === 0) return |
|
|
|
|
|
|
|
|
|
// Check if we've already prefetched these exact pubkeys
|
|
|
|
|
const prev = visiblePubkeysRef.current |
|
|
|
|
const same = allPubkeys.length === prev.size && allPubkeys.every((p) => prev.has(p)) |
|
|
|
|
if (same) return |
|
|
|
|
|
|
|
|
|
// Find pubkeys that haven't been prefetched yet
|
|
|
|
|
const newPubkeys = allPubkeys.filter((p) => !prefetchedPubkeysRef.current.has(p)) |
|
|
|
|
|
|
|
|
|
if (newPubkeys.length === 0) { |
|
|
|
|
// All pubkeys already prefetched, just update the ref
|
|
|
|
|
visiblePubkeysRef.current = new Set(allPubkeys) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Update refs
|
|
|
|
|
visiblePubkeysRef.current = new Set(allPubkeys) |
|
|
|
|
newPubkeys.forEach((p) => prefetchedPubkeysRef.current.add(p)) |
|
|
|
|
|
|
|
|
|
// Batch fetch profiles for new pubkeys (IndexedDB + network in one request)
|
|
|
|
|
// This is the key optimization: batch processing prevents individual fetches during scrolling
|
|
|
|
|
client.fetchProfilesForPubkeys(newPubkeys).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
newPubkeys.forEach((p) => prefetchedPubkeysRef.current.delete(p)) |
|
|
|
|
}) |
|
|
|
|
}, 200) // Debounce by 200ms to reduce frequency
|
|
|
|
|
|
|
|
|
|
return () => { |
|
|
|
|
if (prefetchTimeoutRef.current) { |
|
|
|
|
clearTimeout(prefetchTimeoutRef.current) |
|
|
|
|
prefetchTimeoutRef.current = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, [filteredEvents, events, extractEmbeddedEventIds]) |
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch embedded events for visible events
|
|
|
|
|
useEffect(() => { |
|
|
|
|
const pubkeys = Array.from( |
|
|
|
|
new Set(filteredEvents.slice(0, 80).map((ev) => ev.pubkey).filter((p) => p?.length === 64)) |
|
|
|
|
) |
|
|
|
|
if (pubkeys.length === 0) return |
|
|
|
|
const prev = visiblePubkeysRef.current |
|
|
|
|
const same = pubkeys.length === prev.size && pubkeys.every((p) => prev.has(p)) |
|
|
|
|
if (same) return |
|
|
|
|
visiblePubkeysRef.current = new Set(pubkeys) |
|
|
|
|
client.fetchProfilesForPubkeys(pubkeys).catch(() => {}) |
|
|
|
|
}, [filteredEvents]) |
|
|
|
|
// Throttle embedded event prefetching to reduce frequency during rapid scrolling
|
|
|
|
|
// Clear any existing timeout
|
|
|
|
|
if (prefetchEmbeddedEventsTimeoutRef.current) { |
|
|
|
|
clearTimeout(prefetchEmbeddedEventsTimeoutRef.current) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debounce embedded event prefetching by 300ms to reduce frequency during rapid scrolling
|
|
|
|
|
prefetchEmbeddedEventsTimeoutRef.current = setTimeout(() => { |
|
|
|
|
// Extract embedded event IDs from visible events (first 60)
|
|
|
|
|
const visibleEmbeddedEventIds = new Set<string>() |
|
|
|
|
filteredEvents.slice(0, 60).forEach((ev) => { |
|
|
|
|
const embeddedIds = extractEmbeddedEventIds(ev) |
|
|
|
|
embeddedIds.forEach((id) => visibleEmbeddedEventIds.add(id)) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Also extract from upcoming events (next 150)
|
|
|
|
|
const upcomingEmbeddedEventIds = new Set<string>() |
|
|
|
|
events.slice(0, 150).forEach((ev) => { |
|
|
|
|
const embeddedIds = extractEmbeddedEventIds(ev) |
|
|
|
|
embeddedIds.forEach((id) => upcomingEmbeddedEventIds.add(id)) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Combine visible and upcoming
|
|
|
|
|
const allEmbeddedEventIds = Array.from( |
|
|
|
|
new Set([...visibleEmbeddedEventIds, ...upcomingEmbeddedEventIds]) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (allEmbeddedEventIds.length === 0) return |
|
|
|
|
|
|
|
|
|
// Filter out already prefetched event IDs
|
|
|
|
|
const eventIdsToFetch = allEmbeddedEventIds.filter( |
|
|
|
|
(id) => !prefetchedEventIdsRef.current.has(id) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (eventIdsToFetch.length === 0) return |
|
|
|
|
|
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.add(id)) |
|
|
|
|
|
|
|
|
|
// Batch fetch embedded events in background (non-blocking)
|
|
|
|
|
Promise.all(eventIdsToFetch.map((id) => client.fetchEvent(id))).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.delete(id)) |
|
|
|
|
}) |
|
|
|
|
}, 300) // Debounce by 300ms to reduce frequency
|
|
|
|
|
|
|
|
|
|
return () => { |
|
|
|
|
if (prefetchEmbeddedEventsTimeoutRef.current) { |
|
|
|
|
clearTimeout(prefetchEmbeddedEventsTimeoutRef.current) |
|
|
|
|
prefetchEmbeddedEventsTimeoutRef.current = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, [filteredEvents, events, extractEmbeddedEventIds]) |
|
|
|
|
|
|
|
|
|
// Also prefetch when loading more events (scrolling down)
|
|
|
|
|
// Throttled to reduce frequency during rapid scrolling
|
|
|
|
|
const prefetchNewEventsTimeoutRef = useRef<NodeJS.Timeout | null>(null) |
|
|
|
|
useEffect(() => { |
|
|
|
|
if (loading || !hasMore) return |
|
|
|
|
|
|
|
|
|
// Clear any existing timeout
|
|
|
|
|
if (prefetchNewEventsTimeoutRef.current) { |
|
|
|
|
clearTimeout(prefetchNewEventsTimeoutRef.current) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debounce profile prefetching for newly loaded events
|
|
|
|
|
prefetchNewEventsTimeoutRef.current = setTimeout(() => { |
|
|
|
|
// When we have more events loaded, prefetch profiles for the newly loaded ones
|
|
|
|
|
// Reduced from 200 to 100 to reduce batch size
|
|
|
|
|
const newlyLoadedPubkeys = Array.from( |
|
|
|
|
new Set(events.slice(showCount, showCount + 100).map((ev) => ev.pubkey).filter((p) => p?.length === 64)) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (newlyLoadedPubkeys.length > 0) { |
|
|
|
|
// Filter out already prefetched pubkeys
|
|
|
|
|
const newPubkeys = newlyLoadedPubkeys.filter((p) => !prefetchedPubkeysRef.current.has(p)) |
|
|
|
|
|
|
|
|
|
if (newPubkeys.length > 0) { |
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
newPubkeys.forEach((p) => prefetchedPubkeysRef.current.add(p)) |
|
|
|
|
|
|
|
|
|
// Batch fetch in background (non-blocking)
|
|
|
|
|
client.fetchProfilesForPubkeys(newPubkeys).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
newPubkeys.forEach((p) => prefetchedPubkeysRef.current.delete(p)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// CRITICAL: Prefetch embedded events for newly loaded events
|
|
|
|
|
const newlyLoadedEmbeddedEventIds = new Set<string>() |
|
|
|
|
events.slice(showCount, showCount + 100).forEach((ev) => { |
|
|
|
|
const embeddedIds = extractEmbeddedEventIds(ev) |
|
|
|
|
embeddedIds.forEach((id) => newlyLoadedEmbeddedEventIds.add(id)) |
|
|
|
|
}) |
|
|
|
|
const eventIdsToFetch = Array.from(newlyLoadedEmbeddedEventIds).filter( |
|
|
|
|
(id) => !prefetchedEventIdsRef.current.has(id) |
|
|
|
|
) |
|
|
|
|
if (eventIdsToFetch.length > 0) { |
|
|
|
|
// Mark as prefetched immediately to prevent duplicate requests
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.add(id)) |
|
|
|
|
// Batch fetch embedded events in background (non-blocking)
|
|
|
|
|
Promise.all(eventIdsToFetch.map((id) => client.fetchEvent(id))).catch(() => { |
|
|
|
|
// On error, remove from prefetched set so we can retry later
|
|
|
|
|
eventIdsToFetch.forEach((id) => prefetchedEventIdsRef.current.delete(id)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, 300) // Debounce by 300ms to reduce frequency during rapid scrolling
|
|
|
|
|
|
|
|
|
|
return () => { |
|
|
|
|
if (prefetchNewEventsTimeoutRef.current) { |
|
|
|
|
clearTimeout(prefetchNewEventsTimeoutRef.current) |
|
|
|
|
prefetchNewEventsTimeoutRef.current = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, [events.length, showCount, loading, hasMore]) |
|
|
|
|
|
|
|
|
|
const showNewEvents = () => { |
|
|
|
|
setEvents((oldEvents) => [...newEvents, ...oldEvents]) |
|
|
|
|
|