update useProfile hook

This commit is contained in:
Ren Amamiya
2023-07-19 17:37:10 +07:00
parent 29d40ed406
commit a80477b40e
4 changed files with 48 additions and 33 deletions

View File

@@ -11,29 +11,36 @@ export function useProfile(pubkey: string, fallback?: string) {
data: user,
error,
isFetching,
} = useQuery(['user', pubkey], async () => {
if (!fallback) {
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 cache;
} else {
const filter: NDKFilter = { kinds: [0], authors: [pubkey] };
const events = await ndk.fetchEvents(filter);
const latest = [...events].slice(-1)[0];
if (latest) {
await createMetadata(pubkey, pubkey, latest.content);
return JSON.parse(latest.content);
} = useQuery(
['user', pubkey],
async () => {
if (!fallback) {
const current = Math.floor(Date.now() / 1000);
const cache = await getUserMetadata(pubkey);
if (cache && parseInt(cache.created_at) + 86400 >= current) {
return JSON.parse(cache.content);
} else {
return null;
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);
} else {
return null;
}
}
} else {
const profile = JSON.parse(fallback);
return profile;
}
} else {
const profile = JSON.parse(fallback);
return profile;
},
{
refetchOnWindowFocus: false,
refetchOnReconnect: false,
staleTime: Infinity,
}
});
);
return { status, user, error, isFetching };
}