fix: reply list loading state

This commit is contained in:
codytseng
2024-11-24 20:53:44 +08:00
parent 3aa383ad32
commit 6571209346
2 changed files with 17 additions and 6 deletions

View File

@@ -33,10 +33,12 @@ export default function ReplyNoteList({ event, className }: { event: Event; clas
try { try {
const relayList = await client.fetchRelayList(event.pubkey) const relayList = await client.fetchRelayList(event.pubkey)
const closer = await client.subscribeReplies(relayList.read.slice(0, 5), event.id, 100, { const closer = await client.subscribeReplies(relayList.read.slice(0, 5), event.id, 100, {
onReplies: (evts, until) => { onReplies: (evts, isCache, until) => {
setReplies(evts) setReplies(evts)
setUntil(until) setUntil(until)
setLoading(false) if (!isCache) {
setLoading(false)
}
}, },
onNew: (evt) => { onNew: (evt) => {
setReplies((pre) => [...pre, evt]) setReplies((pre) => [...pre, evt])

View File

@@ -159,7 +159,7 @@ class ClientService {
onReplies, onReplies,
onNew onNew
}: { }: {
onReplies: (events: NEvent[], until?: number) => void onReplies: (events: NEvent[], isCache: boolean, until?: number) => void
onNew: (evt: NEvent) => void onNew: (evt: NEvent) => void
} }
) { ) {
@@ -170,7 +170,7 @@ class ClientService {
replies = (await Promise.all(cache.refs.map(([id]) => this.eventCache.get(id)))).filter( replies = (await Promise.all(cache.refs.map(([id]) => this.eventCache.get(id)))).filter(
Boolean Boolean
) as NEvent[] ) as NEvent[]
onReplies(replies, cache.until) onReplies(replies, true, cache.until)
} else { } else {
cache = { refs } cache = { refs }
this.repliesCache.set(parentEventId, cache) this.repliesCache.set(parentEventId, cache)
@@ -206,12 +206,21 @@ class ClientService {
hasEosed = true hasEosed = true
const newReplies = events.sort((a, b) => a.created_at - b.created_at) const newReplies = events.sort((a, b) => a.created_at - b.created_at)
replies = replies.concat(newReplies) replies = replies.concat(newReplies)
refs.push(...newReplies.map((evt) => [evt.id, evt.created_at] as [string, number]))
// first fetch // first fetch
if (!since) { if (!since) {
cache.until = events.length >= limit ? events[0].created_at - 1 : undefined cache.until = events.length >= limit ? events[0].created_at - 1 : undefined
} }
onReplies(replies, cache.until) onReplies(replies, false, cache.until)
const lastRefCreatedAt = refs.length ? refs[refs.length - 1][1] : undefined
if (lastRefCreatedAt) {
refs.push(
...newReplies
.filter((reply) => reply.created_at > lastRefCreatedAt)
.map((evt) => [evt.id, evt.created_at] as [string, number])
)
} else {
refs.push(...newReplies.map((evt) => [evt.id, evt.created_at] as [string, number]))
}
} }
} }
) )