diff --git a/src/components/comment/Comment.tsx b/src/components/comment/Comment.tsx index 44fcdd7..4b6f7f4 100644 --- a/src/components/comment/Comment.tsx +++ b/src/components/comment/Comment.tsx @@ -1,8 +1,19 @@ -import { NDKKind } from '@nostr-dev-kit/ndk' +import { + NDKEvent, + NDKFilter, + NDKKind, + NDKSubscriptionCacheUsage +} from '@nostr-dev-kit/ndk' import { formatDate } from 'date-fns' -import { useDidMount, useNDKContext } from 'hooks' +import { useAppSelector, useDidMount, useNDKContext } from 'hooks' import { useState } from 'react' -import { useParams, useLocation, Link } from 'react-router-dom' +import { + useParams, + useLocation, + Link, + useSubmit, + useNavigation +} from 'react-router-dom' import { getModPageRoute, getBlogPageRoute, @@ -15,6 +26,8 @@ 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 @@ -38,6 +51,16 @@ export const Comment = ({ comment }: CommentProps) => { 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 @@ -52,7 +75,65 @@ export const Comment = ({ comment }: CommentProps) => { })) ) }) + + 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({ @@ -107,25 +188,82 @@ export const Comment = ({ comment }: CommentProps) => {
- {/*
- - - -

0

-
-
-
-
*/} + + {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 !== '' && ( )}