import { Button } from "@repo/shadcn-ui/components/button"; import { EntityAction, EntityListHeader, EntityRowActions, EntityStatus, EntityTable, } from "@repo/shadcn-ui/components/entity-list"; import { FormDialog } from "@repo/shadcn-ui/components/form-dialog"; import { useQuery } from "@tanstack/react-query"; import type { ReactNode } from "react"; import { useId, useState } from "react"; import { useTranslation } from "react-i18next"; import type { ResourceDTO } from "../common"; import { catalogApi, catalogKeys } from "./catalog-api"; import { inventoryApi, inventoryKeys } from "./inventory-api"; import { ResourceDetail } from "./resource-detail"; import { InventoryFeedback, ResourceForm } from "./resource-form"; export function ResourcesPage({ companyId, csrfToken, relatedContent, initialId, initialCreate = false, }: { companyId: string; csrfToken: string; relatedContent?: (resource: ResourceDTO) => ReactNode; initialId?: string | undefined; initialCreate?: boolean | undefined; }) { const { t, i18n } = useTranslation("resources"); const _searchId = useId(); const [search, setSearch] = useState(""); const [editor, setEditor] = useState(() => initialEditor(initialCreate)); const [viewId, setViewId] = useState(initialId); const query = useQuery({ queryKey: inventoryKeys.company(companyId), queryFn: () => inventoryApi.get(companyId), }); const catalog = useQuery({ queryKey: catalogKeys.scope(companyId), queryFn: () => catalogApi.get(companyId), }); const edited = query.data?.resources.find((item) => item.id === editor); const viewed = query.data?.resources.find((item) => item.id === viewId); const visible = query.data?.resources.filter((item) => `${item.values.name} ${item.specification.templateName}` .toLocaleLowerCase() .includes(search.toLocaleLowerCase()) ) ?? []; return (
{" "} {" "} } description={t("resourceScope")} locale={i18n.language} title={t("inventory")} /> {(query.isPending || catalog.isPending) &&

{t("loading")}

} {(query.isError || catalog.isError) && ( )} item.values.name, hideable: false, render: (item) => ( ), }, { id: "template", label: t("template"), value: (item) => item.specification.templateName, render: (item) => <>{item.specification.templateName}, }, { id: "state", label: t("status"), value: (item) => t(item.values.active ? "active" : "inactive"), render: (item) => ( {t(item.values.active ? "active" : "inactive")} ), }, { id: "actions", label: t("actions"), hideable: false, render: (item) => ( {" "}
setViewId(item.id)}> {t("viewResource")} setEditor(item.id)}> {t("edit")}
{" "}
), }, ]} emptyMessage={t("empty")} items={visible} loading={query.isPending || catalog.isPending} locale={i18n.language} onSearchChange={setSearch} search={search} searchPlaceholder={t("search")} title={t("inventory")} unavailable={(!query.data && query.isError) || (!catalog.data && catalog.isError)} /> {editor && catalog.data && ( { if (!open) { setEditor(undefined); } }} open title={t(edited ? "editResource" : "newResource")} wide > { setEditor(undefined); setViewId(id); }} resource={edited} /> )} {viewed && ( { if (!open) { setViewId(undefined); } }} open title={viewed.values.name} wide > {relatedContent?.(viewed)} )}
); } function initialEditor(create: boolean) { return create ? "new" : undefined; }