mirror of
https://github.com/hoornet/vega.git
synced 2026-05-07 12:49:13 -07:00
Bump to v0.5.0 — note sharing, reply counts
This commit is contained in:
29
src/hooks/useReplyCount.ts
Normal file
29
src/hooks/useReplyCount.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { fetchReplyCount } from "../lib/nostr";
|
||||
|
||||
const cache = new Map<string, number>();
|
||||
|
||||
export function useReplyCount(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;
|
||||
}
|
||||
fetchReplyCount(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