import { useState } from "react"; import { Button } from "@/components/ui/button"; import { HiOutlineLightningBolt } from "react-icons/hi"; import { RiRepeatFill } from "react-icons/ri"; import RSVPModal from "@/components/Modals/RSVP"; import ConfirmModal from "@/components/Modals/Confirm"; import { NDKKind, type NDKEvent } from "@nostr-dev-kit/ndk"; import { getTagAllValues, getTagValues } from "@/lib/nostr/utils"; import { formatDate } from "@/lib/utils/dates"; import { useModal } from "@/app/_providers/modal/provider"; import { btcToSats, formatNumber } from "@/lib/utils"; import useCurrentUser from "@/lib/hooks/useCurrentUser"; import { useNDK } from "@/app/_providers/ndk"; import { toast } from "sonner"; import { sendZap, checkPayment } from "@/lib/actions/zap"; import { useEvent } from "@/lib/hooks/useEvents"; type RSVPButtonProps = { event: NDKEvent; }; export default function RSVPButton({ event }: RSVPButtonProps) { const modal = useModal(); const { currentUser } = useCurrentUser(); const { ndk } = useNDK(); const eventReference = event.tagId(); const name = getTagValues("title", event.tags); const tickets = getTagValues("tickets", event.tags); const price = getTagAllValues("price", event.tags); const priceInBTC = parseFloat(getTagValues("price", event.tags) ?? "0"); const [ticketPending, setTicketPending] = useState(false); const [checkingPayment, setCheckingPayment] = useState(false); const [hasValidPayment, setHasValidPayment] = useState(false); const { event: rsvpEvent } = useEvent({ filter: { kinds: [31925 as NDKKind], authors: [currentUser?.pubkey as string], ["#a"]: [eventReference], }, enabled: !!currentUser, }); async function handleBuyTicket() { setTicketPending(true); try { if (!currentUser || !ndk?.signer) return; const result = await sendZap( ndk!, btcToSats(priceInBTC), event.rawEvent(), `Ticket payment: ${name}`, ); toast.success("Payment Sent!"); void handleCheckPayment(); } catch (err) { console.log("error sending zap", err); } finally { setTicketPending(false); } } async function handleCheckPayment() { if (!event || !currentUser || !ndk) return; setCheckingPayment(true); try { const result = await checkPayment( ndk, event.tagId(), currentUser.pubkey, event.rawEvent(), ); console.log("Payment result", result); if (result) { setHasValidPayment(true); } } catch (err) { console.log("error sending zap", err); } finally { setCheckingPayment(false); } } if (!tickets) { if (rsvpEvent) { const rsvpResponse = getTagValues("status", rsvpEvent.tags); const rsvpResponseDeprecated = getTagValues("l", rsvpEvent.tags); return (
); } else { return ( ); } } if (price) { return ( ); } return ( ); }