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 { MACHINE, ago } from "../lib/format";
import { Button, Notice, Panel, Row, RowName, ScreenHead, Toggle } from "../components/ui";
import { UpdatesPanel } from "./UpdatesPanel";
export function Settings({ hook, ov, onRunSetup, onSignedOut }: { hook: AgentHook; ov: Overview | null; onRunSetup: () => void; onSignedOut: () => void }) {
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]);
// Sign out is two clicks: it stops this machine sharing.
const [confirmOut, setConfirmOut] = useState(false);
const [outError, setOutError] = useState<string | null>(null);
const signOut = async () => {
try {
await host.signOut();
onSignedOut();
} catch (e) {
setConfirmOut(false);
setOutError(String((e as { message?: string }).message ?? e));
}
};
const start = async () => {
setMsg("Restarting the agent…");
try {
setMsg((await host.startAgent()) || "Agent installed.");
await hook.retry();
} catch (e) {
setMsg(String((e as { message?: string }).message ?? e));
}
};
return (
<>
<ScreenHead title="Settings" sub="Your account, this app and the background agent" />
{outError && (
<Notice tone="warn" title="Couldn't sign out" onClose={() => setOutError(null)}>
{outError}
</Notice>
)}
<Panel title="Account">
<Row cols="1fr auto">
<RowName
name={ov?.account ? ov.account.username : ov ? "Not signed in" : "Signed in"}
sub={ov?.account ? `${ov.account.email} · ${ov.env} hub` : ov ? undefined : "Your account shows here once the agent is running"}
/>
<span className="flex items-center gap-2">
{confirmOut && <span className="text-[11.5px] text-fg-dim">This {MACHINE} stops sharing.</span>}
{confirmOut ? (
<>
<Button size="sm" variant="primary" onClick={signOut}>
Sign out
</Button>
<Button size="sm" onClick={() => setConfirmOut(false)}>
Cancel
</Button>
</>
) : (
<Button size="sm" onClick={() => setConfirmOut(true)}>
Sign out…
</Button>
)}
</span>
</Row>
</Panel>
<div className="grid grid-cols-2 gap-3">
<Panel title="Background agent" aside="runs your share, even with this window closed">
<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}>Restart agent</Button>
<Button onClick={() => hook.refresh()} disabled={!ov}>
Refresh from 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 in the background, without opening this window" />
<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="The command-line tool inside this app" />
<span className="font-mono text-[12px]">{app?.zcVersion ?? "—"}</span>
</Row>
<Row cols="1fr auto">
<RowName name="Setup" sub={`Check this ${MACHINE} again and choose what to share`} />
<Button size="sm" onClick={onRunSetup}>
Run again
</Button>
</Row>
</Panel>
<div className="col-span-2">
<UpdatesPanel appVersion={app?.appVersion ?? null} onAgentRestarted={() => hook.retry()} />
</div>
</div>
</>
);
}