fix: resolve issue with like and repost counts being overwritten

This commit is contained in:
codytseng
2024-11-12 15:02:56 +08:00
parent 9dd648ffa5
commit ab667afc30

View File

@@ -65,7 +65,10 @@ export function NoteStatsProvider({ children }: { children: React.ReactNode }) {
const newMap = new Map(prev) const newMap = new Map(prev)
for (const [eventId, count] of countMap) { for (const [eventId, count] of countMap) {
const old = prev.get(eventId) const old = prev.get(eventId)
newMap.set(eventId, old ? { ...old, likeCount: count } : { likeCount: count }) newMap.set(
eventId,
old ? { ...old, likeCount: Math.max(count, old.likeCount ?? 0) } : { likeCount: count }
)
} }
return newMap return newMap
}) })
@@ -84,7 +87,9 @@ export function NoteStatsProvider({ children }: { children: React.ReactNode }) {
const old = prev.get(event.id) const old = prev.get(event.id)
newMap.set( newMap.set(
event.id, event.id,
old ? { ...old, repostCount: events.length } : { repostCount: events.length } old
? { ...old, repostCount: Math.max(events.length, old.repostCount ?? 0) }
: { repostCount: events.length }
) )
return newMap return newMap
}) })
@@ -95,7 +100,7 @@ export function NoteStatsProvider({ children }: { children: React.ReactNode }) {
if (!pubkey) return false if (!pubkey) return false
const relayList = await client.fetchRelayList(pubkey) const relayList = await client.fetchRelayList(pubkey)
const events = await client.fetchEvents(relayList.write.slice(0, 3), { const events = await client.fetchEvents(relayList.write, {
'#e': [event.id], '#e': [event.id],
authors: [pubkey], authors: [pubkey],
kinds: [kinds.Reaction] kinds: [kinds.Reaction]
@@ -123,7 +128,7 @@ export function NoteStatsProvider({ children }: { children: React.ReactNode }) {
if (!pubkey) return false if (!pubkey) return false
const relayList = await client.fetchRelayList(pubkey) const relayList = await client.fetchRelayList(pubkey)
const events = await client.fetchEvents(relayList.write.slice(0, 3), { const events = await client.fetchEvents(relayList.write, {
'#e': [event.id], '#e': [event.id],
authors: [pubkey], authors: [pubkey],
kinds: [kinds.Repost] kinds: [kinds.Repost]