From 48c7a10892108ed938f5bf39ce49518c7cac1213 Mon Sep 17 00:00:00 2001 From: digi-monkey Date: Fri, 19 Jan 2024 14:27:25 +0800 Subject: [PATCH] wip: add reaction count with react-query --- .../PostItems/PostReactions/hook.ts | 66 +++++++++++++++++++ .../PostItems/PostReactions/index.tsx | 14 ++++ 2 files changed, 80 insertions(+) create mode 100644 src/components/PostItems/PostReactions/hook.ts diff --git a/src/components/PostItems/PostReactions/hook.ts b/src/components/PostItems/PostReactions/hook.ts new file mode 100644 index 00000000..5b624756 --- /dev/null +++ b/src/components/PostItems/PostReactions/hook.ts @@ -0,0 +1,66 @@ +import { useQueryMsg } from 'components/TimelineRender/hook/useQueryMsg'; +import { EventId, Filter, WellKnownEventKind } from 'core/nostr/type'; +import { CallWorker } from 'core/worker/caller'; +import { useCallback, useMemo } from 'react'; +import { useQuery } from '@tanstack/react-query'; + +export interface ReactionCountProp { + worker: CallWorker | undefined; + eventId: EventId; +} + +export interface ReactionCount { + repost: number; + zap: number; + comment: number; + bookmark: number; +} + +export function useReactionCount({ worker, eventId }: ReactionCountProp) { + const relayUrls = useMemo( + () => worker?.relays.map(r => r.url) || [], + [worker?.relays], + ); + const filter: Filter = useMemo(() => { + return { + '#e': [eventId], + kinds: [ + WellKnownEventKind.text_note, + WellKnownEventKind.reposts, + WellKnownEventKind.zap_receipt, + WellKnownEventKind.bookmark_list, + ], + }; + }, [eventId]); + + const { queryMsg } = useQueryMsg(); + + const queryFn = useCallback(async () => { + return await queryMsg({ filter, worker }); + }, [filter, relayUrls, worker, queryMsg]); + + const queryKey = ['reaction-count', filter, relayUrls]; + const { data, isFetching } = useQuery({ + queryKey, + queryFn, + refetchOnWindowFocus: false, + refetchOnMount: false, + refetchOnReconnect: false, + staleTime: Infinity, + }); + + const reactCount = useMemo(() => { + if (!data) return undefined; + + const reactCount: ReactionCount = { + repost: data.filter(e => e.kind === WellKnownEventKind.reposts).length, + comment: data.filter(e => e.kind === WellKnownEventKind.reposts).length, + zap: data.filter(e => e.kind === WellKnownEventKind.zap_receipt).length, + bookmark: data.filter(e => e.kind === WellKnownEventKind.bookmark_list) + .length, + }; + return reactCount; + }, [data]); + + return { reactCount, isFetching }; +} diff --git a/src/components/PostItems/PostReactions/index.tsx b/src/components/PostItems/PostReactions/index.tsx index fe97dbf7..4f2a5812 100644 --- a/src/components/PostItems/PostReactions/index.tsx +++ b/src/components/PostItems/PostReactions/index.tsx @@ -18,6 +18,7 @@ import { RootState } from 'store/configureStore'; import { noticePubEventResult } from 'components/PubEventNotice'; import { useRouter } from 'next/router'; import { dexieDb } from 'core/db'; +import { useReactionCount } from './hook'; import Icon from 'components/Icon'; import styles from './index.module.scss'; @@ -136,17 +137,28 @@ const PostReactions: React.FC = ({ setIsBookMarking(false); }; + const { reactCount } = useReactionCount({ + worker, + eventId: ownerEvent.id, + }); + + const displayCount = (count?: number) => ( + <>{count && count > 0 ? {count} : ''} + ); + return (