import { Button } from "@repo/shadcn-ui/components/button"; import { Card, CardContent } from "@repo/shadcn-ui/components/card"; import { Checkbox } from "@repo/shadcn-ui/components/checkbox"; import { FormDialog } from "@repo/shadcn-ui/components/form-dialog"; import { Input } from "@repo/shadcn-ui/components/input"; import { Label } from "@repo/shadcn-ui/components/label"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@repo/shadcn-ui/components/table"; import { useId, useState } from "react"; import { useTranslation } from "react-i18next"; import type { CompanyDTO, PersonDTO } from "../../common"; import { AssignmentForm } from "./assignment-form"; import { PersonForm } from "./person-form"; function PersonDetails({ company, person, csrfToken, onBack, onEdit, }: { company: CompanyDTO; person: PersonDTO; csrfToken: string; onBack: () => void; onEdit: () => void; }) { const { t, i18n } = useTranslation("companies"); const [editor, setEditor] = useState(); const [historyId, setHistoryId] = useState(); const assignments = company.assignments.filter((item) => item.personId === person.id); const selected = assignments.find((item) => item.id === editor); const history = assignments.find((item) => item.id === historyId); const departmentName = (id: string) => company.departments.find((item) => item.id === id)?.name ?? "—"; return (

{person.name}

{t(person.active ? "activePerson" : "inactivePerson")} {person.isContact ? ` · ${t("isContact")}` : ""}

{t("generalPosition")}

{person.position ?? "—"}

{t("email")}

{person.email ?? "—"}

{t("phone")}

{person.phone ?? "—"}

{t("personHelp")}

{!person.active && assignments.some((item) => item.endDate === null) && (

{t("inactiveMembershipWarning")}

)}

{t("departmentAssignments")}

{assignments.length ? ( {t("department")} {t("position")} {t("startDate")} {t("endDate")} {t("primary")} {t("actions")} {assignments.map((item) => ( {departmentName(item.departmentId)} {company.departments.find((department) => department.id === item.departmentId) ?.active === false && ( {t("inactiveDepartment")} )} {item.position ?? "—"} {item.startDate} {item.endDate ?? t("currentAssignment")} {item.isPrimary ? t("yes") : "—"}
))}
) : (

{t("noAssignments")}

)} {!company.departments.some((item) => item.active) && (

{t("createDepartmentFirst")}

)}
{ if (!open) { setEditor(undefined); } }} open={editor !== undefined} title={t(selected ? "editAssignment" : "addAssignment")} > setEditor(undefined)} personId={person.id} /> { if (!open) { setHistoryId(undefined); } }} open={history !== undefined} title={t("assignmentHistory")} > {history && (

{departmentName(history.departmentId)}

{[...history.history].reverse().map((revision) => (

{new Date(revision.recordedAt).toLocaleString(i18n.language)}

{t("position")}: {revision.position ?? "—"}

{revision.startDate} → {revision.endDate ?? t("currentAssignment")}

{t("primaryDepartment")}: {t(revision.isPrimary ? "yes" : "no")}

))}
)}
); } export function PeopleSection({ company, csrfToken }: { company: CompanyDTO; csrfToken: string }) { const { t } = useTranslation("companies"); const filterId = useId(); const [selection, setSelection] = useState< { mode: "new" | "detail" | "edit"; id?: string } | undefined >(); const [search, setSearch] = useState(""); const [contactsOnly, setContactsOnly] = useState(false); const selected = company.people.find((item) => item.id === selection?.id); const personEditor = ( { if (!open) { setSelection(selected ? { mode: "detail", id: selected.id } : undefined); } }} open={selection?.mode === "new" || selection?.mode === "edit"} title={t(selected ? "editPerson" : "newPerson")} wide > setSelection({ mode: "detail", id })} person={selected} /> ); if (selected) { return ( <> {personEditor} setSelection(undefined)} onEdit={() => setSelection({ mode: "edit", id: selected.id })} person={selected} /> ); } const people = company.people.filter( (person) => (!contactsOnly || person.isContact) && person.name.toLocaleLowerCase().includes(search.trim().toLocaleLowerCase()) ); return (
{personEditor}

{t("people")}

setSearch(event.target.value)} value={search} />
setContactsOnly(checked === true)} />
{people.length ? ( {t("name")} {t("position")} {t("departments")} {t("status")} {t("actions")} {people.map((person) => ( {person.name} {person.isContact && ( {t("isContact")} )} {person.position ?? "—"} {company.assignments .filter((item) => item.personId === person.id && item.endDate === null) .map( (item) => (company.departments.find( (department) => department.id === item.departmentId )?.name ?? "—") + (item.isPrimary ? " ★" : "") ) .join(", ") || "—"} {t(person.active ? "active" : "inactive")} ))}
) : (

{t(search || contactsOnly ? "noResults" : "noPeople")}

)}
); }