fix small errors

This commit is contained in:
Ren Amamiya
2023-07-22 17:35:04 +07:00
parent 20a8ce9cba
commit 6d20f84489
9 changed files with 124 additions and 135 deletions

View File

@@ -7,58 +7,46 @@ import { parser } from '@utils/parser';
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) {
if (result.kind === 1) {
result['content'] = parser(result);
}
return result;
const { status, data, error, isFetching } = useQuery(['note', id], async () => {
const result = await getNoteByID(id);
if (result) {
return result;
} else {
if (fallback) {
const embed = JSON.parse(fallback);
await createNote(
embed.id,
embed.pubkey,
embed.kind,
embed.tags,
embed.content,
embed.created_at
);
return embed;
} else {
if (fallback) {
const embed = JSON.parse(fallback);
const event = await ndk.fetchEvent(id);
if (event) {
await createNote(
embed.id,
embed.pubkey,
embed.kind,
embed.tags,
embed.content,
embed.created_at
event.id,
event.pubkey,
event.kind,
event.tags,
event.content,
event.created_at
);
return embed;
} else {
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;
} else {
throw new Error('Event not found');
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;
} else {
throw new Error('Event not found');
}
}
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
staleTime: Infinity,
}
);
});
return { status, data, error, isFetching };
}

View File

@@ -18,6 +18,7 @@ export function useProfile(pubkey: string, fallback?: string) {
const current = Math.floor(Date.now() / 1000);
const cache = await getUserMetadata(pubkey);
if (cache && parseInt(cache.created_at) + 86400 >= current) {
console.log('cache hit:', cache);
return JSON.parse(cache.content);
} else {
const filter: NDKFilter = { kinds: [0], authors: [pubkey] };
@@ -27,7 +28,7 @@ export function useProfile(pubkey: string, fallback?: string) {
await createMetadata(latest.id, latest.pubkey, latest.content);
return JSON.parse(latest.content);
} else {
return null;
throw new Error('User not found');
}
}
} else {
@@ -38,7 +39,6 @@ export function useProfile(pubkey: string, fallback?: string) {
{
refetchOnWindowFocus: false,
refetchOnReconnect: false,
staleTime: Infinity,
}
);