This commit is contained in:
Ren Amamiya
2023-05-26 09:28:49 +07:00
parent 225179dd6d
commit 5c7b18bf29
41 changed files with 404 additions and 461 deletions

View File

@@ -1,5 +1,4 @@
import { getActiveAccount } from "@utils/storage";
import useSWR from "swr";
const fetcher = () => getActiveAccount();

View File

@@ -1,33 +1,47 @@
import { METADATA_SERVICE } from "@stores/constants";
import { createPleb, getPleb } from "@utils/storage";
import { nip19 } from "nostr-tools";
import useSWR from "swr";
const fetcher = async (pubkey: string) => {
const result = await getPleb(pubkey);
if (result) {
const metadata = JSON.parse(result["metadata"]);
result["content"] = metadata.content;
result["metadata"] = undefined;
const fetcher = async (key: string) => {
let npub: string;
if (key.substring(0, 4) === "npub") {
npub = key;
} else {
npub = nip19.npubEncode(key);
}
const current = Math.floor(Date.now() / 1000);
const result = await getPleb(npub);
if (result && result.created_at + 86400 < current) {
return result;
} else {
const result = await fetch(`${METADATA_SERVICE}/${pubkey}/metadata.json`);
const resultJSON = await result.json();
const cache = await createPleb(pubkey, resultJSON);
const res = await fetch(`${METADATA_SERVICE}/${key}/metadata.json`);
if (cache) {
return resultJSON;
if (!res.ok) {
return null;
}
const json = await res.json();
const saveToDB = await createPleb(key, json);
if (saveToDB) {
return JSON.parse(json.content);
}
}
};
export function useProfile(pubkey: string) {
const { data, error, isLoading } = useSWR(pubkey, fetcher);
export function useProfile(key: string) {
const { data, error, isLoading } = useSWR(key, fetcher, {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: true,
});
return {
user: data ? JSON.parse(data.content ? data.content : null) : null,
user: data,
isLoading,
isError: error,
};