This commit is contained in:
Ren Amamiya
2023-08-04 08:51:26 +07:00
parent 2e47415160
commit ac50cd1373
16 changed files with 108 additions and 78 deletions

View File

@@ -8,47 +8,56 @@ import { LumeEvent } from '@utils/types';
export function useEvent(id: string, fallback?: string) {
const { ndk } = useNDK();
const { status, data, error, isFetching } = useQuery(['note', id], async () => {
const result = await getNoteByID(id);
if (result) {
return result as LumeEvent;
} else {
if (fallback) {
const embed: LumeEvent = JSON.parse(fallback);
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;
const { status, data, error, isFetching } = useQuery(
['note', id],
async () => {
const result = await getNoteByID(id);
if (result) {
return result as LumeEvent;
} else {
const event = await ndk.fetchEvent(id);
if (event) {
if (fallback) {
const embed: LumeEvent = JSON.parse(fallback);
embed['event_id'] = embed.id;
await createNote(
event.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at
embed.id,
embed.pubkey,
embed.kind,
embed.tags,
embed.content as unknown as string,
embed.created_at
);
event['event_id'] = event.id;
if (event.kind === 1) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
event['content'] = parser(event);
}
return event as unknown as LumeEvent;
return embed;
} else {
throw new Error('Event not found');
const event = await ndk.fetchEvent(id);
if (event) {
await createNote(
event.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at
);
event['event_id'] = event.id;
if (event.kind === 1) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
event['content'] = parser(event);
}
return event as unknown as LumeEvent;
} else {
throw new Error('Event not found');
}
}
}
},
{
staleTime: Infinity,
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
}
});
);
return { status, data, error, isFetching };
}

View File

@@ -1,4 +1,3 @@
import { NDKFilter } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
@@ -15,12 +14,12 @@ export function useProfile(pubkey: string, fallback?: string) {
['user', pubkey],
async () => {
if (!fallback) {
const filter: NDKFilter = { kinds: [0], authors: [pubkey] };
const events = await ndk.fetchEvents(filter);
const latest = [...events].sort((a, b) => b.created_at - a.created_at).pop();
if (latest) {
await createMetadata(latest.id, latest.pubkey, latest.content);
return JSON.parse(latest.content);
const user = await ndk.getUser({ hexpubkey: pubkey });
await user.fetchProfile();
if (user.profile) {
user.profile.display_name = user.profile.displayName;
await createMetadata(user.npub, pubkey, JSON.stringify(user.profile));
return user.profile;
} else {
throw new Error('User not found');
}
@@ -30,6 +29,7 @@ export function useProfile(pubkey: string, fallback?: string) {
}
},
{
staleTime: Infinity,
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,