"use client"; import { PlusIcon, Trash2Icon } from "lucide-react"; import { useState, type FormEvent } from "react"; import { toast } from "sonner"; import { ErrorState, PageHeader } from "@/components/shell/page"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Skeleton } from "@/components/ui/skeleton"; import { Switch } from "@/components/ui/switch"; import { api, ApiError } from "@/lib/api"; import { useAuth } from "@/lib/auth"; import { useI18n } from "@/lib/i18n"; import type { ApprovalRule, Role, TenantSettings, ToolInfo } from "@/lib/types"; import { useApi } from "@/lib/use-api"; const ROLES: Role[] = ["viewer", "agent_operator", "manager", "owner"]; function Section({ title, hint, children }: { title: string; hint?: string; children: React.ReactNode }) { return ( {title} {hint && {hint}} {children} ); } function PolicyForm({ settings, tools, onSaved }: { settings: TenantSettings; tools: ToolInfo[]; onSaved: () => void }) { const { t, m } = useI18n(); const editable = settings.editable; const [attrs, setAttrs] = useState<[string, string][]>(() => Object.entries(settings.policy_attrs).map(([k, v]) => [k, String(v)]), ); const [rules, setRules] = useState(() => settings.approval_rules); const [busy, setBusy] = useState(false); const save = async (e: FormEvent) => { e.preventDefault(); setBusy(true); try { const policy_attrs = Object.fromEntries( attrs .filter(([k]) => k.trim()) .map(([k, v]) => [k.trim(), v === "true" ? true : v === "false" ? false : /^-?\d+$/.test(v) ? Number(v) : v]), ); const approval_rules = rules.map((r) => ({ tool: r.tool, role: r.role, stepup: r.stepup, ...(r.arg ? { arg: r.arg, min: r.min ?? 0 } : {}), })); await api("/settings", { method: "PUT", body: { policy_attrs, approval_rules } }); toast.success(t("settings.saved")); onSaved(); } catch (err) { toast.error(err instanceof ApiError ? err.detail : t("common.error")); } finally { setBusy(false); } }; return ( {attrs.map(([k, v], i) => ( setAttrs(attrs.map((a, j) => (j === i ? [e.target.value, a[1]] : a)))} /> setAttrs(attrs.map((a, j) => (j === i ? [a[0], e.target.value] : a)))} /> {editable && ( setAttrs(attrs.filter((_, j) => j !== i))}> )} ))} {editable && ( setAttrs([...attrs, ["", ""]])}> {t("settings.addAttr")} )} {rules.length > 0 && ( {t("settings.tool")} {t("settings.arg")} {t("settings.min")} {t("settings.role")} {t("settings.stepup")} )} {rules.map((r, i) => { const set = (patch: Partial) => setRules(rules.map((x, j) => (j === i ? { ...x, ...patch } : x))); return ( set({ tool: e.target.value })} aria-label={t("settings.tool")}> {t("common.anyTool")} {tools.map((tl) => ( {tl.name} ))} set({ arg: e.target.value || null })} aria-label={t("settings.arg")} /> set({ min: e.target.value === "" ? null : Number(e.target.value) })} aria-label={t("settings.min")} /> set({ role: e.target.value as Role })} aria-label={t("settings.role")}> {ROLES.map((ro) => ( {m.settings.roles[ro]} ))} set({ stepup: c })} aria-label={t("settings.stepup")} /> {editable ? ( setRules(rules.filter((_, j) => j !== i))}> ) : ( )} ); })} {editable && ( setRules([...rules, { tool: tools[0]?.name ?? "*", arg: null, min: null, role: "manager", stepup: false }])} > {t("settings.addRule")} )} {editable ? ( {t("common.save")} ) : ( {t("settings.ownerOnly")} )} ); } function KillSwitches({ settings, tools, onChanged }: { settings: TenantSettings; tools: ToolInfo[]; onChanged: () => void }) { const { t } = useI18n(); const { me } = useAuth(); const [tool, setTool] = useState(""); const tenantOn = settings.kill_switches.some((k) => k.scope === "tenant" && k.target === me?.tenant.id && k.enabled); const set = async (scope: "tenant" | "tool", enabled: boolean, target?: string) => { try { await api("/admin/killswitch", { method: "POST", body: { scope, target, enabled, reason: "set from dashboard" } }); onChanged(); } catch (err) { toast.error(err instanceof ApiError ? err.detail : t("common.error")); } }; return ( {t("settings.tenantSwitch")} set("tenant", c)} data-testid="tenant-killswitch" /> {settings.kill_switches.filter((k) => k.scope === "tool").length === 0 && ( {t("settings.noSwitches")} )} {settings.kill_switches .filter((k) => k.scope === "tool") .map((k) => ( {k.target} {k.reason} set("tool", c, k.target)} aria-label={k.target} /> ))} {settings.editable && ( setTool(e.target.value)} aria-label={t("settings.toolSwitch")}> — {tools.map((tl) => ( {tl.name} ))} set("tool", true, tool)}> {t("settings.toolSwitch")} )} ); } export function SettingsView() { const { t, m } = useI18n(); const { me } = useAuth(); const s = useApi("/settings"); const tools = useApi("/tools"); return ( {s.error && !s.data ? ( ) : !s.data ? ( ) : ( {s.data.team.map((u) => ( {u.email} {m.settings.roles[u.role]} ))} {t("settings.totp")} {me?.user.totp_enrolled ? t("settings.totpOn") : t("settings.totpOff")} {t("settings.channelsHint")} )} ); }
{hint}
{t("settings.ownerOnly")}
{k.target}
{t("settings.totp")} {me?.user.totp_enrolled ? t("settings.totpOn") : t("settings.totpOff")}
{t("settings.channelsHint")}