update useProfile hook

This commit is contained in:
Ren Amamiya 2023-04-28 17:42:27 +07:00
parent 12e28edad3
commit cb3dbdd4dc

View File

@ -1,11 +1,29 @@
import { METADATA_SERVICE } from '@lume/stores/constants';
import { createPleb, getPleb } from '@lume/utils/storage';
import useSWR from 'swr';
const fetcher = (url: string) => fetch(url).then((r: any) => r.json());
const fetcher = async (pubkey: string) => {
const result = await getPleb(pubkey);
if (result) {
const metadata = JSON.parse(result['metadata']);
result['content'] = metadata.content;
delete result['metadata'];
return result;
} else {
const result = await fetch(`${METADATA_SERVICE}/${pubkey}/metadata.json`);
const resultJSON = await result.json();
const cache = await createPleb(pubkey, resultJSON);
if (cache) {
return resultJSON;
}
}
};
export const useProfile = (pubkey: string) => {
const { data, error, isLoading } = useSWR(`${METADATA_SERVICE}/${pubkey}/metadata.json`, fetcher);
const { data, error, isLoading } = useSWR(pubkey, fetcher);
return {
user: data ? JSON.parse(data.content ? data.content : null) : null,