import { EntityAction, EntityListHeader, EntityRowActions, EntityTable, } from "@repo/rdx-ui/components/entity-list"; import { ChevronLeftIcon } from "@repo/rdx-ui/lib/icons"; import { Button } from "@repo/shadcn-ui/components/button"; import { PersonAvatar } from "../../shared"; import { Card, CardContent } from "@repo/shadcn-ui/components/card"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import type { CompanyDTO, PersonDTO } from "../../../../common"; import { AssignmentEditorDialog, AssignmentHistoryDialog } from "../../../assignments"; import { deleteAssignment } from "../../../assignments/shared/api"; import { deletePerson } from "../../shared/api"; export 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 [deleting, setDeleting] = useState(false); const [deletingAssignment, setDeletingAssignment] = 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")}

)} setEditor("new")}> {t("addAssignment")} } locale={i18n.language} title={t("departmentAssignments")} /> departmentName(item.departmentId), hideable: false, render: (item) => (

{departmentName(item.departmentId)} {company.departments.find((entry) => entry.id === item.departmentId)?.active === false && {t("inactiveDepartment")}}

), }, { id: "position", label: t("position"), value: (item) => item.position, render: (item) => <>{item.position ?? "—"}, }, { id: "start", label: t("startDate"), value: (item) => item.startDate, render: (item) => <>{item.startDate}, }, { id: "end", label: t("endDateColumn"), value: (item) => item.endDate, render: (item) => <>{item.endDate ?? "—"}, }, { id: "primary", label: t("primary"), value: (item) => item.isPrimary, render: (item) => <>{item.isPrimary ? t("yes") : "—"}, }, { id: "actions", label: t("actions"), hideable: false, render: (item) => ( setEditor(item.id)}>{t("edit")} setHistoryId(item.id)}> {t("history")} { if ( !window.confirm( t("deleteAssignmentConfirm", { name: departmentName(item.departmentId), }) ) ) { return; } setDeletingAssignment(item.id); try { await deleteAssignment(company.id, person.id, item.id, csrfToken); window.location.reload(); } catch { window.alert(t("deleteAssignmentError")); } finally { setDeletingAssignment(undefined); } }} > {t( deletingAssignment === item.id ? "deletingAssignment" : "deleteAssignment" )} ), }, ]} emptyMessage={t("noAssignments")} items={assignments} locale={i18n.language} title={t("departmentAssignments")} /> {!company.departments.some((item) => item.active) && (

{t("createDepartmentFirst")}

)}
{ if (!open) { setEditor(undefined); } }} onSaved={() => setEditor(undefined)} personId={person.id} selected={selected} /> { if (!open) { setHistoryId(undefined); } }} />
); }