import { formatDate } from 'date-fns' import { useBodyScrollDisable, useNDKContext, useReplies } from 'hooks' import { nip19 } from 'nostr-tools' import { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react' import { Link, useLoaderData, useLocation, useNavigate, useParams } from 'react-router-dom' import { getBlogPageRoute, getModPageRoute, getProfilePageRoute } from 'routes' import { CommentEvent, UserProfile } from 'types' import { CommentsLoaderResult } from 'types/comments' import { adjustTextareaHeight, handleCommentSubmit, hexToNpub } from 'utils' import { Reactions } from './Reactions' import { Zap } from './Zap' import { NDKKind } from '@nostr-dev-kit/ndk' import { Comment } from './Comment' import { useComments } from 'hooks/useComments' import { CommentContent } from './CommentContent' import { Dots } from 'components/Spinner' export const CommentsPopup = () => { const { naddr } = useParams() const location = useLocation() const { ndk } = useNDKContext() useBodyScrollDisable(true) const isMod = location.pathname.includes('/mod/') const isBlog = location.pathname.includes('/blog/') const baseUrl = naddr ? isMod ? getModPageRoute(naddr) : isBlog ? getBlogPageRoute(naddr) : undefined : undefined const { event } = useLoaderData() as CommentsLoaderResult const { size, parent: replyEvent, isComplete, root: rootEvent } = useReplies(event.tagValue('e')) const isRoot = event.tagValue('a') === event.tagValue('A') const [profile, setProfile] = useState() const { commentEvents, setCommentEvents } = useComments( event.author.pubkey, undefined, event.id ) useEffect(() => { event.author.fetchProfile().then((res) => setProfile(res)) }, [event.author]) const profileRoute = useMemo( () => getProfilePageRoute( nip19.nprofileEncode({ pubkey: event.pubkey }) ), [event.pubkey] ) const navigate = useNavigate() const [isSubmitting, setIsSubmitting] = useState(false) const [replyText, setReplyText] = useState('') const handleChange = useCallback((e: ChangeEvent) => { const value = e.currentTarget.value setReplyText(value) adjustTextareaHeight(e.currentTarget) }, []) const [visible, setVisible] = useState([]) const discoveredCount = commentEvents.length - visible.length const [isLoading, setIsLoading] = useState(true) useEffect(() => { // Initial loading to indicate comments fetching (stop after 5 seconds) const t = window.setTimeout(() => setIsLoading(false), 5000) return () => { window.clearTimeout(t) } }, []) useEffect(() => { if (isLoading) { setVisible(commentEvents) } }, [commentEvents, isLoading]) const handleDiscoveredClick = () => { setVisible(commentEvents) } const handleSubmit = handleCommentSubmit( event, setCommentEvents, setVisible, ndk ) const handleComment = async () => { setIsSubmitting(true) const submitted = await handleSubmit(replyText) if (submitted) setReplyText('') setIsSubmitting(false) } return (

Comment replies

navigate('..')} >
{replyEvent && ( )}

Reply Depth: {size} {!isComplete && }

{!isRoot && rootEvent && ( Main Post {!isComplete && } )}
{profile?.displayName || profile?.name || ''}{' '} {hexToNpub(event.pubkey)}
{event.created_at && ( )}
{/*

0

*/} {typeof profile?.lud16 !== 'undefined' && profile.lud16 !== '' && } {event.kind === NDKKind.GenericReply && ( <>

{commentEvents.length}

Replies

)}
{/* Quote-Repost */}
{commentEvents.length > 0 && ( <>

Replies

{commentEvents.map((reply) => ( ))}
)}
) }