import { useEffect, useState } from "react";
import type { Overview } from "../lib/overview";
import type { AgentHook } from "../lib/useAgent";
import { host, type AgentInfo, type AppInfo } from "../lib/agent";
import { ago } from "../lib/format";
import { Button, Panel, Row, RowName, ScreenHead, Toggle } from "../components/ui";
export function Settings({ hook, ov }: { hook: AgentHook; ov: Overview | null }) {
const [agentInfo, setAgentInfo] = useState<AgentInfo | null>(null);
const [app, setApp] = useState<AppInfo | null>(null);
const [msg, setMsg] = useState<string | null>(null);
useEffect(() => {
host.agentInfo().then(setAgentInfo).catch(() => setAgentInfo(null));
host.appInfo().then(setApp).catch(() => setApp(null));
}, [hook.state.status]);
const start = async () => {
setMsg("Running zc agent install…");
try {
setMsg((await host.startAgent()) || "Agent installed.");
await hook.retry();
} catch (e) {
setMsg(String((e as { message?: string }).message ?? e));
}
};
return (
<>
<ScreenHead title="Settings" />
<div className="grid grid-cols-2 gap-3">
<Panel title="Agent" aside="the app's only connection">
<Row cols="1fr auto">
<RowName name="Endpoint" sub={agentInfo?.agentJson} />
<span className="font-mono text-[12px]">{agentInfo?.port ? `127.0.0.1:${agentInfo.port}` : "not installed"}</span>
</Row>
<Row cols="1fr auto">
<RowName name="Agent version" sub={ov ? `summary as of ${ago(ov.asOf)}` : undefined} />
<span className="font-mono text-[12px]">{ov?.agentVersion ?? agentInfo?.version ?? "—"}</span>
</Row>
<Row cols="1fr auto">
<RowName name="Hub" sub={ov ? (ov.hub.reachable ? `reachable · last ok ${ago(ov.hub.lastOk)}` : (ov.hub.error ?? "unreachable")) : undefined} />
<span className="font-mono text-[12px]">{ov?.env ?? "—"}</span>
</Row>
<div className="flex flex-wrap items-center gap-2">
<Button onClick={start}>Install / restart agent</Button>
<Button onClick={() => hook.refresh()} disabled={!ov}>
Re-poll the hub
</Button>
</div>
{msg && <pre className="m-0 max-h-32 overflow-auto whitespace-pre-wrap rounded-sm bg-raised p-2 font-mono text-[11px] text-fg-dim">{msg}</pre>}
</Panel>
<Panel title="This app">
<Row cols="1fr auto">
<RowName name="Open at login" sub="Starts Zakuro in the tray when you log in" />
<Toggle
label="Open at login"
on={!!app?.autostart}
disabled={!app}
onChange={(v) => host.setAutostart(v).then((on) => app && setApp({ ...app, autostart: on }))}
/>
</Row>
<Row cols="1fr auto">
<RowName name="Zakuro Desktop" sub={app?.platform} />
<span className="font-mono text-[12px]">{app?.appVersion ?? "—"}</span>
</Row>
<Row cols="1fr auto">
<RowName name="Bundled zc" sub="sidecar, used for zc agent install" />
<span className="font-mono text-[12px]">{app?.zcVersion ?? "—"}</span>
</Row>
</Panel>
</div>
</>
);
}