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 // get metadata
export async function getUserMetadata(pubkey: string) { export async function getUserMetadata(pubkey: string) {
const db = await connect(); const db = await connect();
const result = await db.select( const result = await db.select(`SELECT * FROM metadata WHERE pubkey = "${pubkey}";`);
`SELECT content, created_at FROM metadata WHERE id = "${pubkey}";`
);
if (result[0]) { if (result[0]) {
return JSON.parse(result[0].content); return result[0];
} else { } else {
return null; return null;
} }

View File

@@ -6,6 +6,8 @@ import { useNDK } from '@libs/ndk/provider';
import { LoaderIcon } from '@shared/icons'; import { LoaderIcon } from '@shared/icons';
import { compactNumber } from '@utils/number';
export function NoteStats({ id }: { id: string }) { export function NoteStats({ id }: { id: string }) {
const { ndk } = useNDK(); const { ndk } = useNDK();
const { status, data } = useQuery( const { status, data } = useQuery(
@@ -59,17 +61,25 @@ export function NoteStats({ id }: { id: string }) {
return ( return (
<div className="flex h-11 items-center gap-3"> <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"> <p className="text-zinc-500">
{data.reactions} <span className="font-semibold text-zinc-300">
<span className="text-zinc-400">reactions</span> {compactNumber.format(data.reactions)}
</span>{' '}
reactions
</p> </p>
<p className="inline-flex h-6 items-center justify-center gap-1 rounded bg-zinc-800 px-2 text-sm"> <span className="text-zinc-500">·</span>
{data.reposts} <p className="text-zinc-500">
<span className="text-zinc-400">reposts</span> <span className="font-semibold text-zinc-300">
{compactNumber.format(data.reposts)}
</span>{' '}
reposts
</p> </p>
<p className="inline-flex h-6 items-center justify-center gap-1 rounded bg-zinc-800 px-2 text-sm"> <span className="text-zinc-500">·</span>
{data.zaps} <p className="text-zinc-500">
<span className="text-zinc-400">zaps</span> <span className="font-semibold text-zinc-300">
{compactNumber.format(data.zaps)}
</span>{' '}
zaps
</p> </p>
</div> </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" /> <VerticalDotsIcon className="h-4 w-4 rotate-90 transform text-zinc-200" />
</button> </button>
</div> </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">{createdAt}</span>
<span className="leading-none text-zinc-500">·</span> <span className="leading-none text-zinc-500">·</span>
<span className="leading-none text-zinc-500">{displayNpub(pubkey, 16)}</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, data: user,
error, error,
isFetching, isFetching,
} = useQuery(['user', pubkey], async () => { } = useQuery(
if (!fallback) { ['user', pubkey],
const current = Math.floor(Date.now() / 1000); async () => {
const cache = await getUserMetadata(pubkey); if (!fallback) {
if (cache && parseInt(cache.created_at) + 86400 >= current) { const current = Math.floor(Date.now() / 1000);
console.log('cache hit - ', cache); const cache = await getUserMetadata(pubkey);
return cache; if (cache && parseInt(cache.created_at) + 86400 >= current) {
} else { return JSON.parse(cache.content);
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);
} else { } 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 }; return { status, user, error, isFetching };
} }