import { commands } from "@/commands.gen"; import { appSettings, displayNpub } from "@/commons"; import { Frame, Spinner, User } from "@/components"; import { ArrowRight, DotsThree, GearSix, Plus } from "@phosphor-icons/react"; import { createLazyFileRoute } from "@tanstack/react-router"; import { Menu, MenuItem } from "@tauri-apps/api/menu"; import { message } from "@tauri-apps/plugin-dialog"; import { useCallback, useEffect, useMemo, useState, useTransition, } from "react"; export const Route = createLazyFileRoute("/")({ component: Screen, }); function Screen() { const context = Route.useRouteContext(); const navigate = Route.useNavigate(); const currentDate = useMemo( () => new Date().toLocaleString("default", { weekday: "long", month: "long", day: "numeric", }), [], ); const [accounts, setAccounts] = useState([]); const [value, setValue] = useState(""); const [autoLogin, setAutoLogin] = useState(false); const [password, setPassword] = useState(""); const [isPending, startTransition] = useTransition(); const showContextMenu = useCallback( async (e: React.MouseEvent, account: string) => { e.stopPropagation(); const menuItems = await Promise.all([ MenuItem.new({ text: "Reset password", enabled: !account.includes("_nostrconnect"), // @ts-ignore, this is tanstack router bug action: () => navigate({ to: "/reset", search: { account } }), }), MenuItem.new({ text: "Delete account", action: async () => await deleteAccount(account), }), ]); const menu = await Menu.new({ items: menuItems, }); await menu.popup().catch((e) => console.error(e)); }, [], ); const deleteAccount = async (account: string) => { const res = await commands.deleteAccount(account); if (res.status === "ok") { setAccounts((prev) => prev.filter((item) => item !== account)); } }; const selectAccount = (account: string) => { setValue(account); if (account.includes("_nostrconnect")) { setAutoLogin(true); } }; const loginWith = () => { startTransition(async () => { const res = await commands.login(value, password); if (res.status === "ok") { const settings = await commands.getUserSettings(); if (settings.status === "ok") { appSettings.setState(() => settings.data); } const status = await commands.isAccountSync(res.data); if (status) { navigate({ to: "/$account/home", // @ts-ignore, this is tanstack router bug params: { account: res.data }, replace: true, }); } else { navigate({ to: "/loading", // @ts-ignore, this is tanstack router bug search: { account: res.data }, replace: true, }); } } else { await message(res.error, { title: "Login", kind: "error" }); return; } }); }; useEffect(() => { if (autoLogin) { loginWith(); } }, [autoLogin, value]); useEffect(() => { setAccounts(context.accounts); }, [context.accounts]); return (