From 83c0ed42afa764f19cad4c761a0be67393ae3ab6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 26 Apr 2026 20:09:29 -0500 Subject: [PATCH] Simplify nostr-comments skill and add 3-dots actions menu - Drop the outer Card wrapper, title, and counter from CommentsSection; it now renders as a lightweight space-y-6 container matching starpoint. - Drop the Card wrapper and 'Adding to the discussion' label from CommentForm; use a rounded Textarea + right-aligned 'Post' button. - Add a per-comment 3-dots menu with 'Open in Ditto' (opens nevent on ditto.pub) and 'Delete' (NIP-09 kind 5, shown only for own comments, confirmed via AlertDialog, optimistically removes from the cached useComments result via queryClient.setQueriesData). - Remove the obsolete title prop from SKILL.md docs and examples. --- .agents/skills/nostr-comments/SKILL.md | 41 +++-- .../files/components/comments/Comment.tsx | 152 +++++++++++++++++- .../files/components/comments/CommentForm.tsx | 75 ++++----- .../components/comments/CommentsSection.tsx | 108 +++++-------- 4 files changed, 238 insertions(+), 138 deletions(-) diff --git a/.agents/skills/nostr-comments/SKILL.md b/.agents/skills/nostr-comments/SKILL.md index 826792b..9d1fdc3 100644 --- a/.agents/skills/nostr-comments/SKILL.md +++ b/.agents/skills/nostr-comments/SKILL.md @@ -5,7 +5,7 @@ description: Implement Nostr comment systems, add discussion features to posts/a # Adding Nostr Comments Sections -This skill provides a complete commenting system using NIP-22 (kind 1111) comments that can be added to any Nostr event, URL, hashtag, or NIP-73 external content identifier. The `CommentsSection` component provides a full-featured commenting interface with threaded replies, user authentication, and real-time updates. +This skill provides a complete commenting system using NIP-22 (kind 1111) comments that can be added to any Nostr event, URL, hashtag, or NIP-73 external content identifier. The `CommentsSection` component renders a lightweight, inline commenting interface with threaded replies, user authentication, and a per-comment actions menu (Open in Ditto, Delete own comment). **The comment system is not included in the project by default.** When the user wants comment functionality, follow the setup instructions below to install the files. @@ -37,14 +37,18 @@ The comment components depend on pieces that already ship with the template: - `@/components/NoteContent` — renders comment text (used by `Comment.tsx`) - `@/components/auth/LoginArea` — shown to logged-out users (used by `CommentForm.tsx`) +- `@/components/ui/alert-dialog` — confirms deletion (used by `Comment.tsx`) - `@/hooks/useAuthor` — fetches comment author profile (used by `Comment.tsx`) -- `@/hooks/useCurrentUser` — determines whether to show the composer (used by `CommentForm.tsx`) -- `@/hooks/useNostrPublish` — publishes kind 1111 events (used by `usePostComment.ts`) +- `@/hooks/useCurrentUser` — determines whether to show the composer and the Delete action (used by `CommentForm.tsx`, `Comment.tsx`) +- `@/hooks/useNostrPublish` — publishes kind 1111 comments and kind 5 deletion requests (used by `usePostComment.ts`, `Comment.tsx`) +- `@/hooks/useToast` — user feedback on delete success/failure (used by `Comment.tsx`) All of these are standard in the template; no extra work is needed beyond copying the skill files. ## Basic Usage +`CommentsSection` renders without an outer card, title, or header — it's just the composer followed by the threaded comment list. Wrap it in whatever page layout you want: + ```tsx import { CommentsSection } from "@/components/comments/CommentsSection"; @@ -54,13 +58,23 @@ function ArticlePage({ article }: { article: NostrEvent }) { {/* Your article content */}
{/* article content */}
- {/* Comments section */} - + {/* Comments section — add your own heading if needed */} +
+

Comments

