import { commands } from "@/commands.gen";
import { cn } from "@/commons";
import { PublishIcon } from "@/components";
import { User } from "@/components/user";
import { LumeWindow } from "@/system";
import { MagnifyingGlass, Plus } from "@phosphor-icons/react";
import { useQuery } from "@tanstack/react-query";
import {
Link,
Outlet,
createLazyFileRoute,
useRouter,
} from "@tanstack/react-router";
import { listen } from "@tauri-apps/api/event";
import { Menu, MenuItem, PredefinedMenuItem } from "@tauri-apps/api/menu";
import { writeText } from "@tauri-apps/plugin-clipboard-manager";
import { message } from "@tauri-apps/plugin-dialog";
import { useCallback, useEffect } from "react";
export const Route = createLazyFileRoute("/_app")({
component: Layout,
});
function Layout() {
const { platform } = Route.useRouteContext();
return (
);
}
function Topbar() {
const { platform, accounts } = Route.useRouteContext();
return (
{accounts?.map((account) => (
))}
);
}
function Account({ pubkey }: { pubkey: string }) {
const navigate = Route.useNavigate();
const context = Route.useRouteContext();
const router = useRouter();
const { data: isActive } = useQuery({
queryKey: ["signer", pubkey],
queryFn: async () => {
const res = await commands.hasSigner(pubkey);
if (res.status === "ok") {
return res.data;
} else {
return false;
}
},
});
const showContextMenu = useCallback(
async (e: React.MouseEvent) => {
e.preventDefault();
const items = await Promise.all([
MenuItem.new({
text: "Unlock",
enabled: !isActive || true,
action: async () => await commands.setSigner(pubkey),
}),
PredefinedMenuItem.new({ item: "Separator" }),
MenuItem.new({
text: "View Profile",
action: () => LumeWindow.openProfile(pubkey),
}),
MenuItem.new({
text: "Update Profile",
action: () =>
LumeWindow.openPopup(
`${pubkey}/set-profile`,
"Update Profile",
true,
),
}),
PredefinedMenuItem.new({ item: "Separator" }),
MenuItem.new({
text: "Copy Public Key",
action: async () => await writeText(pubkey),
}),
MenuItem.new({
text: "Copy Private Key",
action: async () => {
const res = await commands.getPrivateKey(pubkey);
if (res.status === "ok") {
await writeText(res.data);
} else {
await message(res.error, { kind: "error" });
}
},
}),
PredefinedMenuItem.new({ item: "Separator" }),
MenuItem.new({
text: "Settings",
action: () => LumeWindow.openSettings(pubkey),
}),
PredefinedMenuItem.new({ item: "Separator" }),
MenuItem.new({
text: "Logout",
action: async () => {
const res = await commands.deleteAccount(pubkey);
if (res.status === "ok") {
router.invalidate();
// Check remain account
const newAccounts = context.accounts.filter(
(account) => account !== pubkey,
);
// Redirect to new account screen
if (newAccounts.length < 1) {
navigate({ to: "/new", replace: true });
}
}
},
}),
]);
const menu = await Menu.new({ items });
await menu.popup().catch((e) => console.error(e));
},
[pubkey],
);
useEffect(() => {
const unlisten = listen("signer-updated", async () => {
await context.queryClient.invalidateQueries({ queryKey: ["signer"] });
});
return () => {
unlisten.then((f) => f());
};
}, []);
return (
);
}