"use client"; // Header sign-in: a small popover that signs in a server-configured principal (name + secret). // Shared changes (promote, publish, approve, retire) need one; workspace-local work does not. import { LogIn, LogOut, UserRound } from "lucide-react"; import { useEffect, useRef, useState } from "react"; import { authLogin, authLogout, authMe, onAuth, type AuthState } from "@/lib/api"; import { Button } from "./ui"; const ROLE: Record = { bid_manager: "Bid manager", approver: "Approver", admin: "Admin" }; export function useAuth(): AuthState | null { const [s, setS] = useState(null); useEffect(() => onAuth(setS), []); return s; } export default function SignIn() { const auth = useAuth(); const [open, setOpen] = useState(false); const [name, setName] = useState(""); const [secret, setSecret] = useState(""); const [busy, setBusy] = useState(false); const [err, setErr] = useState(""); const box = useRef(null); useEffect(() => { authMe().catch(() => {}); }, []); useEffect(() => { if (!open) return; box.current?.querySelector("input, button")?.focus(); const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false); const onDown = (e: MouseEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false); }; window.addEventListener("keydown", onKey); window.addEventListener("mousedown", onDown); return () => { window.removeEventListener("keydown", onKey); window.removeEventListener("mousedown", onDown); }; }, [open]); async function submit(e: React.FormEvent) { e.preventDefault(); setBusy(true); setErr(""); try { await authLogin(name.trim(), secret); setSecret(""); setOpen(false); } catch (x) { setErr(x instanceof Error ? x.message : String(x)); } finally { setBusy(false); } } const p = auth?.principal; return (
{open && (
{p ? (
Signed in as {p.name}
{ROLE[p.role] || p.role}. Approvals and publications are recorded under this name.
) : auth && !auth.enabled ? (

{auth.message || "Sign-in is not configured on this server. Shared publication is disabled; workspace work needs no login."}

) : (

Needed only to promote, publish, approve or retire shared company facts.

setName(e.target.value)} /> setSecret(e.target.value)} /> {err &&

{err}

}
)}
)}
); }