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

@ -454,11 +454,9 @@ export async function createMetadata(id: string, pubkey: string, content: string
// get metadata
export async function getUserMetadata(pubkey: string) {
const db = await connect();
const result = await db.select(
`SELECT content, created_at FROM metadata WHERE id = "${pubkey}";`
);
const result = await db.select(`SELECT * FROM metadata WHERE pubkey = "${pubkey}";`);
if (result[0]) {
return JSON.parse(result[0].content);
return result[0];
} else {
return null;
}

View File

@ -6,6 +6,8 @@ import { useNDK } from '@libs/ndk/provider';
import { LoaderIcon } from '@shared/icons';
import { compactNumber } from '@utils/number';
export function NoteStats({ id }: { id: string }) {
const { ndk } = useNDK();
const { status, data } = useQuery(
@ -59,17 +61,25 @@ export function NoteStats({ id }: { id: string }) {
return (
<div className="flex h-11 items-center gap-3">
<p className="inline-flex h-6 items-center justify-center gap-1 rounded bg-zinc-800 px-2 text-sm">
{data.reactions}
<span className="text-zinc-400">reactions</span>
<p className="text-zinc-500">
<span className="font-semibold text-zinc-300">
{compactNumber.format(data.reactions)}
</span>{' '}
reactions
</p>
<p className="inline-flex h-6 items-center justify-center gap-1 rounded bg-zinc-800 px-2 text-sm">
{data.reposts}
<span className="text-zinc-400">reposts</span>
<span className="text-zinc-500">·</span>
<p className="text-zinc-500">
<span className="font-semibold text-zinc-300">
{compactNumber.format(data.reposts)}
</span>{' '}
reposts
</p>
<p className="inline-flex h-6 items-center justify-center gap-1 rounded bg-zinc-800 px-2 text-sm">
{data.zaps}
<span className="text-zinc-400">zaps</span>
<span className="text-zinc-500">·</span>
<p className="text-zinc-500">
<span className="font-semibold text-zinc-300">
{compactNumber.format(data.zaps)}
</span>{' '}
zaps
</p>
</div>
);

View File

@ -35,7 +35,7 @@ export function ThreadUser({ pubkey, time }: { pubkey: string; time: number }) {
<VerticalDotsIcon className="h-4 w-4 rotate-90 transform text-zinc-200" />
</button>
</div>
<div className="inline-flex items-center gap-2">
<div className="mt-1 inline-flex items-center gap-2">
<span className="leading-none text-zinc-500">{createdAt}</span>
<span className="leading-none text-zinc-500">·</span>
<span className="leading-none text-zinc-500">{displayNpub(pubkey, 16)}</span>

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 };
}