147 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-11-02 10:50:49 -04:00
import { useState } from "react";
2023-10-25 12:56:51 -04:00
import { Button } from "@/components/ui/button";
2023-11-02 10:50:49 -04:00
import { HiOutlineLightningBolt } from "react-icons/hi";
2023-10-25 12:56:51 -04:00
import RSVPModal from "@/components/Modals/RSVP";
2023-11-02 10:50:49 -04:00
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";
2023-10-25 12:56:51 -04:00
type RSVPButtonProps = {
2023-11-02 10:50:49 -04:00
event: NDKEvent;
2023-10-25 12:56:51 -04:00
};
2023-11-02 10:50:49 -04:00
export default function RSVPButton({ event }: RSVPButtonProps) {
2023-10-25 12:56:51 -04:00
const modal = useModal();
2023-11-02 10:50:49 -04:00
const { currentUser } = useCurrentUser();
const { ndk } = useNDK();
const eventReference = event.tagId();
2023-11-02 10:50:49 -04:00
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);
}
}
2023-10-25 12:56:51 -04:00
2023-11-02 10:50:49 -04:00
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 (
<Button
onClick={() =>
modal?.show(<RSVPModal eventReference={eventReference} />)
}
>
RSVP
</Button>
);
}
if (price) {
return (
<Button
onClick={() =>
modal?.show(
<ConfirmModal
title={`Buy Ticket for ${name}`}
onConfirm={handleBuyTicket}
ctaBody={
<>
<span>Zap for Ticket</span>
<HiOutlineLightningBolt className="h-4 w-4" />
</>
}
>
<p className="text-muted-forground">
{`Pay ${priceInBTC} BTC (${formatNumber(
btcToSats(priceInBTC),
)} sats) for one ticket for ${name}`}
</p>
</ConfirmModal>,
)
}
>
Buy Ticket
</Button>
);
}
2023-10-25 12:56:51 -04:00
return (
<Button
2023-11-02 10:50:49 -04:00
onClick={() =>
modal?.show(
<ConfirmModal
title={`Get Ticket for ${name}`}
onConfirm={handleBuyTicket}
ctaBody={
<>
<span>Zap to Subscribe</span>
<HiOutlineLightningBolt className="h-4 w-4" />
</>
}
>
<p className="text-muted-forground">
{`Pay ${priceInBTC} BTC (${formatNumber(
btcToSats(priceInBTC),
)} sats) for year long access until ${formatDate(
new Date(new Date().setFullYear(new Date().getFullYear() + 1)),
"MMM Do, YYYY",
)}`}
</p>
</ConfirmModal>,
)
}
2023-10-25 12:56:51 -04:00
>
2023-11-02 10:50:49 -04:00
Get Ticket
2023-10-25 12:56:51 -04:00
</Button>
);
}