72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { catalogApi, catalogKeys } from "./catalog-api";
|
|
import { inventoryApi, inventoryKeys } from "./inventory-api";
|
|
import { InventoryFeedback } from "./inventory-feedback";
|
|
import { ResourceForm } from "./resource-form";
|
|
export function ResourceEditor({
|
|
companyId,
|
|
csrfToken,
|
|
resourceId,
|
|
initialKind,
|
|
initialAddress = "",
|
|
initialAccess,
|
|
onSaved,
|
|
allowQuickCreate = true,
|
|
}: {
|
|
companyId: string;
|
|
csrfToken: string;
|
|
resourceId?: string | undefined;
|
|
initialKind?: "mailbox" | "access" | "application" | "certificate" | undefined;
|
|
initialAddress?: string;
|
|
initialAccess?:
|
|
| { resourceTypeId: string; systemId: string | null; environmentId: string | null }
|
|
| undefined;
|
|
onSaved: (id: string) => void;
|
|
allowQuickCreate?: boolean;
|
|
}) {
|
|
const { t } = useTranslation("resources");
|
|
const catalog = useQuery({
|
|
queryKey: catalogKeys.scope(companyId),
|
|
queryFn: () => catalogApi.get(companyId),
|
|
});
|
|
const inventory = useQuery({
|
|
queryKey: inventoryKeys.company(companyId),
|
|
queryFn: () => inventoryApi.get(companyId),
|
|
});
|
|
if (catalog.isPending || inventory.isPending) {
|
|
return <p role="status">{t("loading")}</p>;
|
|
}
|
|
if (catalog.error || inventory.error) {
|
|
return <InventoryFeedback error={catalog.error ?? inventory.error} />;
|
|
}
|
|
const resource = inventory.data?.resources.find((item) => item.id === resourceId);
|
|
if (resourceId && !resource) {
|
|
return <p role="alert">{t("inventoryErrors.RESOURCE_NOT_FOUND")}</p>;
|
|
}
|
|
if (!catalog.data) {
|
|
return null;
|
|
}
|
|
const referenceFieldId = catalog.data.items.find(
|
|
(item) => item.builtinKey === "field:reference"
|
|
)?.id;
|
|
const initialIntegration =
|
|
initialAccess && referenceFieldId
|
|
? { kind: "access" as const, referenceFieldId, ...initialAccess }
|
|
: undefined;
|
|
return (
|
|
<ResourceForm
|
|
allowQuickCreate={allowQuickCreate}
|
|
companyId={companyId}
|
|
csrfToken={csrfToken}
|
|
initialAddress={initialAddress}
|
|
initialIntegration={initialIntegration}
|
|
initialKind={initialKind}
|
|
items={catalog.data.items}
|
|
onSaved={onSaved}
|
|
resource={resource}
|
|
/>
|
|
);
|
|
}
|