import { commands } from "@/commands.gen"; import { cn, upload } from "@/commons"; import { Spinner } from "@/components"; import type { Metadata } from "@/types"; import { Plus } from "@phosphor-icons/react"; import * as ScrollArea from "@radix-ui/react-scroll-area"; import { createLazyFileRoute } from "@tanstack/react-router"; import { getCurrentWindow } from "@tauri-apps/api/window"; import { message } from "@tauri-apps/plugin-dialog"; import { type Dispatch, type ReactNode, type SetStateAction, useState, useTransition, } from "react"; import { useForm } from "react-hook-form"; export const Route = createLazyFileRoute("/$id/set-profile")({ component: Screen, }); function Screen() { const { id } = Route.useParams(); const { profile, queryClient } = Route.useRouteContext(); const { register, handleSubmit } = useForm({ defaultValues: profile }); const [picture, setPicture] = useState(""); const [isPending, startTransition] = useTransition(); const onSubmit = (data: Metadata) => { startTransition(async () => { const signer = await commands.hasSigner(id); if (signer.status === "ok") { if (!signer.data) { const res = await commands.setSigner(id); if (res.status === "error") { await message(res.error, { kind: "error" }); return; } } } else { await message(signer.error, { kind: "error" }); return; } const newProfile: Metadata = { ...profile, ...data, picture }; const res = await commands.setProfile(JSON.stringify(newProfile)); if (res.status === "ok") { // Invalidate cache await queryClient.invalidateQueries({ queryKey: ["profile", id], }); // Close current popup await getCurrentWindow().close(); } else { await message(res.error, { title: "Profile", kind: "error" }); return; } }); }; return (
{profile.picture ? ( avatar ) : null}
{profile.display_name}
{profile.nip05?.startsWith("_") ? profile.nip05.replace("_@", "") : profile.nip05}
); } function AvatarUploader({ setPicture, children, className, }: { setPicture: Dispatch>; children: ReactNode; className?: string; }) { const [isPending, startTransition] = useTransition(); const uploadAvatar = () => { startTransition(async () => { try { const image = await upload(); if (image) { setPicture(image); } } catch (e) { await message(String(e)); return; } }); }; return ( ); }