update useNostr hook
This commit is contained in:
@@ -1,51 +1,24 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useNDK } from '@libs/ndk/provider';
|
||||
import { createNote, getNoteByID } from '@libs/storage';
|
||||
|
||||
import { parser } from '@utils/parser';
|
||||
import { LumeEvent } from '@utils/types';
|
||||
|
||||
export function useEvent(id: string, fallback?: string) {
|
||||
export function useEvent(id: string, embed?: string) {
|
||||
const { ndk } = useNDK();
|
||||
const { status, data, error, isFetching } = useQuery(
|
||||
['note', id],
|
||||
async () => {
|
||||
const result = await getNoteByID(id);
|
||||
if (result) {
|
||||
return result as LumeEvent;
|
||||
if (embed) {
|
||||
const event: LumeEvent = JSON.parse(embed);
|
||||
if (event.kind === 1) embed['content'] = parser(event);
|
||||
return embed;
|
||||
} else {
|
||||
if (fallback) {
|
||||
const embed: LumeEvent = JSON.parse(fallback);
|
||||
if (embed.kind === 1) embed['content'] = parser(embed);
|
||||
embed['event_id'] = embed.id;
|
||||
await createNote(
|
||||
embed.id,
|
||||
embed.pubkey,
|
||||
embed.kind,
|
||||
embed.tags,
|
||||
embed.content as unknown as string,
|
||||
embed.created_at
|
||||
);
|
||||
return embed;
|
||||
} else {
|
||||
const event = (await ndk.fetchEvent(id)) as unknown as LumeEvent;
|
||||
if (event) {
|
||||
await createNote(
|
||||
event.id,
|
||||
event.pubkey,
|
||||
event.kind,
|
||||
event.tags,
|
||||
event.content as unknown as string,
|
||||
event.created_at
|
||||
);
|
||||
event['event_id'] = event.id;
|
||||
if (event.kind === 1) event['content'] = parser(event);
|
||||
return event as unknown as LumeEvent;
|
||||
} else {
|
||||
throw new Error('Event not found');
|
||||
}
|
||||
}
|
||||
const event = (await ndk.fetchEvent(id)) as unknown as LumeEvent;
|
||||
if (!event) throw new Error('Event not found');
|
||||
if (event.kind === 1) event['content'] = parser(event);
|
||||
return event as unknown as LumeEvent;
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user