diff --git a/src/utils/hooks/useProfile.tsx b/src/utils/hooks/useProfile.tsx index 9512e744..e41feb2e 100644 --- a/src/utils/hooks/useProfile.tsx +++ b/src/utils/hooks/useProfile.tsx @@ -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,