From cb3dbdd4dc82db06a80416b391ca96c685a1b436 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Fri, 28 Apr 2023 17:42:27 +0700 Subject: [PATCH] update useProfile hook --- src/utils/hooks/useProfile.tsx | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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,