"use client"; /* The person screen is the issue screen: who they are, how near the six sets they hold, and the * Issue | Hand back | History segments. Each segment draws its own docked bar through setBar; a * finished hand back swaps the whole screen for the Done screen in place. */ import { useCallback, useRef, useState } from "react"; import { useParams, useSearchParams } from "next/navigation"; import { useSnap } from "@/lib/client"; import { approvalRemaining, capCheck, isNursing, staffName } from "@/lib/compute"; import { MBody, MEmpty, MError, MHead, MHeadRow, MMeterPair, MPill, MRule, MSeg, MTop } from "@/components/m"; import { useBasket } from "@/components/MBasket"; import { DoneScreen, type DoneProps } from "@/components/SignFlow"; import HandBackTab from "@/components/person/HandBackTab"; import IssueTab from "@/components/m/issue/IssueTab"; import HistoryTab from "@/components/m/issue/HistoryTab"; import { personMeta, plural } from "@/components/m/issue/meta"; type Tab = "issue" | "back" | "history"; const TABS: { key: Tab; label: string }[] = [ { key: "issue", label: "Issue" }, { key: "back", label: "Hand back" }, { key: "history", label: "History" }, ]; const asTab = (v: string | null): Tab => (v === "back" || v === "history" ? v : "issue"); export default function MPersonPage() { const { s } = useSnap(); const id = String(useParams().id || ""); const sp = useSearchParams(); const basket = useBasket(); const st = s.staff.find((x) => x.id === id); const [tab, setTab] = useState(() => asTab(sp.get("tab"))); const [bar, setBarNode] = useState(null); const [err, setErr] = useState(""); const [done, setDone] = useState(null); const top = useRef(null); const setBar = useCallback((b: React.ReactNode) => setBarNode(b), []); const onError = useCallback((m: string) => setErr(m), []); const onDone = useCallback((d: DoneProps) => { setErr(""); setDone(d); }, []); if (done) return ; if (!st) { return ( <> ); } const pickTab = (k: Tab) => { if (k === tab) return; setErr(""); setTab(k); try { window.history.replaceState(null, "", `/m/person/${encodeURIComponent(id)}${k === "issue" ? "" : `?tab=${k}`}`); } catch { /* not fatal */ } const body = top.current?.parentElement; if (body) body.scrollTop = 0; }; const lines = basket.issue(id); const cap = capCheck(s, st, lines); const left = approvalRemaining(s, st.id); const props = { staffId: st.id, setBar, onDone, onError }; return ( <> setErr("")} /> {bar} ); }