From 616c3cad27d1b6f816178ec170fc64b7fcb863fd Mon Sep 17 00:00:00 2001 From: Jure <44338+hoornet@users.noreply.github.com> Date: Fri, 27 Mar 2026 18:21:36 +0100 Subject: [PATCH] Remove unused useReactionCount, seed reactions from batch engagement - Delete useReactionCount hook (replaced by useReactions) - Remove fetchReactionCount (no longer referenced) - Seed per-note reaction cache from fetchBatchEngagement in trending feed, so emoji pills render instantly without individual fetches --- src/hooks/useReactionCount.ts | 29 ----------------------------- src/lib/nostr/engagement.ts | 6 ------ src/lib/nostr/index.ts | 2 +- src/stores/feed.ts | 8 ++++++++ 4 files changed, 9 insertions(+), 36 deletions(-) delete mode 100644 src/hooks/useReactionCount.ts diff --git a/src/hooks/useReactionCount.ts b/src/hooks/useReactionCount.ts deleted file mode 100644 index 85e110b..0000000 --- a/src/hooks/useReactionCount.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { useEffect, useState } from "react"; -import { fetchReactionCount } from "../lib/nostr"; - -const cache = new Map(); - -export function useReactionCount(eventId: string): [number | null, (delta: number) => void] { - const [count, setCount] = useState(() => 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]; -} diff --git a/src/lib/nostr/engagement.ts b/src/lib/nostr/engagement.ts index be93cca..bddea29 100644 --- a/src/lib/nostr/engagement.ts +++ b/src/lib/nostr/engagement.ts @@ -15,12 +15,6 @@ export async function publishReaction(eventId: string, eventPubkey: string, reac await event.publish(); } -export async function fetchReactionCount(eventId: string): Promise { - const instance = getNDK(); - const filter: NDKFilter = { kinds: [NDKKind.Reaction], "#e": [eventId] }; - const events = await fetchWithTimeout(instance, filter, SINGLE_TIMEOUT); - return events.size; -} export interface GroupedReactions { groups: Map; diff --git a/src/lib/nostr/index.ts b/src/lib/nostr/index.ts index 1062b8d..0d4d067 100644 --- a/src/lib/nostr/index.ts +++ b/src/lib/nostr/index.ts @@ -2,7 +2,7 @@ export { getNDK, getNDKUptimeMs, connectToRelays, ensureConnected, resetNDK, get export { fetchGlobalFeed, fetchFollowFeed, fetchUserNotes, fetchUserNotesNIP65, fetchNoteById, fetchReplies, publishNote, publishReply, publishRepost, publishQuote, fetchHashtagFeed, fetchThreadEvents, fetchAncestors } from "./notes"; export { publishProfile, publishContactList, fetchProfile, fetchFollowSuggestions, fetchMentions, fetchFollowers, fetchNewFollowers } from "./social"; export { publishArticle, fetchArticle, fetchAuthorArticles, fetchArticleFeed, searchArticles, fetchByAddr } from "./articles"; -export { publishReaction, fetchReactionCount, fetchReplyCount, fetchZapCount, fetchReactions, groupReactions, fetchBatchEngagement, fetchZapsReceived, fetchZapsSent } from "./engagement"; +export { publishReaction, fetchReplyCount, fetchZapCount, fetchReactions, groupReactions, fetchBatchEngagement, fetchZapsReceived, fetchZapsSent } from "./engagement"; export type { GroupedReactions, BatchEngagement } from "./engagement"; export { fetchDMConversations, fetchDMThread, sendDM, decryptDM } from "./dms"; export { fetchBookmarkList, publishBookmarkList, fetchBookmarkListFull, publishBookmarkListFull } from "./bookmarks"; diff --git a/src/stores/feed.ts b/src/stores/feed.ts index 749e269..f8cbc0e 100644 --- a/src/stores/feed.ts +++ b/src/stores/feed.ts @@ -1,6 +1,7 @@ import { create } from "zustand"; import { NDKEvent, NDKFilter, NDKKind, NDKSubscription, NDKSubscriptionCacheUsage } from "@nostr-dev-kit/ndk"; import { connectToRelays, ensureConnected, resetNDK, fetchGlobalFeed, fetchBatchEngagement, fetchTrendingCandidates, getNDK } from "../lib/nostr"; +import { seedReactionsCache } from "../hooks/useReactions"; import { useToastStore } from "./toast"; import { dbLoadFeed, dbSaveNotes } from "../lib/db"; import { diagWrapFetch, logDiag, startRelaySnapshots, getRelayStates } from "../lib/feedDiagnostics"; @@ -232,6 +233,13 @@ export const useFeedStore = create((set, get) => ({ const eventIds = notes.map((n) => n.id).filter(Boolean) as string[]; const engagement = await fetchBatchEngagement(eventIds); + // Seed per-note reaction cache so emoji pills render instantly + for (const [id, eng] of engagement) { + if (eng.reactionGroups.size > 0) { + seedReactionsCache(id, eng.reactionGroups, eng.myReactions); + } + } + const now = Math.floor(Date.now() / 1000); const scored = notes .map((note) => {