feat: improve column carousel

This commit is contained in:
2024-10-15 09:59:26 +07:00
parent 62bd689031
commit e158f2e4d7
11 changed files with 182 additions and 163 deletions

45
src/system/useProfile.ts Normal file
View File

@@ -0,0 +1,45 @@
import { commands } from "@/commands.gen";
import type { Metadata } from "@/types";
import { useQuery } from "@tanstack/react-query";
import { nip19 } from "nostr-tools";
export function useProfile(pubkey: string, embed?: string) {
const {
isLoading,
isError,
data: profile,
} = useQuery({
queryKey: ["profile", pubkey],
queryFn: async () => {
if (embed) {
const metadata: Metadata = JSON.parse(embed);
return metadata;
}
let normalizeId = pubkey.replace("nostr:", "").replace(/[^\w\s]/gi, "");
if (normalizeId.startsWith("nprofile")) {
const decoded = nip19.decode(normalizeId);
if (decoded.type === "nprofile") {
normalizeId = decoded.data.pubkey;
}
}
const query = await commands.getProfile(normalizeId);
if (query.status === "ok") {
return JSON.parse(query.data) as Metadata;
} else {
throw new Error(query.error);
}
},
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
staleTime: Number.POSITIVE_INFINITY,
retry: 2,
});
return { isLoading, isError, profile };
}