Files
shakespeare/src/hooks/useNostrPublish.ts
T
Mary Kate Fain bdb73252eb feat: add emoji reactions to comments on showcase pages
- Create useCommentReactions hook for managing emoji reactions
- Add EmojiReactions component with picker and toggle functionality
- Integrate emoji reactions into Comment component
- Support 8 predefined emoji reactions (👍, ❤️, 😂, 😮, 😢, 😡, 🎉, 🔥)
- Implement optimistic updates with error recovery
- Add real-time synchronization using Nostr kind 7 events
- Include reaction tooltips showing user context
- Support reaction toggling (add/remove) with visual feedback
- Update useNostrPublish to invalidate emoji reaction caches
- Create comprehensive documentation and demo components
- Add loading states and accessibility features

Users can now quickly react to comments with emojis,
providing instant feedback and social engagement without
needing to write full replies. All reactions sync in
real-time across clients using the Nostr protocol.
2025-11-14 10:56:48 -06:00

89 lines
3.1 KiB
TypeScript

import { useNostr } from "@nostrify/react";
import { useMutation, useQueryClient, type UseMutationResult } from "@tanstack/react-query";
import { useCurrentUser } from "./useCurrentUser";
import type { NostrEvent } from "@nostrify/nostrify";
export function useNostrPublish(): UseMutationResult<NostrEvent> {
const { nostr } = useNostr();
const { user } = useCurrentUser();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (t: Omit<NostrEvent, 'id' | 'pubkey' | 'sig'>) => {
if (user) {
const tags = t.tags ?? [];
// Add the client tag if it doesn't exist
if (location.protocol === "https:" && !tags.some(([name]) => name === "client")) {
tags.push(["client", location.hostname]);
}
const event = await user.signer.signEvent({
kind: t.kind,
content: t.content ?? "",
tags,
created_at: t.created_at ?? Math.floor(Date.now() / 1000),
});
await nostr.event(event, { signal: AbortSignal.timeout(5000) });
return event;
} else {
throw new Error("User is not logged in");
}
},
onError: (error) => {
console.error("Failed to publish event:", error);
},
onSuccess: (data) => {
console.log("Event published successfully:", data);
// Invalidate relevant queries based on event kind and tags
const kind = data.kind;
const tags = data.tags || [];
// Comments (kind 1111)
if (kind === 1111) {
// Invalidate all comment queries
queryClient.invalidateQueries({ queryKey: ['nostr', 'comments'] });
}
// Ratings and Emoji Reactions (kind 7)
if (kind === 7) {
// Invalidate rating queries for the target event
const eTag = tags.find(([name]) => name === 'e')?.[1];
if (eTag) {
queryClient.invalidateQueries({ queryKey: ['ratings', eTag] });
// Also invalidate comment reactions if this is a reaction to a comment
queryClient.invalidateQueries({ queryKey: ['comment-reactions', eTag] });
}
}
// App submissions (kind 31733)
if (kind === 31733) {
queryClient.invalidateQueries({ queryKey: ['nostr', 'app-submissions'] });
queryClient.invalidateQueries({ queryKey: ['app-submissions'] });
}
// Moderation lists (kind 30267)
if (kind === 30267) {
const dTag = tags.find(([name]) => name === 'd')?.[1];
if (dTag?.includes('soapbox-')) {
// Invalidate app submissions and showcase queries
queryClient.invalidateQueries({ queryKey: ['nostr', 'app-submissions'] });
queryClient.invalidateQueries({ queryKey: ['app-submissions'] });
queryClient.invalidateQueries({ queryKey: ['showcase-moderation'] });
}
}
// Reports (kind 1984)
if (kind === 1984) {
// Invalidate app submissions and moderation queries
queryClient.invalidateQueries({ queryKey: ['nostr', 'app-submissions'] });
queryClient.invalidateQueries({ queryKey: ['app-submissions'] });
queryClient.invalidateQueries({ queryKey: ['showcase-moderation'] });
}
},
});
}