import { NDKEvent, NDKFilter, NDKKind, NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { formatDate } from 'date-fns' import { useAppSelector, useDidMount, useNDKContext } from 'hooks' import { useState } from 'react' import { useParams, useLocation, Link, useSubmit, useNavigation } from 'react-router-dom' import { getModPageRoute, getBlogPageRoute, getProfilePageRoute, appRoutes } from 'routes' import { CommentEvent, UserProfile } from 'types' import { hexToNpub } from 'utils' import { Reactions } from './Reactions' import { Zap } from './Zap' import { nip19 } from 'nostr-tools' import { CommentContent } from './CommentContent' import { NoteQuoteRepostPopup } from 'components/Notes/NoteQuoteRepostPopup' import { NoteRepostPopup } from 'components/Notes/NoteRepostPopup' interface CommentProps { comment: CommentEvent } export const Comment = ({ comment }: CommentProps) => { const { naddr } = useParams() const location = useLocation() const { ndk } = useNDKContext() const isMod = location.pathname.includes('/mod/') const isBlog = location.pathname.includes('/blog/') const isNote = location.pathname.includes('/feed') const baseUrl = naddr ? isMod ? getModPageRoute(naddr) : isBlog ? getBlogPageRoute(naddr) : undefined : isNote ? `${appRoutes.feed}/` : undefined const [commentEvents, setCommentEvents] = useState([]) const [profile, setProfile] = useState() const submit = useSubmit() const navigation = useNavigation() const [repostEvents, setRepostEvents] = useState([]) const [quoteRepostEvents, setQuoteRepostEvents] = useState([]) const [hasReposted, setHasReposted] = useState(false) const [hasQuoted, setHasQuoted] = useState(false) const [showRepostPopup, setShowRepostPopup] = useState(false) const [showQuoteRepostPopup, setShowQuoteRepostPopup] = useState(false) const userState = useAppSelector((state) => state.user) const userPubkey = userState.user?.pubkey as string | undefined useDidMount(() => { comment.event.author.fetchProfile().then((res) => setProfile(res)) ndk .fetchEvents({ kinds: [NDKKind.Text, NDKKind.GenericReply], '#e': [comment.event.id] }) .then((ndkEventsSet) => { setCommentEvents( Array.from(ndkEventsSet).map((ndkEvent) => ({ event: ndkEvent })) ) }) const repostFilter: NDKFilter = { kinds: [NDKKind.Repost], '#e': [comment.event.id] } const quoteFilter: NDKFilter = { kinds: [NDKKind.Text], '#q': [comment.event.id] } ndk .fetchEvents([repostFilter, quoteFilter], { closeOnEose: true, cacheUsage: NDKSubscriptionCacheUsage.PARALLEL }) .then((ndkEventSet) => { const ndkEvents = Array.from(ndkEventSet) if (ndkEventSet.size) { const quoteRepostEvents = ndkEvents.filter( (n) => n.kind === NDKKind.Text ) userPubkey && setHasQuoted( quoteRepostEvents.some((qr) => qr.pubkey === userPubkey) ) setQuoteRepostEvents(quoteRepostEvents) const repostEvents = ndkEvents.filter( (n) => n.kind === NDKKind.Repost ) userPubkey && setHasReposted(repostEvents.some((qr) => qr.pubkey === userPubkey)) setRepostEvents(repostEvents) } }) }) const handleRepost = async (confirm: boolean) => { if (navigation.state !== 'idle') return setShowRepostPopup(false) // Cancel if not confirmed if (!confirm) return const repostNdkEvent = await comment.event.repost(false) const rawEvent = repostNdkEvent.rawEvent() submit( JSON.stringify({ intent: 'repost', note1: comment.event.encode(), data: rawEvent }), { method: 'post', encType: 'application/json', action: appRoutes.feed } ) } const profileRoute = getProfilePageRoute( nip19.nprofileEncode({ pubkey: comment.event.pubkey }) ) return (
{profile?.displayName || profile?.name || ''}{' '} {hexToNpub(comment.event.pubkey)}
{comment.event.created_at && ( )}
{comment.status && (

Status: {comment.status}

)}
{comment.event.kind === NDKKind.Text && ( <> {/* Quote Repost, Kind 1 */}
setShowQuoteRepostPopup(true) : undefined } >

{quoteRepostEvents.length}

{showQuoteRepostPopup && ( setShowQuoteRepostPopup(false)} /> )} {/* Repost, Kind 6 */}
setShowRepostPopup(true) : undefined } >

{repostEvents.length}

{showRepostPopup && ( setShowRepostPopup(false)} /> )} )} {typeof profile?.lud16 !== 'undefined' && profile.lud16 !== '' && ( )}

{commentEvents.length}

Replies

Reply

) }