+ +
); } ``` +## Per-Comment Actions Menu + +Each comment has a 3-dots menu with: + +- **Open in Ditto** — opens the comment at `https://ditto.pub/` in a new tab so users can view replies, react, or zap via Ditto. +- **Delete** — only shown when the logged-in user is the comment's author. Publishes a NIP-09 (kind 5) deletion request tagging the comment's `id` and `kind`, then optimistically removes the comment from the cached query result via `queryClient.setQueriesData`. Deletion is not guaranteed across the network — the `AlertDialog` confirmation makes this clear to the user. + ## Props and Customization The `CommentsSection` component accepts the following props: @@ -69,16 +83,14 @@ The `CommentsSection` component accepts the following props: - `NostrEvent` — comment on a Nostr event (kind 1 note, long-form article, etc.) - `URL` — comment on an external identifier: web URLs (`new URL("https://...")`) or any NIP-73 identifier except hashtags (e.g. `new URL("isbn:9780765382030")`, `new URL("iso3166:US")`) - `#${string}` — NIP-73 hashtag only (e.g. `"#bitcoin"`); this template string type is exclusively for hashtags and must not be used for other NIP-73 identifiers -- **`title`**: Custom title for the comments section (default: "Comments") - **`emptyStateMessage`**: Message shown when no comments exist (default: "No comments yet") - **`emptyStateSubtitle`**: Subtitle for empty state (default: "Be the first to share your thoughts!") -- **`className`**: Additional CSS classes for styling +- **`className`**: Additional CSS classes applied to the outer `
` (it's a plain `space-y-6` container, no card/border) - **`limit`**: Maximum number of comments to load (default: 500) ```tsx ``` @@ -105,13 +116,11 @@ Pass a hashtag string (`#${string}` format) to attach comments to a topic. The h // Comments for the #bitcoin hashtag // Comments for a community-specific tag ``` @@ -125,7 +134,6 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // ISBN must be without hyphens ``` @@ -135,13 +143,11 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // Podcast feed // Podcast episode ``` @@ -151,7 +157,6 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // ISAN without version part ``` @@ -161,7 +166,6 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // Geohash must be lowercase ``` @@ -171,13 +175,11 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // ISO 3166 codes must be uppercase // Subdivision (state/province) ``` @@ -187,7 +189,6 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // DOI must be lowercase ``` @@ -197,12 +198,10 @@ NIP-73 defines a standard set of external content IDs. All NIP-73 identifiers (e // Bitcoin transaction // Ethereum address ``` diff --git a/.agents/skills/nostr-comments/files/components/comments/Comment.tsx b/.agents/skills/nostr-comments/files/components/comments/Comment.tsx index e1c1cd5..e776ce2 100644 --- a/.agents/skills/nostr-comments/files/components/comments/Comment.tsx +++ b/.agents/skills/nostr-comments/files/components/comments/Comment.tsx @@ -4,14 +4,41 @@ import { NostrEvent } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; import { useAuthor } from '@/hooks/useAuthor'; import { useComments } from '@/hooks/useComments'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; +import { useNostrPublish } from '@/hooks/useNostrPublish'; +import { useToast } from '@/hooks/useToast'; +import { useQueryClient } from '@tanstack/react-query'; import { CommentForm } from './CommentForm'; import { NoteContent } from '@/components/NoteContent'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'; -import { DropdownMenu, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; -import { MessageSquare, ChevronDown, ChevronRight, MoreHorizontal } from 'lucide-react'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from '@/components/ui/alert-dialog'; +import { + MessageSquare, + ChevronDown, + ChevronRight, + MoreHorizontal, + ExternalLink, + Trash2, +} from 'lucide-react'; import { formatDistanceToNow } from 'date-fns'; import { genUserName } from '@/lib/genUserName'; @@ -26,18 +53,73 @@ interface CommentProps { export function Comment({ root, comment, depth = 0, maxDepth = 3, limit }: CommentProps) { const [showReplyForm, setShowReplyForm] = useState(false); const [showReplies, setShowReplies] = useState(depth < 2); // Auto-expand first 2 levels - + const [showDeleteDialog, setShowDeleteDialog] = useState(false); + const author = useAuthor(comment.pubkey); const { data: commentsData } = useComments(root, limit); - + const { user } = useCurrentUser(); + const { mutate: publishEvent, isPending: isDeleting } = useNostrPublish(); + const { toast } = useToast(); + const queryClient = useQueryClient(); + const metadata = author.data?.metadata; - const displayName = metadata?.name ?? genUserName(comment.pubkey) + const displayName = metadata?.name ?? genUserName(comment.pubkey); const timeAgo = formatDistanceToNow(new Date(comment.created_at * 1000), { addSuffix: true }); + const nevent = nip19.neventEncode({ id: comment.id, author: comment.pubkey, kind: comment.kind }); + const isOwnComment = user?.pubkey === comment.pubkey; // Get direct replies to this comment const replies = commentsData?.getDirectReplies(comment.id) || []; const hasReplies = replies.length > 0; + const handleDelete = () => { + publishEvent( + { + kind: 5, + content: '', + tags: [ + ['e', comment.id], + ['k', comment.kind.toString()], + ], + }, + { + onSuccess: () => { + toast({ title: 'Comment deleted' }); + setShowDeleteDialog(false); + // Optimistically remove the deleted comment from every cached useComments + // result. Relays may honor the kind 5 deletion request on refetch, but + // that's not guaranteed — this keeps the UI consistent immediately. + queryClient.setQueriesData['data']>( + { queryKey: ['nostr', 'comments'] }, + (oldData) => { + if (!oldData) return oldData; + const allComments = oldData.allComments.filter((c) => c.id !== comment.id); + const topLevelComments = oldData.topLevelComments.filter( + (c) => c.id !== comment.id, + ); + return { + ...oldData, + allComments, + topLevelComments, + getDescendants: (id: string) => + oldData.getDescendants(id).filter((c) => c.id !== comment.id), + getDirectReplies: (id: string) => + oldData.getDirectReplies(id).filter((c) => c.id !== comment.id), + }; + }, + ); + }, + onError: (error) => { + toast({ + title: 'Failed to delete comment', + description: error.message, + variant: 'destructive', + }); + }, + }, + ); + }; + return (
0 ? 'ml-6 border-l-2 border-muted pl-4' : ''}`}> @@ -55,7 +137,7 @@ export function Comment({ root, comment, depth = 0, maxDepth = 3, limit }: Comme
- @@ -83,7 +165,7 @@ export function Comment({ root, comment, depth = 0, maxDepth = 3, limit }: Comme Reply - + {hasReplies && ( @@ -112,6 +194,33 @@ export function Comment({ root, comment, depth = 0, maxDepth = 3, limit }: Comme + + + + + Open in Ditto + + + {isOwnComment && ( + <> + + { + e.preventDefault(); + setShowDeleteDialog(true); + }} + className="text-destructive focus:text-destructive" + > + + Delete + + + )} +
@@ -148,6 +257,33 @@ export function Comment({ root, comment, depth = 0, maxDepth = 3, limit }: Comme )} + + {/* Delete confirmation */} + + + + Delete comment? + + This publishes a NIP-09 deletion request (kind 5). Relays and clients that + honor deletion requests will hide this comment, but deletion is not + guaranteed across the network. + + + + Cancel + { + e.preventDefault(); + handleDelete(); + }} + disabled={isDeleting} + className="bg-destructive text-destructive-foreground hover:bg-destructive/90" + > + {isDeleting ? 'Deleting…' : 'Delete'} + + + +
); -} \ No newline at end of file +} diff --git a/.agents/skills/nostr-comments/files/components/comments/CommentForm.tsx b/.agents/skills/nostr-comments/files/components/comments/CommentForm.tsx index 3fc3002..a73104e 100644 --- a/.agents/skills/nostr-comments/files/components/comments/CommentForm.tsx +++ b/.agents/skills/nostr-comments/files/components/comments/CommentForm.tsx @@ -1,12 +1,12 @@ import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; -import { Card, CardContent } from '@/components/ui/card'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { usePostComment } from '@/hooks/usePostComment'; import { LoginArea } from '@/components/auth/LoginArea'; import { NostrEvent } from '@nostrify/nostrify'; -import { MessageSquare, Send } from 'lucide-react'; +import { MessageSquare } from 'lucide-react'; +import { cn } from '@/lib/utils'; interface CommentFormProps { root: NostrEvent | URL | `#${string}`; @@ -19,9 +19,9 @@ interface CommentFormProps { export function CommentForm({ root, reply, - onSuccess, + onSuccess, placeholder = "Write a comment...", - compact = false + compact = false, }: CommentFormProps) { const [content, setContent] = useState(''); const { user } = useCurrentUser(); @@ -29,7 +29,7 @@ export function CommentForm({ const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); - + if (!content.trim() || !user) return; postComment( @@ -45,46 +45,37 @@ export function CommentForm({ if (!user) { return ( - - -
-
- - Sign in to {reply ? 'reply' : 'comment'} -
- +
+
+
+ + Sign in to {reply ? 'reply' : 'comment'}
- - + +
+
); } return ( - - -
-