import { useEffect, useState } from "react"; import { get, put } from "../../api"; import { useT } from "../../i18n"; import { toast } from "../../dialogs"; import { Icon } from "../Icon"; interface Cred { host: string; username: string; token: string; protocol?: string; } interface GitSettings { name: string; email: string; credentials: Cred[]; } /** Git identity and credentials for agents, terminal and git client (per user). */ export function GitAccount() { const t = useT(); const [s, setS] = useState(); const [saving, setSaving] = useState(false); useEffect(() => { get("/account/git") .then((v) => setS({ name: v.name ?? "", email: v.email ?? "", credentials: v.credentials ?? [] })) .catch((e) => toast(e.message)); }, []); if (!s) return null; const setCred = (i: number, patch: Partial) => setS({ ...s, credentials: s.credentials.map((c, j) => (j === i ? { ...c, ...patch } : c)) }); const save = async () => { setSaving(true); try { const v = await put("/account/git", s); setS({ name: v.name ?? "", email: v.email ?? "", credentials: v.credentials ?? [] }); toast(t("gitcred.saved"), "success"); } catch (e: any) { toast(e.message); } finally { setSaving(false); } }; return (

{t("gitcred.title")}

{t("gitcred.hint")}
setS({ ...s, name: e.target.value })} placeholder="Jane Doe" autoComplete="name" /> setS({ ...s, email: e.target.value })} placeholder="jane@example.com" autoComplete="email" />

{t("gitcred.credentials")}

{s.credentials.length === 0 &&
{t("gitcred.none")}
} {s.credentials.map((c, i) => (
setCred(i, { host: e.target.value })} aria-label={t("gitcred.host")} /> setCred(i, { username: e.target.value })} aria-label={t("gitcred.user")} autoComplete="off" /> c.token.startsWith("•") && e.target.select()} onChange={(e) => setCred(i, { token: e.target.value })} aria-label={t("gitcred.token")} autoComplete="new-password" />
))}
); }