"use client"; import React, { useEffect, useState } from 'react'; import { useRouter } from 'next/router'; import { useNostr } from '@/hooks/useNostr'; import { findKind0Fields } from '@/utils/nostr'; import { useImageProxy } from '@/hooks/useImageProxy'; import ZapDisplay from '@/components/zaps/ZapDisplay'; import { getSatAmountFromInvoice } from '@/utils/lightning'; import { Tag } from 'primereact/tag'; import { nip19 } from 'nostr-tools'; import { useLocalStorageWithEffect } from '@/hooks/useLocalStorage'; import Image from 'next/image'; import dynamic from 'next/dynamic'; import ZapThreadsWrapper from '@/components/ZapThreadsWrapper'; 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, } ); export default function CourseDetails({processedEvent}) { const [author, setAuthor] = useState(null); const [bitcoinConnect, setBitcoinConnect] = useState(false); const [nAddress, setNAddress] = useState(null); const [user] = useLocalStorageWithEffect('user', {}); const [zaps, setZaps] = useState([]); const [zapAmount, setZapAmount] = useState(0); const { returnImageProxy } = useImageProxy(); const { fetchKind0, zapEvent, fetchZapsForEvent } = useNostr(); const router = useRouter(); const handleZapEvent = async () => { if (!processedEvent) return; const response = await zapEvent(processedEvent); console.log('zap response:', response); } useEffect(() => { if (typeof window === 'undefined') return; const bitcoinConnectConfig = window.localStorage.getItem('bc:config'); if (bitcoinConnectConfig) { setBitcoinConnect(true); } }, []); useEffect(() => { const fetchAuthor = async (pubkey) => { const author = await fetchKind0(pubkey); const fields = await findKind0Fields(author); console.log('fields:', fields); if (fields) { setAuthor(fields); } } if (processedEvent) { fetchAuthor(processedEvent.pubkey); } }, [fetchKind0, processedEvent]); useEffect(() => { if (processedEvent?.d) { const naddr = nip19.naddrEncode({ pubkey: processedEvent.pubkey, kind: processedEvent.kind, identifier: processedEvent.d, }); setNAddress(naddr); } }, [processedEvent]); useEffect(() => { if (!zaps || zaps.length === 0) 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]); useEffect(() => { const fetchZaps = async () => { if (processedEvent) { const zaps = await fetchZapsForEvent(processedEvent); setZaps(zaps); } } fetchZaps(); }, [fetchZapsForEvent, processedEvent]); return (
{processedEvent?.summary}
Created by{' '} {author?.username}