fully support nip05

This commit is contained in:
Ren Amamiya
2023-06-30 16:36:03 +07:00
parent 1ba7f823cb
commit 332dbf608d
17 changed files with 250 additions and 245 deletions

View File

@@ -1,38 +1,28 @@
import { createPleb, getPleb } from "@libs/storage";
import { RelayContext } from "@shared/relayProvider";
import { useQuery } from "@tanstack/react-query";
import { nip19 } from "nostr-tools";
import { useContext } from "react";
export function useProfile(id: string) {
export function useProfile(pubkey: string) {
const ndk = useContext(RelayContext);
const {
status,
data: user,
error,
isFetching,
} = useQuery(["user", id], async () => {
let npub: string;
if (id.substring(0, 4) === "npub") {
npub = id;
} else {
npub = nip19.npubEncode(id);
}
const current = Math.floor(Date.now() / 1000);
const result = await getPleb(npub);
if (result && parseInt(result.created_at) + 86400 >= current) {
return result;
} else {
const user = ndk.getUser({ npub });
} = useQuery(
["user", pubkey],
async () => {
const user = ndk.getUser({ hexpubkey: pubkey });
await user.fetchProfile();
await createPleb(id, user.profile);
return user.profile;
}
});
},
{
staleTime: Infinity,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
},
);
return { status, user, error, isFetching };
}