mirror of
https://github.com/hoornet/vega.git
synced 2026-05-07 04:39:12 -07:00
Add network reaction counts to note cards
- fetchReactionCount (kind 7 #e filter) in nostr lib - useReactionCount hook with module-level cache to avoid refetching - NoteCard shows count next to like button; increments optimistically on like - Falls back to "like"/"liked" text when count is zero or still loading Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
29
src/hooks/useReactionCount.ts
Normal file
29
src/hooks/useReactionCount.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { fetchReactionCount } from "../lib/nostr";
|
||||
|
||||
const cache = new Map<string, number>();
|
||||
|
||||
export function useReactionCount(eventId: string): [number | null, (delta: number) => void] {
|
||||
const [count, setCount] = useState<number | null>(() => cache.get(eventId) ?? null);
|
||||
|
||||
useEffect(() => {
|
||||
if (cache.has(eventId)) {
|
||||
setCount(cache.get(eventId)!);
|
||||
return;
|
||||
}
|
||||
fetchReactionCount(eventId).then((n) => {
|
||||
cache.set(eventId, n);
|
||||
setCount(n);
|
||||
});
|
||||
}, [eventId]);
|
||||
|
||||
const adjust = (delta: number) => {
|
||||
setCount((prev) => {
|
||||
const next = (prev ?? 0) + delta;
|
||||
cache.set(eventId, next);
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
return [count, adjust];
|
||||
}
|
||||
Reference in New Issue
Block a user