From ec1a9777deb1c40c1f0eb3e5a84d35fde6fe053f Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sun, 13 Jul 2025 04:09:32 +0000 Subject: [PATCH] use event directly --- src/components/ZapDialog.tsx | 28 ++++------------------------ src/hooks/useZaps.ts | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/components/ZapDialog.tsx b/src/components/ZapDialog.tsx index 6e567c1..0b196c1 100644 --- a/src/components/ZapDialog.tsx +++ b/src/components/ZapDialog.tsx @@ -17,20 +17,12 @@ import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAuthor } from '@/hooks/useAuthor'; import { useToast } from '@/hooks/useToast'; import { useZaps } from '@/hooks/useZaps'; -import { requestProvider } from 'webln'; import type { WebLNProvider } from 'webln'; import QRCode from 'qrcode'; - -export interface ZapTarget { - pubkey: string; - id: string; - relays?: string[]; - dTag?: string; - naddr?: string; -} +import type { Event } from 'nostr-tools'; interface ZapDialogProps { - target: ZapTarget; + target: Event; children?: React.ReactNode; className?: string; } @@ -39,7 +31,7 @@ const presetAmounts = [1, 50, 100, 250, 1000]; export function ZapDialog({ target, children, className }: ZapDialogProps) { const [open, setOpen] = useState(false); - const [webln, setWebln] = useState(null); + const [webln, _setWebln] = useState(null); const { user } = useCurrentUser(); const { data: author } = useAuthor(target.pubkey); const { toast } = useToast(); @@ -82,18 +74,6 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) { zap(finalAmount, comment); }; - const handleTriggerClick = async () => { - if (!webln) { - try { - const provider = await requestProvider(); - setWebln(provider); - } catch (err) { - // Silently fail - console.error(err); - } - } - }; - if (!user || user.pubkey === target.pubkey || !author?.metadata?.lud16) { return null; } @@ -101,7 +81,7 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) { return ( - diff --git a/src/hooks/useZaps.ts b/src/hooks/useZaps.ts index 72eb1e0..ccaf966 100644 --- a/src/hooks/useZaps.ts +++ b/src/hooks/useZaps.ts @@ -6,12 +6,12 @@ import { useAppContext } from '@/hooks/useAppContext'; import { useToast } from '@/hooks/useToast'; import { nip57, nip19, Event } from 'nostr-tools'; import type { WebLNProvider } from 'webln'; -import type { ZapTarget } from '@/components/ZapDialog'; + import { useQuery } from '@tanstack/react-query'; import { useNostr } from '@nostrify/react'; import type { NostrEvent, NostrFilter } from '@nostrify/nostrify'; -export function useZaps(target: ZapTarget, webln: WebLNProvider | null, onZapSuccess?: () => void) { +export function useZaps(target: Event, webln: WebLNProvider | null, onZapSuccess?: () => void) { const { nostr } = useNostr(); const { toast } = useToast(); const { user } = useCurrentUser(); @@ -21,19 +21,28 @@ export function useZaps(target: ZapTarget, webln: WebLNProvider | null, onZapSuc const [isZapping, setIsZapping] = useState(false); const [invoice, setInvoice] = useState(null); - const queryKey = target.naddr ? `naddr:${target.naddr}` : `event:${target.id}`; + const naddr = + target.kind >= 30000 && target.kind < 40000 + ? nip19.naddrEncode({ + identifier: target.tags.find((t) => t[0] === 'd')?.[1] || '', + pubkey: target.pubkey, + kind: target.kind, + }) + : undefined; + + const queryKey = naddr ? `naddr:${naddr}` : `event:${target.id}`; const { data: zaps, ...query } = useQuery({ queryKey: ['zaps', queryKey], queryFn: async (c) => { - if (!target.id && !target.naddr) return []; + if (!target.id && !naddr) return []; const signal = AbortSignal.any([c.signal, AbortSignal.timeout(1500)]); const filters: NostrFilter[] = []; - if (target.naddr) { + if (naddr) { try { - const decoded = nip19.decode(target.naddr); + const decoded = nip19.decode(naddr); if (decoded.type === 'naddr') { const { kind, pubkey, identifier } = decoded.data; filters.push({ @@ -42,7 +51,7 @@ export function useZaps(target: ZapTarget, webln: WebLNProvider | null, onZapSuc }); } } catch (e) { - console.error("Invalid naddr", target.naddr, e); + console.error("Invalid naddr", naddr, e); } } else { filters.push({ @@ -56,7 +65,7 @@ export function useZaps(target: ZapTarget, webln: WebLNProvider | null, onZapSuc const events = await nostr.query(filters, { signal }); return events; }, - enabled: !!target.id || !!target.naddr, + enabled: !!target.id || !!naddr, }); const zap = async (amount: number, comment: string) => { @@ -119,9 +128,9 @@ export function useZaps(target: ZapTarget, webln: WebLNProvider | null, onZapSuc comment: comment, }); - if (target.naddr) { - const naddr = nip19.decode(target.naddr).data as nip19.AddressPointer; - zapRequest.tags.push(["a", `${naddr.kind}:${naddr.pubkey}:${naddr.identifier}`]); + if (naddr) { + const decoded = nip19.decode(naddr).data as nip19.AddressPointer; + zapRequest.tags.push(["a", `${decoded.kind}:${decoded.pubkey}:${decoded.identifier}`]); zapRequest.tags = zapRequest.tags.filter(t => t[0] !== 'e'); }