metis-desk/modules/companies/src/web/components/company-inventory.tsx

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2026-10-04 19:04:14 +00:00
import { AssetInventoryPage, WorkstationsPage } from "@metis/assets/client";
import { FormDialog } from "@repo/shadcn-ui/components/form-dialog";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import type { CompanyDTO } from "../../common";
import { PersonForm } from "./person-form";
import { SiteForm } from "./site-form";
export function CompanyInventory({
company,
csrfToken,
2026-10-04 19:04:14 +00:00
workstations = false,
initialId,
initialCreate,
}: {
company: CompanyDTO;
csrfToken: string;
2026-10-04 19:04:14 +00:00
workstations?: boolean;
initialId?: string | undefined;
initialCreate?: boolean | undefined;
}) {
const { t } = useTranslation("companies");
const { t: assetTranslation } = useTranslation("assets");
const [quick, setQuick] = useState<{
kind: "site" | "person";
onCreated: (id: string) => void;
}>();
2026-10-04 19:04:14 +00:00
const Page = workstations ? WorkstationsPage : AssetInventoryPage;
return (
<>
2026-10-04 19:04:14 +00:00
<Page
{...(workstations ? {} : { initialId, initialCreate })}
companyId={company.id}
csrfToken={csrfToken}
lookups={{
sites: company.sites,
people: company.people,
onCreateSite: (onCreated) => setQuick({ kind: "site", onCreated }),
onCreatePerson: (onCreated) => setQuick({ kind: "person", onCreated }),
}}
/>
{quick && (
<FormDialog
cancelLabel={t("cancel")}
formActions
onOpenChange={(open) => {
if (!open) {
setQuick(undefined);
}
}}
open
title={t(quick.kind === "site" ? "newSite" : "newPerson")}
>
<p className="text-sm text-muted-foreground">{assetTranslation("relatedNotice")}</p>
{quick.kind === "site" ? (
<SiteForm
companyId={company.id}
csrfToken={csrfToken}
onSaved={(id) => {
quick.onCreated(id);
setQuick(undefined);
}}
showTitle={false}
/>
) : (
<PersonForm
companyId={company.id}
csrfToken={csrfToken}
onSaved={(id) => {
quick.onCreated(id);
setQuick(undefined);
}}
requireActive
/>
)}
</FormDialog>
)}
</>
);
}