import { NDKEvent, NDKFilter } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; import { decode } from 'light-bolt11-decoder'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; import { LoaderIcon } from '@shared/icons'; import { MiniUser } from '@shared/notes/users/mini'; import { widgetKinds } from '@stores/constants'; import { useWidgets } from '@stores/widgets'; import { compactNumber } from '@utils/number'; export function NoteMetadata({ id }: { id: string }) { const setWidget = useWidgets((state) => state.setWidget); const { db } = useStorage(); const { ndk } = useNDK(); const { status, data } = useQuery( ['note-metadata', id], async () => { let replies = 0; let zap = 0; const users = []; const filter: NDKFilter = { '#e': [id], kinds: [1, 9735], }; const events = await ndk.fetchEvents(filter); events.forEach((event: NDKEvent) => { switch (event.kind) { case 1: replies += 1; if (users.length < 3) users.push(event.pubkey); break; case 9735: { const bolt11 = event.tags.find((tag) => tag[0] === 'bolt11')[1]; if (bolt11) { const decoded = decode(bolt11); const amount = decoded.sections.find((item) => item.name === 'amount'); const sats = amount.value / 1000; zap += sats; } break; } default: break; } }); return { replies, users, zap }; }, { refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false } ); if (status === 'loading') { return (
{compactNumber.format(data.zap)} {' '} zaps