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

82 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-10-08 12:14:01 +00:00
import { Button } from "@repo/shadcn-ui/components/button";
import {
EntityAction,
EntityListHeader,
EntityRowActions,
EntityTable,
} from "@repo/shadcn-ui/components/entity-list";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import type { CompanyDTO } from "../../common";
2026-10-08 14:43:34 +00:00
import { SiteEditorDialog } from "./site-editor-dialog";
2026-10-08 12:14:01 +00:00
export function SiteSection({ company, csrfToken }: { company: CompanyDTO; csrfToken: string }) {
const { t, i18n } = useTranslation("companies");
const [createOpen, setCreateOpen] = useState(false);
const [selectedId, setSelectedId] = useState<string | undefined>();
const selected = company.sites.find((site) => site.id === selectedId);
return (
<div className="space-y-6">
<EntityListHeader
actions={<Button onClick={() => setCreateOpen(true)}>{t("newSite")}</Button>}
locale={i18n.language}
title={t("sites")}
/>
<EntityTable
columns={[
{
id: "name",
label: t("name"),
value: (site) => site.name,
hideable: false,
render: (site) => <span className="font-medium">{site.name}</span>,
},
{
id: "address",
label: t("addressOptional"),
value: (site) => site.address,
render: (site) => (
<span className="whitespace-pre-wrap">{site.address ?? t("noAddress")}</span>
),
},
{
id: "actions",
label: t("actions"),
hideable: false,
render: (site) => (
<EntityRowActions label={t("actions")}>
<EntityAction onClick={() => setSelectedId(site.id)}>{t("edit")}</EntityAction>
</EntityRowActions>
),
},
]}
emptyMessage={t("noSites")}
items={company.sites}
locale={i18n.language}
title={t("sites")}
/>
2026-10-08 14:43:34 +00:00
<SiteEditorDialog
companyId={company.id}
csrfToken={csrfToken}
2026-10-08 12:14:01 +00:00
onOpenChange={setCreateOpen}
2026-10-08 14:43:34 +00:00
onSaved={() => setCreateOpen(false)}
2026-10-08 12:14:01 +00:00
open={createOpen}
2026-10-08 14:43:34 +00:00
/>
<SiteEditorDialog
companyId={company.id}
csrfToken={csrfToken}
2026-10-08 12:14:01 +00:00
onOpenChange={(open) => {
if (!open) {
setSelectedId(undefined);
}
}}
2026-10-08 14:43:34 +00:00
onSaved={() => setSelectedId(undefined)}
2026-10-08 12:14:01 +00:00
open={selected !== undefined}
2026-10-08 14:43:34 +00:00
{...(selected ? { site: selected } : {})}
/>
2026-10-08 12:14:01 +00:00
</div>
);
}