"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import dynamic from "next/dynamic"; import { Button } from "@/components/ui/button"; import useProfile from "@/lib/hooks/useProfile"; import { HiOutlineLightningBolt } from "react-icons/hi"; import Spinner from "@/components/spinner"; import { getTagValues, getTagsValues } from "@/lib/nostr/utils"; import ProfileInfo from "./ProfileInfo"; import useCurrentUser from "@/lib/hooks/useCurrentUser"; import { useNDK } from "@/app/_providers/ndk"; import { toast } from "sonner"; import { sendZap, checkPayment, updateListUsersFromZaps, } from "@/lib/actions/zap"; import { useModal } from "@/app/_providers/modal/provider"; import { type NDKEvent } from "@nostr-dev-kit/ndk"; import { btcToSats, formatNumber } from "@/lib/utils"; import { formatDate } from "@/lib/utils/dates"; const EditListModal = dynamic(() => import("@/components/Modals/EditList"), { ssr: false, }); const CreateEventModal = dynamic(() => import("@/components/Modals/NewEvent"), { ssr: false, }); const ConfirmModal = dynamic(() => import("@/components/Modals/Confirm"), { ssr: false, }); export default function Header({ event }: { event: NDKEvent }) { const { currentUser } = useCurrentUser(); const modal = useModal(); const { ndk } = useNDK(); const [checkingPayment, setCheckingPayment] = useState(false); const [hasValidPayment, setHasValidPayment] = useState(false); const [syncingUsers, setSyncingUsers] = useState(false); const pubkey = event.pubkey; const { profile } = useProfile(pubkey); const noteIds = getTagsValues("e", event.tags).filter(Boolean); const title = getTagValues("title", event.tags) ?? getTagValues("name", event.tags) ?? "Untitled"; const image = getTagValues("image", event.tags) ?? getTagValues("picture", event.tags) ?? getTagValues("banner", event.tags) ?? profile?.banner; const description = getTagValues("description", event.tags); const rawEvent = event.rawEvent(); const subscriptionsEnabled = !!getTagValues("subscriptions", rawEvent.tags); const priceInBTC = parseFloat(getTagValues("price", rawEvent.tags) ?? "0"); const isMember = currentUser && getTagsValues("p", rawEvent.tags).includes(currentUser.pubkey); useEffect(() => { if (!currentUser || !subscriptionsEnabled) return; if (!isMember && !checkingPayment && !hasValidPayment) { void handleCheckPayment(); } }, [isMember, currentUser]); async function handleCheckPayment() { if (!event || !currentUser || !ndk) return; setCheckingPayment(true); console.log("Checking payment"); try { const result = await checkPayment( ndk, event.tagId(), currentUser.pubkey, rawEvent, ); console.log("Payment result", result); if (result) { setHasValidPayment(true); } } catch (err) { console.log("error sending zap", err); } finally { setCheckingPayment(false); } } async function handleSyncUsers() { if (!event || !ndk) return; setSyncingUsers(true); try { console.log("handleSyncUsers"); await updateListUsersFromZaps(ndk, event.tagId(), rawEvent); toast.success("Users Synced!"); } catch (err) { console.log("error syncing users", err); } finally { setSyncingUsers(false); } } async function handleSendZap() { try { const result = await sendZap( ndk!, btcToSats(priceInBTC), rawEvent, `Access payment: ${title}`, ); toast.success("Payment Sent!"); void handleCheckPayment(); } catch (err) { console.log("error sending zap", err); } finally { } } if (!event) { return (
); } return (
{!!image && ( banner )}

{title}

{!!currentUser && currentUser.pubkey === pubkey && ( <> )} {subscriptionsEnabled && !isMember && (hasValidPayment ? ( ) : ( ))}
{!!description && (

{description}

)}
); }