import React, { useEffect, useState } from 'react'; import { useRouter } from 'next/router'; import { parseEvent, findKind0Fields, hexToNpub } from '@/utils/nostr'; import { useImageProxy } from '@/hooks/useImageProxy'; import { getSatAmountFromInvoice } from '@/utils/lightning'; import ZapDisplay from '@/components/zaps/ZapDisplay'; import { Tag } from 'primereact/tag'; import { nip19, nip04 } from 'nostr-tools'; import { useSession } from 'next-auth/react'; import Image from 'next/image'; import dynamic from 'next/dynamic'; import ZapThreadsWrapper from '@/components/ZapThreadsWrapper'; import { useNDKContext } from '@/context/NDKContext'; import { useZapsSubscription } from '@/hooks/nostrQueries/zaps/useZapsSubscription'; import 'primeicons/primeicons.css'; const MDDisplay = dynamic( () => import("@uiw/react-markdown-preview"), { ssr: false, } ); const BitcoinConnectPayButton = dynamic( () => import('@getalby/bitcoin-connect-react').then((mod) => mod.PayButton), { ssr: false, } ); const privkey = process.env.NEXT_PUBLIC_APP_PRIV_KEY; export default function Details() { const [event, setEvent] = useState(null); const [processedEvent, setProcessedEvent] = useState({}); const [author, setAuthor] = useState(null); const [bitcoinConnect, setBitcoinConnect] = useState(false); const [nAddress, setNAddress] = useState(null); const [zapAmount, setZapAmount] = useState(null); const [paidResource, setPaidResource] = useState(false); const [decryptedContent, setDecryptedContent] = useState(null); const ndk = useNDKContext(); const { data: session, status } = useSession(); const [user, setUser] = useState(null); const { returnImageProxy } = useImageProxy(); const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: processedEvent }); const router = useRouter(); useEffect(() => { if (session) { setUser(session.user); } }, [session]); useEffect(() => { if (processedEvent.price) { setPaidResource(true); } }, [processedEvent]); useEffect(() => { if (typeof window === 'undefined') return; const bitcoinConnectConfig = window.localStorage.getItem('bc:config'); if (bitcoinConnectConfig) { setBitcoinConnect(true); } }, []); useEffect(() => { const decryptContent = async () => { if (user && paidResource) { if (!user.purchased.includes(processedEvent.id)) { // decrypt the content console.log('privkey', privkey); console.log('user.pubkey', user.pubkey); console.log('processedEvent.content', processedEvent.content); const decryptedContent = await nip04.decrypt(privkey, user.pubkey, processedEvent.content); console.log('decryptedContent', decryptedContent); setDecryptedContent(decryptedContent); } } } decryptContent(); }, [user, paidResource, processedEvent]); useEffect(() => { if (router.isReady) { const { slug } = router.query; const fetchEvent = async (slug) => { try { await ndk.connect(); const filter = { ids: [slug] } const event = await ndk.fetchEvent(filter); if (event) { setEvent(event); } } catch (error) { console.error('Error fetching event:', error); } }; if (ndk) { fetchEvent(slug); } } }, [router.isReady, router.query, ndk]); useEffect(() => { const fetchAuthor = async (pubkey) => { try { await ndk.connect(); const filter = { kinds: [0], authors: [pubkey] } const author = await ndk.fetchEvent(filter); if (author) { const fields = await findKind0Fields(JSON.parse(author.content)); setAuthor(fields); } } catch (error) { console.error('Error fetching author:', error); } } if (event && ndk) { fetchAuthor(event.pubkey); } }, [ndk, event]); useEffect(() => { if (event) { const parsedEvent = parseEvent(event); setProcessedEvent(parsedEvent); } }, [event]); useEffect(() => { if (processedEvent?.d) { const naddr = nip19.naddrEncode({ pubkey: processedEvent.pubkey, kind: processedEvent.kind, identifier: processedEvent.d, }); setNAddress(naddr); } }, [processedEvent]); useEffect(() => { if (!zaps) return; let total = 0; zaps.forEach((zap) => { const bolt11Tag = zap.tags.find(tag => tag[0] === "bolt11"); const invoice = bolt11Tag ? bolt11Tag[1] : null; if (invoice) { const amount = getSatAmountFromInvoice(invoice); total += amount; } }); setZapAmount(total); }, [zaps]); return (
{processedEvent?.summary}
Created by{' '} {author?.username}