diff --git a/bun.lockb b/bun.lockb index d25561ea..0a2a603b 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/shared/notes/kinds/repost.tsx b/src/shared/notes/kinds/repost.tsx index 5f1ae1e3..29f17cc7 100644 --- a/src/shared/notes/kinds/repost.tsx +++ b/src/shared/notes/kinds/repost.tsx @@ -1,8 +1,11 @@ import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk'; +import { useQuery } from '@tanstack/react-query'; import { nip19 } from 'nostr-tools'; import { useCallback } from 'react'; import { Link } from 'react-router-dom'; +import { useNDK } from '@libs/ndk/provider'; + import { ArticleNote, FileNote, @@ -15,28 +18,67 @@ import { } from '@shared/notes'; import { User } from '@shared/user'; -import { useEvent } from '@utils/hooks/useEvent'; +export function Repost({ event }: { event: NDKEvent }) { + const embedEvent: null | NDKEvent = + event.content.length > 0 ? JSON.parse(event.content) : null; -export function Repost({ event, root }: { event: NDKEvent; root?: string }) { - const rootPost = root ?? event.tags.find((el) => el[0] === 'e')?.[1]; - const { status, data } = useEvent(rootPost, null, event.content); + const { ndk } = useNDK(); + const { status, data } = useQuery( + ['repost', event.id], + async () => { + const id = event.tags.find((el) => el[0] === 'e')[1]; + if (id === undefined) throw new Error('wrong id'); - const renderKind = useCallback( - (repostEvent: NDKEvent) => { - switch (repostEvent.kind) { - case NDKKind.Text: - return ; - case NDKKind.Article: - return ; - case 1063: - return ; - default: - return ; - } + const ndkEvent = await ndk.fetchEvent(id); + if (!ndkEvent) throw new Error('Event not found'); + + return ndkEvent; }, - [data] + { + enabled: embedEvent === null, + refetchOnWindowFocus: false, + } ); + const renderKind = useCallback((repostEvent: NDKEvent) => { + switch (repostEvent.kind) { + case NDKKind.Text: + return ; + case NDKKind.Article: + return ; + case 1063: + return ; + default: + return ; + } + }, []); + + if (embedEvent) { + return ( +
+
+
+
+ + +
+
+
+
+ {renderKind(embedEvent)} + +
+
+
+
+
+ ); + } + if (status === 'loading') { return (
diff --git a/src/shared/widgets/other/learnNostr.tsx b/src/shared/widgets/other/learnNostr.tsx index 6d8d5e1c..f31c761a 100644 --- a/src/shared/widgets/other/learnNostr.tsx +++ b/src/shared/widgets/other/learnNostr.tsx @@ -47,9 +47,7 @@ export function LearnNostrWidget({ params }: { params: Widget }) { )}
- + )) ) : ( diff --git a/src/utils/hooks/useEvent.ts b/src/utils/hooks/useEvent.ts index f6dde150..d88102dd 100644 --- a/src/utils/hooks/useEvent.ts +++ b/src/utils/hooks/useEvent.ts @@ -7,12 +7,17 @@ import { useStorage } from '@libs/storage/provider'; import { toRawEvent } from '@utils/rawEvent'; -export function useEvent(id: string, naddr?: AddressPointer, embed?: string) { +export function useEvent( + id: undefined | string, + naddr?: undefined | AddressPointer, + embed?: undefined | string +) { const { db } = useStorage(); const { ndk } = useNDK(); const { status, data } = useQuery( ['event', id], async () => { + // return event refer from naddr if (naddr) { const rEvents = await ndk.fetchEvents({ kinds: [naddr.kind], @@ -20,11 +25,10 @@ export function useEvent(id: string, naddr?: AddressPointer, embed?: string) { authors: [naddr.pubkey], }); const rEvent = [...rEvents].slice(-1)[0]; - return rEvent; } - // return embed event (nostr.band api) or repost + // return embed event (nostr.band api) if (embed) { const event: NDKEvent = JSON.parse(embed); return event; @@ -36,7 +40,7 @@ export function useEvent(id: string, naddr?: AddressPointer, embed?: string) { // get event from relay if event in db not present const event = await ndk.fetchEvent(id); - if (!event) throw new Error(`Event not found: ${id.toString()}`); + if (!event) throw new Error(`Event not found: ${id}`); const rawEvent = toRawEvent(event); await db.createEvent(rawEvent); @@ -45,7 +49,6 @@ export function useEvent(id: string, naddr?: AddressPointer, embed?: string) { }, { enabled: !!ndk, - refetchOnMount: false, refetchOnWindowFocus: false, } );