From 5606dcb32fd876e98a11857bf964512a23a528d8 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Mon, 17 Jul 2023 13:37:01 +0700 Subject: [PATCH] update replies --- src/app/note/index.tsx | 15 ++--- src/app/space/components/blocks/thread.tsx | 46 +++++-------- src/libs/storage.tsx | 4 +- src/shared/notes/index.tsx | 2 + src/shared/notes/mentions/user.tsx | 2 +- src/shared/notes/replies/item.tsx | 22 +++++-- src/shared/notes/replies/list.tsx | 20 ++++-- src/shared/notes/stats.tsx | 76 ++++++++++++++++++++++ src/shared/notes/users/thread.tsx | 46 +++++++++++++ src/utils/shortenKey.tsx | 14 ++++ src/utils/transform.tsx | 2 +- 11 files changed, 193 insertions(+), 56 deletions(-) create mode 100644 src/shared/notes/stats.tsx create mode 100644 src/shared/notes/users/thread.tsx diff --git a/src/app/note/index.tsx b/src/app/note/index.tsx index 20016f4e..bd019934 100644 --- a/src/app/note/index.tsx +++ b/src/app/note/index.tsx @@ -1,10 +1,7 @@ -import { useQuery } from '@tanstack/react-query'; import { useParams } from 'react-router-dom'; import { useLiveThread } from '@app/space/hooks/useLiveThread'; -import { getNoteByID } from '@libs/storage'; - import { NoteMetadata } from '@shared/notes/metadata'; import { NoteReplyForm } from '@shared/notes/replies/form'; import { RepliesList } from '@shared/notes/replies/list'; @@ -12,16 +9,12 @@ import { NoteSkeleton } from '@shared/notes/skeleton'; import { User } from '@shared/user'; import { useAccount } from '@utils/hooks/useAccount'; -import { parser } from '@utils/parser'; +import { useEvent } from '@utils/hooks/useEvent'; export function NoteScreen() { const { id } = useParams(); const { account } = useAccount(); - const { status, data } = useQuery(['thread', id], async () => { - const res = await getNoteByID(id); - res['content'] = parser(res); - return res; - }); + const { status, data } = useEvent(id); useLiveThread(id); @@ -39,7 +32,7 @@ export function NoteScreen() {
Share your thought on it...
diff --git a/src/shared/notes/stats.tsx b/src/shared/notes/stats.tsx new file mode 100644 index 00000000..ead08097 --- /dev/null +++ b/src/shared/notes/stats.tsx @@ -0,0 +1,76 @@ +import { NDKEvent, NDKFilter } from '@nostr-dev-kit/ndk'; +import { useQuery } from '@tanstack/react-query'; +import { decode } from 'light-bolt11-decoder'; + +import { useNDK } from '@libs/ndk/provider'; + +import { LoaderIcon } from '@shared/icons'; + +export function NoteStats({ id }: { id: string }) { + const { ndk } = useNDK(); + const { status, data } = useQuery( + ['note-stats', id], + async () => { + let reactions = 0; + let reposts = 0; + let zaps = 0; + + const filter: NDKFilter = { + '#e': [id], + kinds: [6, 7, 9735], + }; + + const events = await ndk.fetchEvents(filter); + events.forEach((event: NDKEvent) => { + switch (event.kind) { + case 6: + reposts += 1; + break; + case 7: + reactions += 1; + break; + case 9735: { + const bolt11 = event.tags.find((tag) => tag[0] === 'bolt11')[1]; + if (bolt11) { + const decoded = decode(bolt11); + const amount = decoded.sections.find((item) => item.name === 'amount'); + const sats = amount.value / 1000; + zaps += sats; + } + break; + } + default: + break; + } + }); + + return { reposts, reactions, zaps }; + }, + { refetchOnWindowFocus: false, refetchOnReconnect: false } + ); + + if (status === 'loading') { + return ( ++ {data.reactions} + reactions +
++ {data.reposts} + reposts +
++ {data.zaps} + zaps +
+