import { commands } from "@/commands"; import { npub } from "@/commons"; import { Frame } from "@/components/frame"; import { Spinner } from "@/components/spinner"; import { User } from "@/components/user"; import { ArrowRight, DotsThree, GearSix, Plus } from "@phosphor-icons/react"; import { Link, 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 [password, setPassword] = useState(""); const [isPending, startTransition] = useTransition(); 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); }; const loginWith = async () => { startTransition(async () => { if (!value || !password) return; const res = await commands.login(value, password); if (res.status === "ok") { navigate({ to: "/$account/chats/new", params: { account: res.data }, replace: true, }); } else { if (res.error === "404") { navigate({ to: "/$account/relays", params: { account: value }, replace: true, }); } else { await message(res.error, { title: "Login", kind: "error" }); } } }); }; const showContextMenu = useCallback( async (e: React.MouseEvent, account: string) => { e.stopPropagation(); const menuItems = await Promise.all([ 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)); }, [], ); useEffect(() => { setAccounts(context.accounts); }, [context.accounts]); return (