Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

Show item properties for subassembly item (#849)

Gaurav Bhatt49ef6ad05b20
diff --git a/apps/erp/app/modules/items/ui/Consumables/ConsumableProperties.tsx b/apps/erp/app/modules/items/ui/Consumables/ConsumableProperties.tsx
index 84ada59fe5..96cc394ee8 100644
--- a/apps/erp/app/modules/items/ui/Consumables/ConsumableProperties.tsx
+++ b/apps/erp/app/modules/items/ui/Consumables/ConsumableProperties.tsx
@@ -37,7 +37,19 @@ import type {
} from "../../types";
import { FileBadge } from "../Item";
-const ConsumableProperties = () => {
+type ConsumablePropertiesProps = {
+ data?: {
+ itemId: string;
+ locations: ListItem[];
+ consumableSummary: Consumable;
+ files: Promise<ItemFile[]>;
+ supplierParts: SupplierPart[];
+ pickMethods: PickMethod[];
+ tags: { name: string }[];
+ };
+};
+
+const ConsumableProperties = ({ data }: ConsumablePropertiesProps) => {
const { t } = useLingui();
const translateMethodType = (v: string) =>
v === "Purchase to Order"
@@ -53,21 +65,25 @@ const ConsumableProperties = () => {
: v === "Serial"
? t`Serial`
: t`Batch`;
- const { itemId } = useParams();
+ const params = useParams();
+ const itemId = data?.itemId ?? params.itemId;
if (!itemId) throw new Error("itemId not found");
const sharedConsumablesData = useRouteData<{ locations: ListItem[] }>(
path.to.consumableRoot
);
- const routeData = useRouteData<{
+ // When `data` is injected (subassembly context), this hook won't match a
+ // route and returns undefined — harmless, hooks must be called unconditionally.
+ const routeDataFromRoute = useRouteData<{
consumableSummary: Consumable;
files: Promise<ItemFile[]>;
supplierParts: SupplierPart[];
pickMethods: PickMethod[];
tags: { name: string }[];
}>(path.to.consumable(itemId));
+ const routeData = data ?? routeDataFromRoute;
- const locations = sharedConsumablesData?.locations ?? [];
+ const locations = data?.locations ?? sharedConsumablesData?.locations ?? [];
const supplierParts = routeData?.supplierParts ?? [];
const pickMethods = routeData?.pickMethods ?? [];
diff --git a/apps/erp/app/modules/items/ui/Item/SelectedItemProperties.tsx b/apps/erp/app/modules/items/ui/Item/SelectedItemProperties.tsx
new file mode 100644
index 0000000000..2c902e8102
--- /dev/null
+++ b/apps/erp/app/modules/items/ui/Item/SelectedItemProperties.tsx
@@ -0,0 +1,126 @@
+import { Spinner } from "@carbon/react";
+import type { PostgrestResponse } from "@supabase/supabase-js";
+import { useEffect, useMemo } from "react";
+import { useFetcher, useSearchParams } from "react-router";
+import type { FlatTreeItem } from "~/components/TreeView";
+import { ConsumableProperties } from "~/modules/items/ui/Consumables";
+import { MaterialProperties } from "~/modules/items/ui/Materials";
+import { PartProperties } from "~/modules/items/ui/Parts";
+import { ToolProperties } from "~/modules/items/ui/Tools";
+import type { loader as itemPropertiesLoader } from "~/routes/x+/items+/$itemId.properties";
+import { path } from "~/utils/path";
+import type { MakeMethod, Method } from "../../types";
+
+export function SelectedItemProperties({
+ topLevelItemId,
+ methods
+}: {
+ topLevelItemId: string;
+ methods: FlatTreeItem<Method>[];
+}) {
+ const [searchParams] = useSearchParams();
+ const materialId = searchParams.get("materialId");
+
+ const selectedNode = useMemo(
+ () =>
+ materialId
+ ? methods.find((m) => m.data.methodMaterialId === materialId)
+ : undefined,
+ [materialId, methods]
+ );
+
+ const selectedItemId = selectedNode?.data.itemId ?? null;
+ const selectedItemType = selectedNode?.data.itemType ?? null;
+ const isTopLevel =
+ !selectedNode ||
+ !!selectedNode.data.isRoot ||
+ selectedItemId === topLevelItemId;
+
+ const fetcher = useFetcher<typeof itemPropertiesLoader>();
+
+ // biome-ignore lint/correctness/useExhaustiveDependencies: fetcher identity is stable
+ useEffect(() => {
+ if (isTopLevel || !selectedItemId || !selectedItemType) return;
+ fetcher.load(
+ `${path.to.itemProperties(selectedItemId)}?type=${selectedItemType}`
+ );
+ }, [selectedItemId, selectedItemType, isTopLevel]);
+
+ const filesPromise = useMemo(
+ () => Promise.resolve(fetcher.data?.files ?? []),
+ [fetcher.data]
+ );
+ const makeMethodsPromise = useMemo(
+ () =>
+ Promise.resolve({
+ data:
+ fetcher.data && "makeMethods" in fetcher.data
+ ? fetcher.data.makeMethods
+ : [],
+ error: null
+ } as unknown as PostgrestResponse<MakeMethod>),
+ [fetcher.data]
+ );
+
+ if (isTopLevel) {
+ return <PartProperties key={topLevelItemId} />;
+ }
+
+ const d = fetcher.data;
+ const ready = d && d.itemId === selectedItemId;
+ if (!ready || fetcher.state === "loading") {
+ return (
+ <div className="flex w-96 items-center justify-center bg-card h-full border-l border-border">
+ <Spinner className="h-6 w-6" />
+ </div>
+ );
+ }
+
+ const common = {
+ itemId: d.itemId,
+ locations: d.locations,
+ files: filesPromise,
+ supplierParts: d.supplierParts,
+ pickMethods: d.pickMethods,
+ tags: d.tags
+ };
+
+ switch (d.type) {
+ case "Material":
+ return (
+ <MaterialProperties
+ key={d.itemId}
+ data={{ ...common, materialSummary: d.summary }}
+ />
+ );
+ case "Tool":
+ return (
+ <ToolProperties
+ key={d.itemId}
+ data={{
+ ...common,
+ makeMethods: makeMethodsPromise,
+ toolSummary: d.summary
+ }}
+ />
+ );
+ case "Consumable":
+ return (
+ <ConsumableProperties
+ key={d.itemId}
+ data={{ ...common, consumableSummary: d.summary }}
+ />
+ );
+ default:
+ return (
+ <PartProperties
+ key={d.itemId}
+ data={{
+ ...common,
+ makeMethods: makeMethodsPromise,
+ partSummary: d.summary
+ }}
+ />
+ );
+ }
+}
diff --git a/apps/erp/app/modules/items/ui/Item/index.ts b/apps/erp/app/modules/items/ui/Item/index.ts
index 938fee1415..34bf7671df 100644
--- a/apps/erp/app/modules/items/ui/Item/index.ts
+++ b/apps/erp/app/modules/items/ui/Item/index.ts
@@ -12,6 +12,7 @@ import ItemRiskRegister from "./ItemRiskRegister";
import ItemSalePriceForm from "./ItemSalePriceForm";
import MakeMethodTools from "./MakeMethodTools";
import PickMethodForm from "./PickMethodForm";
+import { SelectedItemProperties } from "./SelectedItemProperties";
import SupplierPartForm from "./SupplierPartForm";
import SupplierParts from "./SupplierParts";
@@ -31,6 +32,7 @@ export {
ItemSalePriceForm,
MakeMethodTools,
PickMethodForm,
+ SelectedItemProperties,
SupplierPartForm,
SupplierParts
};
diff --git a/apps/erp/app/modules/items/ui/Materials/MaterialProperties.tsx b/apps/erp/app/modules/items/ui/Materials/MaterialProperties.tsx
index b92344922b..6f9854abc3 100644
--- a/apps/erp/app/modules/items/ui/Materials/MaterialProperties.tsx
+++ b/apps/erp/app/modules/items/ui/Materials/MaterialProperties.tsx
@@ -57,7 +57,19 @@ import type {
} from "../../types";
import { FileBadge } from "../Item";
-const MaterialProperties = () => {
+type MaterialPropertiesProps = {
+ data?: {
+ itemId: string;
+ locations: ListItem[];
+ materialSummary: MaterialSummary;
+ files: Promise<ItemFile[]>;
+ supplierParts: SupplierPart[];
+ pickMethods: PickMethod[];
+ tags: { name: string }[];
+ };
+};
+
+const MaterialProperties = ({ data }: MaterialPropertiesProps) => {
const { t } = useLingui();
const translateMethodType = (v: string) =>
v === "Purchase to Order"
@@ -73,7 +85,8 @@ const MaterialProperties = () => {
: v === "Serial"
? t`Serial`
: t`Batch`;
- const { itemId } = useParams();
+ const params = useParams();
+ const itemId = data?.itemId ?? params.itemId;
if (!itemId) throw new Error("itemId not found");
const [substanceId, setSubstanceId] = useState<string | undefined>();
@@ -82,15 +95,18 @@ const MaterialProperties = () => {
const sharedMaterialsData = useRouteData<{ locations: ListItem[] }>(
path.to.materialRoot
);
- const routeData = useRouteData<{
+ // When `data` is injected (subassembly context), this hook won't match a
+ // route and returns undefined — harmless, hooks must be called unconditionally.
+ const routeDataFromRoute = useRouteData<{
materialSummary: MaterialSummary;
files: Promise<ItemFile[]>;
supplierParts: SupplierPart[];
pickMethods: PickMethod[];
tags: { name: string }[];
}>(path.to.material(itemId));
+ const routeData = data ?? routeDataFromRoute;
- const locations = sharedMaterialsData?.locations ?? [];
+ const locations = data?.locations ?? sharedMaterialsData?.locations ?? [];
const supplierParts = routeData?.supplierParts ?? [];
const pickMethods = routeData?.pickMethods ?? [];
diff --git a/apps/erp/app/modules/items/ui/Parts/PartProperties.tsx b/apps/erp/app/modules/items/ui/Parts/PartProperties.tsx
index 784276bccc..3cc4d39acd 100644
--- a/apps/erp/app/modules/items/ui/Parts/PartProperties.tsx
+++ b/apps/erp/app/modules/items/ui/Parts/PartProperties.tsx
@@ -53,15 +53,31 @@ import type {
} from "../../types";
import { FileBadge } from "../Item";
-const PartProperties = () => {
+type PartPropertiesProps = {
+ data?: {
+ itemId: string;
+ locations: ListItem[];
+ partSummary: PartSummary;
+ files: Promise<ItemFile[]>;
+ supplierParts: SupplierPart[];
+ pickMethods: PickMethod[];
+ makeMethods: Promise<PostgrestResponse<MakeMethod>>;
+ tags: { name: string }[];
+ };
+};
+
+const PartProperties = ({ data }: PartPropertiesProps) => {
const { t } = useLingui();
- const { itemId } = useParams();
+ const params = useParams();
+ const itemId = data?.itemId ?? params.itemId;
if (!itemId) throw new Error("itemId not found");
const sharedPartsData = useRouteData<{ locations: ListItem[] }>(
path.to.partRoot
);
- const routeData = useRouteData<{
+ // When `data` is injected (subassembly context), this hook won't match a
+ // route and returns undefined — harmless, hooks must be called unconditionally.
+ const routeDataFromRoute = useRouteData<{
partSummary: PartSummary;
files: Promise<ItemFile[]>;
supplierParts: SupplierPart[];
@@ -69,8 +85,9 @@ const PartProperties = () => {
makeMethods: Promise<PostgrestResponse<MakeMethod>>;
tags: { name: string }[];
}>(path.to.part(itemId));
+ const routeData = data ?? routeDataFromRoute;
- const locations = sharedPartsData?.locations ?? [];
+ const locations = data?.locations ?? sharedPartsData?.locations ?? [];
const supplierParts = routeData?.supplierParts ?? [];
const pickMethods = routeData?.pickMethods ?? [];
diff --git a/apps/erp/app/modules/items/ui/Tools/ToolProperties.tsx b/apps/erp/app/modules/items/ui/Tools/ToolProperties.tsx
index d4e3cbf273..0f9d37eac4 100644
--- a/apps/erp/app/modules/items/ui/Tools/ToolProperties.tsx
+++ b/apps/erp/app/modules/items/ui/Tools/ToolProperties.tsx
@@ -42,15 +42,31 @@ import type {
} from "../../types";
import { FileBadge } from "../Item";
-const ToolProperties = () => {
+type ToolPropertiesProps = {
+ data?: {
+ itemId: string;
+ locations: ListItem[];
+ toolSummary: Tool;
+ files: Promise<ItemFile[]>;
+ supplierParts: SupplierPart[];
+ pickMethods: PickMethod[];
+ makeMethods: Promise<PostgrestResponse<MakeMethod>>;
+ tags: { name: string }[];
+ };
+};
+
+const ToolProperties = ({ data }: ToolPropertiesProps) => {
const { t } = useLingui();
- const { itemId } = useParams();
+ const params = useParams();
+ const itemId = data?.itemId ?? params.itemId;
if (!itemId) throw new Error("itemId not found");
const sharedToolsData = useRouteData<{ locations: ListItem[] }>(
path.to.toolRoot
);
- const routeData = useRouteData<{
+ // When `data` is injected (subassembly context), this hook won't match a
+ // route and returns undefined — harmless, hooks must be called unconditionally.
+ const routeDataFromRoute = useRouteData<{
toolSummary: Tool;
files: Promise<ItemFile[]>;
supplierParts: SupplierPart[];
@@ -58,8 +74,9 @@ const ToolProperties = () => {
makeMethods: Promise<PostgrestResponse<MakeMethod>>;
tags: { name: string }[];
}>(path.to.tool(itemId));
+ const routeData = data ?? routeDataFromRoute;
- const locations = sharedToolsData?.locations ?? [];
+ const locations = data?.locations ?? sharedToolsData?.locations ?? [];
const supplierParts = routeData?.supplierParts ?? [];
const pickMethods = routeData?.pickMethods ?? [];
diff --git a/apps/erp/app/routes/x+/items+/$itemId.properties.tsx b/apps/erp/app/routes/x+/items+/$itemId.properties.tsx
new file mode 100644
index 0000000000..ae62c7acad
--- /dev/null
+++ b/apps/erp/app/routes/x+/items+/$itemId.properties.tsx
@@ -0,0 +1,138 @@
+import { requirePermissions } from "@carbon/auth/auth.server";
+import type { LoaderFunctionArgs } from "react-router";
+import type {
+ Consumable,
+ ItemFile,
+ MakeMethod,
+ MaterialSummary,
+ PartSummary,
+ PickMethod,
+ SupplierPart,
+ Tool
+} from "~/modules/items";
+import {
+ getConsumable,
+ getItemFiles,
+ getMakeMethods,
+ getMaterial,
+ getPart,
+ getPickMethods,
+ getSupplierParts,
+ getTool
+} from "~/modules/items";
+import { getLocationsList } from "~/modules/resources";
+import { getTagsList, methodItemType } from "~/modules/shared";
+import type { ListItem } from "~/types";
+
+type CommonFields = {
+ itemId: string;
+ supplierParts: SupplierPart[];
+ pickMethods: PickMethod[];
+ files: ItemFile[];
+ tags: { name: string }[];
+ locations: ListItem[];
+};
+
+export type ItemPropertiesResult = CommonFields &
+ (
+ | { type: "Part"; summary: PartSummary; makeMethods: MakeMethod[] }
+ | { type: "Material"; summary: MaterialSummary }
+ | { type: "Tool"; summary: Tool; makeMethods: MakeMethod[] }
+ | { type: "Consumable"; summary: Consumable }
+ );
+
+const typeConfig = {
+ Part: { tagTable: "part", getSummary: getPart },
+ Material: { tagTable: "material", getSummary: getMaterial },
+ Tool: { tagTable: "tool", getSummary: getTool },
+ Consumable: { tagTable: "consumable", getSummary: getConsumable }
+} as const;
+
+export async function loader({ request, params }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ view: "parts",
+ bypassRls: true
+ });
+
+ const { itemId } = params;
+ if (!itemId) throw new Error("Could not find itemId");
+
+ const url = new URL(request.url);
+ const typeParam = url.searchParams.get("type");
+ const type = (methodItemType as readonly string[]).includes(typeParam ?? "")
+ ? (typeParam as (typeof methodItemType)[number])
+ : "Part";
+
+ const { tagTable, getSummary } = typeConfig[type];
+ const needsMakeMethods = type === "Part" || type === "Tool";
+
+ const [
+ summary,
+ supplierParts,
+ pickMethods,
+ tags,
+ makeMethods,
+ files,
+ locations
+ ] = await Promise.all([
+ getSummary(client, itemId, companyId),
+ getSupplierParts(client, itemId, companyId),
+ getPickMethods(client, itemId, companyId),
+ getTagsList(client, companyId, tagTable),
+ needsMakeMethods ? getMakeMethods(client, itemId, companyId) : null,
+ getItemFiles(client, itemId, companyId),
+ getLocationsList(client, companyId)
+ ]);
+
+ if (!summary.data) {
+ throw new Response("Not Found", { status: 404 });
+ }
+
+ // Guard against cross-tenant access: the detail RPCs run with RLS bypassed
+ // and are not scoped by company, so verify the item belongs to the caller's
+ // company before returning it (mirrors the part route's companyId check).
+ if (summary.data.companyId !== companyId) {
+ throw new Response("Not Found", { status: 404 });
+ }
+
+ const common: CommonFields = {
+ itemId,
+ supplierParts: supplierParts.data ?? [],
+ pickMethods: pickMethods.data ?? [],
+ files,
+ tags: tags.data ?? [],
+ locations: locations.data ?? []
+ };
+
+ // Each branch casts summary.data to the correct type for the discriminated
+ // union. The cast is safe: getSummary is selected from typeConfig[type] so
+ // the runtime type always matches.
+ switch (type) {
+ case "Material":
+ return {
+ ...common,
+ type,
+ summary: summary.data as unknown as MaterialSummary
+ };
+ case "Consumable":
+ return {
+ ...common,
+ type,
+ summary: summary.data as unknown as Consumable
+ };
+ case "Tool":
+ return {
+ ...common,
+ type,
+ summary: summary.data as unknown as Tool,
+ makeMethods: makeMethods?.data ?? []
+ };
+ default:
+ return {
+ ...common,
+ type: "Part" as const,
+ summary: summary.data as unknown as PartSummary,
+ makeMethods: makeMethods?.data ?? []
+ };
+ }
+}
diff --git a/apps/erp/app/routes/x+/part+/$itemId.tsx b/apps/erp/app/routes/x+/part+/$itemId.tsx
index 0fe22dca9a..5e668a13d0 100644
--- a/apps/erp/app/routes/x+/part+/$itemId.tsx
+++ b/apps/erp/app/routes/x+/part+/$itemId.tsx
@@ -38,7 +38,12 @@ import {
getPickMethods,
getSupplierParts
} from "~/modules/items";
-import { BoMActions, BoMExplorer } from "~/modules/items/ui/Item";
+import type { Method } from "~/modules/items/types";
+import {
+ BoMActions,
+ BoMExplorer,
+ SelectedItemProperties
+} from "~/modules/items/ui/Item";
import type { UsedInNode } from "~/modules/items/ui/Item/UsedIn";
import { UsedInSkeleton, UsedInTree } from "~/modules/items/ui/Item/UsedIn";
import { PartHeader, PartProperties } from "~/modules/items/ui/Parts";
@@ -105,7 +110,8 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const tree = await getMethodTree(client, fullMethod.data.id);
if (tree.error) return null;
- const methods = tree.data.length > 0 ? flattenTree(tree.data[0]) : [];
+ const methods =
+ tree.data.length > 0 ? flattenTree<Method>(tree.data[0]) : [];
return {
makeMethod: fullMethod.data,
@@ -206,7 +212,6 @@ export default function PartRoute() {
<BoMExplorer
itemType="Part"
makeMethod={resolved.makeMethod}
- // @ts-ignore
methods={resolved.methods}
methodId={resolved.makeMethod.id}
filterText={filterText}
@@ -507,7 +512,18 @@ export default function PartRoute() {
<Outlet />
</div>
}
- properties={<PartProperties key={itemId} />}
+ properties={
+ <Suspense fallback={<PartProperties key={itemId} />}>
+ <Await resolve={methodTree}>
+ {(resolved) => (
+ <SelectedItemProperties
+ topLevelItemId={itemId}
+ methods={resolved?.methods ?? []}
+ />
+ )}
+ </Await>
+ </Suspense>
+ }
/>
</div>
</div>
diff --git a/apps/erp/app/utils/path.ts b/apps/erp/app/utils/path.ts
index 6554d2d519..670ce14716 100644
--- a/apps/erp/app/utils/path.ts
+++ b/apps/erp/app/utils/path.ts
@@ -1271,6 +1271,7 @@ export const path = {
generatePath(`${x}/sales/no-quote-reasons/${id}`),
notificationSettings: `${x}/account/notifications`,
part: (id: string) => generatePath(`${x}/part/${id}`),
+ itemProperties: (id: string) => generatePath(`${x}/items/${id}/properties`),
partCosting: (id: string) => generatePath(`${x}/part/${id}/costing`),
partDetails: (id: string) => generatePath(`${x}/part/${id}/details`),
partMake: (id: string, makeMethodId: string) =>