import { useEffect, useState } from "react";
import { listen } from "@tauri-apps/api/event";
import { overviewOf, useAgent } from "./lib/useAgent";
import { yen } from "./lib/format";
import { Pill, Pomegranate } from "./components/ui";
import { AgentBanner } from "./components/AgentBanner";
import { Contribution } from "./screens/Contribution";
import { Nodes } from "./screens/Nodes";
import { Jobs, Environments, Squads } from "./screens/V2Screens";
import { BudgetPrice } from "./screens/BudgetPrice";
import { Wallet } from "./screens/Wallet";
import { Settings } from "./screens/Settings";
export const SCREENS = [
"contribution",
"nodes",
"jobs",
"environments",
"squads",
"budget",
"wallet",
"settings",
] as const;
export type Screen = (typeof SCREENS)[number];
const LABEL: Record<Screen, string> = {
contribution: "Contribution",
nodes: "Nodes",
jobs: "Jobs",
environments: "Environments",
squads: "Squads",
budget: "Budget & price",
wallet: "Wallet",
settings: "Settings",
};
export function App() {
const hook = useAgent();
const [screen, setScreen] = useState<Screen>("contribution");
const ov = overviewOf(hook.state);
// The tray's "Settings…" item (and later zakuro:// links) pick a screen.
useEffect(() => {
const un = listen<string>("navigate", (e) => {
if ((SCREENS as readonly string[]).includes(e.payload)) setScreen(e.payload as Screen);
});
return () => {
un.then((f) => f());
};
}, []);
const count = (s: Screen): number | null => {
if (!ov) return null;
if (s === "nodes") return ov.nodesTotal;
return null;
};
return (
<div className="grid h-full grid-cols-[190px_1fr]">
<aside className="flex flex-col gap-0.5 border-r border-hairline bg-surface px-2.5 pb-3 pt-9" data-tauri-drag-region>
<div className="flex items-center gap-2 px-2 pb-3.5 text-[14px] font-semibold" data-tauri-drag-region>
<Pomegranate className="h-4 w-4 text-wine" hollow={ov ? !ov.thisNode.sharing : false} />
Zakuro
</div>
<nav className="grid gap-0.5" aria-label="Sections">
{SCREENS.map((s) => {
const n = count(s);
const on = s === screen;
return (
<button
key={s}
onClick={() => setScreen(s)}
aria-current={on ? "page" : undefined}
className={
"flex items-center justify-between rounded-sm px-2 py-1.5 text-left text-[13px] " +
(on ? "bg-wine/10 font-semibold text-wine-bright" : "text-fg hover:bg-raised")
}
>
{LABEL[s]}
{n !== null && <em className="tnum text-[11px] not-italic text-fg-dim">{n}</em>}
</button>
);
})}
</nav>
<div className="mt-auto grid gap-1.5 border-t border-hairline px-2 pt-2.5 text-[11.5px] text-fg-dim">
<b className="font-semibold text-fg">{ov?.account?.username ?? "Not signed in"}</b>
<span>Balance {yen(ov?.account?.balance)}</span>
{hook.state.status === "ok" ? (
<span className="justify-self-start">
<Pill tone="ok">Agent connected</Pill>
</span>
) : hook.state.status === "loading" ? (
<span className="justify-self-start">
<Pill>Connecting…</Pill>
</span>
) : (
<span className="justify-self-start">
<Pill tone="warn">Agent offline</Pill>
</span>
)}
<span className="font-mono text-[10.5px]">
{ov ? `zc ${ov.agentVersion} · agent ${ov.apiVersion}` : "agent —"}
</span>
</div>
</aside>
<main className="grid min-w-0 content-start gap-3.5 overflow-y-auto px-5 pb-6 pt-9">
<AgentBanner hook={hook} onSettings={() => setScreen("settings")} />
{screen === "contribution" && <Contribution hook={hook} ov={ov} />}
{screen === "nodes" && <Nodes ov={ov} />}
{screen === "jobs" && <Jobs ov={ov} />}
{screen === "environments" && <Environments />}
{screen === "squads" && <Squads />}
{screen === "budget" && <BudgetPrice hook={hook} ov={ov} />}
{screen === "wallet" && <Wallet ov={ov} />}
{screen === "settings" && <Settings hook={hook} ov={ov} />}
</main>
</div>
);
}