mirror of
https://github.com/lumehq/lume.git
synced 2025-03-29 03:02:14 +01:00
update useProfile hook
This commit is contained in:
parent
12e28edad3
commit
cb3dbdd4dc
@ -1,11 +1,29 @@
|
|||||||
import { METADATA_SERVICE } from '@lume/stores/constants';
|
import { METADATA_SERVICE } from '@lume/stores/constants';
|
||||||
|
import { createPleb, getPleb } from '@lume/utils/storage';
|
||||||
|
|
||||||
import useSWR from 'swr';
|
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) => {
|
export const useProfile = (pubkey: string) => {
|
||||||
const { data, error, isLoading } = useSWR(`${METADATA_SERVICE}/${pubkey}/metadata.json`, fetcher);
|
const { data, error, isLoading } = useSWR(pubkey, fetcher);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user: data ? JSON.parse(data.content ? data.content : null) : null,
|
user: data ? JSON.parse(data.content ? data.content : null) : null,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user