fix: 🐛

This commit is contained in:
codytseng
2025-07-04 09:11:50 +08:00
parent af07240df0
commit b470ef4857
2 changed files with 17 additions and 13 deletions

View File

@@ -30,8 +30,8 @@ export default function RepostButton({ event }: { event: Event }) {
const [isDrawerOpen, setIsDrawerOpen] = useState(false) const [isDrawerOpen, setIsDrawerOpen] = useState(false)
const { repostCount, hasReposted } = useMemo(() => { const { repostCount, hasReposted } = useMemo(() => {
return { return {
repostCount: noteStats?.reposts?.size, repostCount: noteStats?.repostPubkeySet?.size,
hasReposted: pubkey ? noteStats?.reposts?.has(pubkey) : false hasReposted: pubkey ? noteStats?.repostPubkeySet?.has(pubkey) : false
} }
}, [noteStats, event.id]) }, [noteStats, event.id])
const canRepost = !hasReposted && !reposting const canRepost = !hasReposted && !reposting
@@ -44,7 +44,7 @@ export default function RepostButton({ event }: { event: Event }) {
const timer = setTimeout(() => setReposting(false), 5000) const timer = setTimeout(() => setReposting(false), 5000)
try { try {
const hasReposted = noteStats?.reposts?.has(pubkey) const hasReposted = noteStats?.repostPubkeySet?.has(pubkey)
if (hasReposted) return if (hasReposted) return
if (!noteStats?.updatedAt) { if (!noteStats?.updatedAt) {
const events = await noteStatsService.fetchNoteStats(event, pubkey) const events = await noteStatsService.fetchNoteStats(event, pubkey)

View File

@@ -6,8 +6,10 @@ import dayjs from 'dayjs'
import { Event, Filter, kinds } from 'nostr-tools' import { Event, Filter, kinds } from 'nostr-tools'
export type TNoteStats = { export type TNoteStats = {
likeIdSet: Set<string>
likes: { id: string; pubkey: string; created_at: number; emoji: TEmoji | string }[] likes: { id: string; pubkey: string; created_at: number; emoji: TEmoji | string }[]
reposts: Set<string> repostPubkeySet: Set<string>
zapPrSet: Set<string>
zaps: { pr: string; pubkey: string; amount: number; comment?: string }[] zaps: { pr: string; pubkey: string; amount: number; comment?: string }[]
updatedAt?: number updatedAt?: number
} }
@@ -149,9 +151,9 @@ class NoteStatsService {
if (!targetEventId) return if (!targetEventId) return
const old = this.noteStatsMap.get(targetEventId) || {} const old = this.noteStatsMap.get(targetEventId) || {}
const likeIdSet = old.likeIdSet || new Set()
const likes = old.likes || [] const likes = old.likes || []
const exists = likes.find((l) => l.id === evt.id) if (likeIdSet.has(evt.id)) return
if (exists) return
let emoji: TEmoji | string = evt.content.trim() let emoji: TEmoji | string = evt.content.trim()
if (!emoji) return if (!emoji) return
@@ -167,8 +169,9 @@ class NoteStatsService {
} }
} }
likeIdSet.add(evt.id)
likes.push({ id: evt.id, pubkey: evt.pubkey, created_at: evt.created_at, emoji }) likes.push({ id: evt.id, pubkey: evt.pubkey, created_at: evt.created_at, emoji })
this.noteStatsMap.set(targetEventId, { ...old, likes }) this.noteStatsMap.set(targetEventId, { ...old, likeIdSet, likes })
return targetEventId return targetEventId
} }
@@ -177,9 +180,9 @@ class NoteStatsService {
if (!eventId) return if (!eventId) return
const old = this.noteStatsMap.get(eventId) || {} const old = this.noteStatsMap.get(eventId) || {}
const reposts = old.reposts || new Set() const repostPubkeySet = old.repostPubkeySet || new Set()
reposts.add(evt.id) repostPubkeySet.add(evt.pubkey)
this.noteStatsMap.set(eventId, { ...old, reposts }) this.noteStatsMap.set(eventId, { ...old, repostPubkeySet })
return eventId return eventId
} }
@@ -190,12 +193,13 @@ class NoteStatsService {
if (!originalEventId || !senderPubkey) return if (!originalEventId || !senderPubkey) return
const old = this.noteStatsMap.get(originalEventId) || {} const old = this.noteStatsMap.get(originalEventId) || {}
const zapPrSet = old.zapPrSet || new Set()
const zaps = old.zaps || [] const zaps = old.zaps || []
const exists = zaps.find((zap) => zap.pr === invoice) if (zapPrSet.has(invoice)) return
if (exists) return
zapPrSet.add(invoice)
zaps.push({ pr: invoice, pubkey: senderPubkey, amount, comment }) zaps.push({ pr: invoice, pubkey: senderPubkey, amount, comment })
this.noteStatsMap.set(originalEventId, { ...old, zaps }) this.noteStatsMap.set(originalEventId, { ...old, zapPrSet, zaps })
return originalEventId return originalEventId
} }
} }