import { useState } from "react"; import { Button } from "@/components/ui/button"; import { HiOutlineLightningBolt } from "react-icons/hi"; import RSVPModal from "@/components/Modals/RSVP"; import ConfirmModal from "@/components/Modals/Confirm"; import { 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"; 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("name", 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); 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) { return ( ); } if (price) { return ( ); } return ( ); }