metis-desk/modules/companies/src/web/components/department-section.tsx

101 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Button } from "@repo/shadcn-ui/components/button";
2026-10-07 15:27:23 +00:00
import {
EntityAction,
EntityListHeader,
EntityRowActions,
EntityStatus,
EntityTable,
} from "@repo/shadcn-ui/components/entity-list";
2026-10-08 12:14:01 +00:00
import { useState } from "react";
import { useTranslation } from "react-i18next";
2026-10-08 12:14:01 +00:00
import type { CompanyDTO } from "../../common";
2026-10-08 14:43:34 +00:00
import { DepartmentEditorDialog } from "./department-editor-dialog";
export function DepartmentSection({
company,
csrfToken,
}: {
company: CompanyDTO;
csrfToken: string;
}) {
2026-10-07 15:27:23 +00:00
const { t, i18n } = useTranslation("companies");
const [editor, setEditor] = useState<string | undefined>();
const selected = company.departments.find((item) => item.id === editor);
2026-10-07 15:27:23 +00:00
return (
2026-10-07 15:27:23 +00:00
<div className="space-y-5">
<EntityListHeader
actions={<Button onClick={() => setEditor("new")}>{t("newDepartment")}</Button>}
locale={i18n.language}
title={t("departments")}
/>
<EntityTable
columns={[
{
id: "name",
label: t("name"),
value: (item) => item.name,
hideable: false,
render: (item) => <>{item.name}</>,
},
{
id: "state",
label: t("status"),
value: (item) => t(item.active ? "active" : "inactive"),
render: (item) => (
<EntityStatus active={item.active}>
{t(item.active ? "active" : "inactive")}
</EntityStatus>
),
},
{
id: "people",
label: t("people"),
value: (item) =>
company.assignments.filter(
(entry) => entry.departmentId === item.id && entry.endDate === null
).length,
render: (item) => (
<>
{
company.assignments.filter(
(entry) => entry.departmentId === item.id && entry.endDate === null
).length
}
</>
),
},
{
id: "actions",
label: t("actions"),
hideable: false,
render: (item) => (
<EntityRowActions label={t("actions")}>
<EntityAction onClick={() => setEditor(item.id)}>{t("edit")}</EntityAction>
</EntityRowActions>
),
},
]}
emptyMessage={t("noDepartments")}
items={company.departments}
locale={i18n.language}
title={t("departments")}
/>
2026-10-08 14:43:34 +00:00
<DepartmentEditorDialog
companyId={company.id}
csrfToken={csrfToken}
department={selected}
2026-10-07 15:27:23 +00:00
onOpenChange={(open) => {
if (!open) {
setEditor(undefined);
}
}}
2026-10-08 14:43:34 +00:00
onSaved={() => setEditor(undefined)}
2026-10-07 15:27:23 +00:00
open={editor !== undefined}
2026-10-08 14:43:34 +00:00
/>
2026-10-07 15:27:23 +00:00
</div>
);
}