feat: 💨
This commit is contained in:
parent
167b6627f1
commit
89bb9ad2d0
4 changed files with 30 additions and 15 deletions
|
|
@ -137,7 +137,13 @@ const NoteList = forwardRef<
|
||||||
const filteredEvents: Event[] = []
|
const filteredEvents: Event[] = []
|
||||||
const keys: string[] = []
|
const keys: string[] = []
|
||||||
|
|
||||||
const mergedEvents = mergeTimelines([events, storedEvents], LIMIT)
|
let mergedEvents: Event[] = events
|
||||||
|
if (
|
||||||
|
storedEvents.length &&
|
||||||
|
(!events.length || storedEvents[0].created_at >= events[events.length - 1].created_at)
|
||||||
|
) {
|
||||||
|
mergedEvents = mergeTimelines([storedEvents, events])
|
||||||
|
}
|
||||||
mergedEvents.forEach((evt) => {
|
mergedEvents.forEach((evt) => {
|
||||||
const key = getEventKey(evt)
|
const key = getEventKey(evt)
|
||||||
if (keySet.has(key)) return
|
if (keySet.has(key)) return
|
||||||
|
|
|
||||||
|
|
@ -211,14 +211,23 @@ const NotificationList = forwardRef((_, ref) => {
|
||||||
}, [timelineKey, until, pubkey, setEvents, setUntil])
|
}, [timelineKey, until, pubkey, setEvents, setUntil])
|
||||||
|
|
||||||
const notifications = useMemo(() => {
|
const notifications = useMemo(() => {
|
||||||
return mergeTimelines(
|
const filteredEvents = events.filter((evt) => evt.pubkey !== pubkey)
|
||||||
[
|
if (storedEvents.length === 0) return filteredEvents
|
||||||
events.filter((evt) => evt.pubkey !== pubkey),
|
|
||||||
storedEvents.filter((evt) => evt.pubkey !== pubkey)
|
const filteredStoredEvents = storedEvents.filter((evt) => evt.pubkey !== pubkey)
|
||||||
],
|
if (!initialLoading) {
|
||||||
LIMIT
|
return mergeTimelines([filteredEvents, filteredStoredEvents])
|
||||||
)
|
}
|
||||||
}, [events, storedEvents, pubkey])
|
|
||||||
|
if (
|
||||||
|
!filteredEvents.length ||
|
||||||
|
storedEvents[0].created_at >= filteredEvents[filteredEvents.length - 1].created_at
|
||||||
|
) {
|
||||||
|
return mergeTimelines([filteredEvents, filteredStoredEvents])
|
||||||
|
}
|
||||||
|
// Stored events are too old
|
||||||
|
return filteredEvents
|
||||||
|
}, [events, storedEvents, pubkey, initialLoading])
|
||||||
|
|
||||||
const { visibleItems, shouldShowLoadingIndicator, bottomRef, setShowCount } = useInfiniteScroll({
|
const { visibleItems, shouldShowLoadingIndicator, bottomRef, setShowCount } = useInfiniteScroll({
|
||||||
items: notifications,
|
items: notifications,
|
||||||
|
|
|
||||||
|
|
@ -293,7 +293,7 @@ const UserAggregationList = forwardRef<
|
||||||
}, [since])
|
}, [since])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const mergedEvents = mergeTimelines([events, storedEvents], LIMIT)
|
const mergedEvents = mergeTimelines([events, storedEvents])
|
||||||
filterEvents(mergedEvents).then((filtered) => {
|
filterEvents(mergedEvents).then((filtered) => {
|
||||||
setFilteredEvents(filtered)
|
setFilteredEvents(filtered)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
import { NostrEvent } from 'nostr-tools'
|
import { NostrEvent } from 'nostr-tools'
|
||||||
import { compareEvents } from './event'
|
import { compareEvents } from './event'
|
||||||
|
|
||||||
export function mergeTimelines(timelines: NostrEvent[][], limit: number) {
|
export function mergeTimelines(timelines: NostrEvent[][], limit?: number) {
|
||||||
if (timelines.length === 0) return []
|
if (timelines.length === 0) return []
|
||||||
if (timelines.length === 1) return [...timelines[0]]
|
if (timelines.length === 1) return [...timelines[0]]
|
||||||
return timelines.reduce((merged, current) => _mergeTimelines(merged, current, limit), [])
|
return timelines.reduce((merged, current) => _mergeTimelines(merged, current, limit), [])
|
||||||
}
|
}
|
||||||
|
|
||||||
function _mergeTimelines(a: NostrEvent[], b: NostrEvent[], limit: number): NostrEvent[] {
|
function _mergeTimelines(a: NostrEvent[], b: NostrEvent[], limit?: number): NostrEvent[] {
|
||||||
if (a.length === 0) return [...b]
|
if (a.length === 0) return [...b]
|
||||||
if (b.length === 0) return [...a]
|
if (b.length === 0) return [...a]
|
||||||
|
|
||||||
|
|
@ -29,16 +29,16 @@ function _mergeTimelines(a: NostrEvent[], b: NostrEvent[], limit: number): Nostr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.length >= limit) {
|
if (limit && result.length >= limit) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i < a.length && result.length < limit) {
|
while (i < a.length && (!limit || result.length < limit)) {
|
||||||
result.push(a[i])
|
result.push(a[i])
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
while (j < b.length && result.length < limit) {
|
while (j < b.length && (!limit || result.length < limit)) {
|
||||||
result.push(b[j])
|
result.push(b[j])
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue