Extract comment system into the nostr-comments skill

Moves the NIP-22 (kind 1111) commenting stack out of src/ and into
.agents/skills/nostr-comments/files/ \u2014 useComments, usePostComment,
and the Comment / CommentForm / CommentsSection components.

Updates the existing SKILL.md with a setup preamble explaining how
to copy the files back into src/ when a project needs comment
functionality. No dependency changes were required; the skill only
uses packages already present in the template.
This commit is contained in:
Alex Gleason committed 2026-04-26 17:57:46 -05:00
1 parent c6be6355a7
commit 29913ef530
6 files changed
+37 -1

No files matched your search

-102
View File
@@ -1,102 +0,0 @@
import { NKinds, NostrEvent, NostrFilter } from '@nostrify/nostrify';
import { useNostr } from '@nostrify/react';
import { useQuery } from '@tanstack/react-query';
export function useComments(root: NostrEvent | URL | `#${string}`, limit?: number) {
const { nostr } = useNostr();
return useQuery({
queryKey: ['nostr', 'comments', root instanceof URL ? root.toString() : typeof root === 'string' ? root : root.id, limit],
queryFn: async () => {
const filter: NostrFilter = { kinds: [1111] };
if (typeof root === 'string') {
filter['#I'] = [root];
} else if (root instanceof URL) {
filter['#I'] = [root.toString()];
} else if (NKinds.addressable(root.kind)) {
const d = root.tags.find(([name]) => name === 'd')?.[1] ?? '';
filter['#A'] = [`${root.kind}:${root.pubkey}:${d}`];
} else if (NKinds.replaceable(root.kind)) {
filter['#A'] = [`${root.kind}:${root.pubkey}:`];
} else {
filter['#E'] = [root.id];
}
if (typeof limit === 'number') {
filter.limit = limit;
}
// Query for all kind 1111 comments that reference this addressable event regardless of depth
const signal = AbortSignal.timeout(5000);
const events = await nostr.query([filter], { signal });
// Helper function to get tag value
const getTagValue = (event: NostrEvent, tagName: string): string | undefined => {
const tag = event.tags.find(([name]) => name === tagName);
return tag?.[1];
};
// Filter top-level comments (those with lowercase tag matching the root)
const topLevelComments = events.filter(comment => {
if (typeof root === 'string') {
return getTagValue(comment, 'i') === root;
} else if (root instanceof URL) {
return getTagValue(comment, 'i') === root.toString();
} else if (NKinds.addressable(root.kind)) {
const d = getTagValue(root, 'd') ?? '';
return getTagValue(comment, 'a') === `${root.kind}:${root.pubkey}:${d}`;
} else if (NKinds.replaceable(root.kind)) {
return getTagValue(comment, 'a') === `${root.kind}:${root.pubkey}:`;
} else {
return getTagValue(comment, 'e') === root.id;
}
});
// Helper function to get all descendants of a comment
const getDescendants = (parentId: string): NostrEvent[] => {
const directReplies = events.filter(comment => {
const eTag = getTagValue(comment, 'e');
return eTag === parentId;
});
const allDescendants = [...directReplies];
// Recursively get descendants of each direct reply
for (const reply of directReplies) {
allDescendants.push(...getDescendants(reply.id));
}
return allDescendants;
};
// Create a map of comment ID to its descendants
const commentDescendants = new Map<string, NostrEvent[]>();
for (const comment of events) {
commentDescendants.set(comment.id, getDescendants(comment.id));
}
// Sort top-level comments by creation time (newest first)
const sortedTopLevel = topLevelComments.sort((a, b) => b.created_at - a.created_at);
return {
allComments: events,
topLevelComments: sortedTopLevel,
getDescendants: (commentId: string) => {
const descendants = commentDescendants.get(commentId) || [];
// Sort descendants by creation time (oldest first for threaded display)
return descendants.sort((a, b) => a.created_at - b.created_at);
},
getDirectReplies: (commentId: string) => {
const directReplies = events.filter(comment => {
const eTag = getTagValue(comment, 'e');
return eTag === commentId;
});
// Sort direct replies by creation time (oldest first for threaded display)
return directReplies.sort((a, b) => a.created_at - b.created_at);
}
};
},
enabled: !!root,
});
}
-93
View File
@@ -1,93 +0,0 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useNostrPublish } from '@/hooks/useNostrPublish';
import { NKinds, type NostrEvent } from '@nostrify/nostrify';
interface PostCommentParams {
root: NostrEvent | URL | `#${string}`; // The root event to comment on
reply?: NostrEvent | URL | `#${string}`; // Optional reply to another comment
content: string;
}
/** Post a NIP-22 (kind 1111) comment on an event. */
export function usePostComment() {
const { mutateAsync: publishEvent } = useNostrPublish();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ root, reply, content }: PostCommentParams) => {
const tags: string[][] = [];
// Root event tags
tags.push(...makeCommentTags('root', root));
// Reply event tags
if (reply) {
tags.push(...makeCommentTags('reply', reply));
} else {
// If this is a top-level comment, use the root event's tags
tags.push(...makeCommentTags('reply', root));
}
const event = await publishEvent({
kind: 1111,
content,
tags,
});
return event;
},
onSuccess: (_, { root }) => {
const rootKey = root instanceof URL ? root.toString() : typeof root === 'string' ? root : root.id;
// Invalidate and refetch comments
queryClient.invalidateQueries({
queryKey: ['nostr', 'comments', rootKey]
});
},
});
}
/** Build NIP-22 comment tags for a given scope and target */
function makeCommentTags(scope: 'root' | 'reply', target: NostrEvent | URL | `#${string}`): string[][] {
const tags: string[][] = [];
const d = (typeof target === 'string' || target instanceof URL)
? ''
: target.tags.find(([name]) => name === 'd')?.[1] ?? '';
if (typeof target === 'string') {
tags.push(['I', target]);
} else if (target instanceof URL) {
tags.push(['I', target.toString()]);
} else if (NKinds.addressable(target.kind)) {
tags.push(['A', `${target.kind}:${target.pubkey}:${d}`]);
} else if (NKinds.replaceable(target.kind)) {
tags.push(['A', `${target.kind}:${target.pubkey}:`]);
} else {
tags.push(['E', target.id]);
}
if (typeof target === 'string') {
tags.push(['K', '#']);
} else if (target instanceof URL) {
switch (target.protocol) {
case 'http:':
case 'https:':
tags.push(['K', 'web']);
break;
default:
tags.push(['K', target.protocol.replace(/:$/, '')]);
break;
}
} else {
tags.push(['K', target.kind.toString()]);
tags.push(['P', target.pubkey]);
}
// Lowercase all tag names for reply scope
if (scope === 'reply') {
return tags.map(([name, ...values]) => [name.toLowerCase(), ...values]);
}
// Root scope: uppercase tags
return tags;
}