This commit is contained in:
Ren Amamiya
2023-08-19 15:27:10 +07:00
parent 08e3a66ece
commit bac70b19ec
20 changed files with 117 additions and 142 deletions

View File

@@ -2,29 +2,38 @@ import { NDKEvent } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
import { useStorage } from '@libs/storage/provider';
import { parser } from '@utils/parser';
import { RichContent } from '@utils/types';
export function useEvent(id: string, embed?: string) {
const { db } = useStorage();
const { ndk } = useNDK();
const { status, data, error } = useQuery(
['event', id],
async () => {
let richContent: RichContent;
// return embed event (nostr.band api)
if (embed) {
const event: NDKEvent = JSON.parse(embed);
let richContent: RichContent;
if (event.kind === 1) richContent = parser(event);
return { event: event as NDKEvent, richContent: richContent };
return { event: event, richContent: richContent };
}
const event = (await ndk.fetchEvent(id)) as NDKEvent;
if (!event) throw new Error('event not found');
let richContent: RichContent;
if (event.kind === 1) richContent = parser(event);
// get event from db
const dbEvent = await db.getEventByID(id);
if (dbEvent) {
if (dbEvent.kind === 1) richContent = parser(dbEvent);
return { event: dbEvent, richContent: richContent };
} else {
// get event from relay if event in db not present
const event = await ndk.fetchEvent(id);
if (event.kind === 1) richContent = parser(event);
return { event: event as NDKEvent, richContent: richContent };
return { event: event, richContent: richContent };
}
},
{
staleTime: Infinity,