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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix: duplicate ids in parts tree [CAR-13]
Brad Barbin5baa120321c6
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.tsindex d555f63f81..d90c02cc1a 100644--- a/apps/carbon/app/modules/items/items.service.ts+++ b/apps/carbon/app/modules/items/items.service.ts@@ -528,13 +528,27 @@ type MethodTreeItem = {children: MethodTreeItem[];};+function removeDuplicateIdsFromTree(tree: MethodTreeItem[]): MethodTreeItem[] {+ // traverse the tree and assign random ids to the nodes+ // to solve the issue of duplicate ids++ function traverse(node: MethodTreeItem) {+ const newNode = JSON.parse(JSON.stringify(node));+ newNode.id = `node-${Math.random().toString(16).slice(2)}`;+ newNode.children = node.children.map((n) => traverse(n));+ return newNode;+ }+ return tree.map((n) => traverse(n));+}+export async function getMethodTree(client: SupabaseClient<Database>,makeMethodId: string) {const items = await getMethodTreeArray(client, makeMethodId);if (items.error) return items;- const tree = getMethodTreeArrayToTree(items.data);+ const tree = removeDuplicateIdsFromTree(getMethodTreeArrayToTree(items.data));+return {data: tree,error: null,diff --git a/apps/carbon/app/modules/items/ui/Item/BoMExplorer.tsx b/apps/carbon/app/modules/items/ui/Item/BoMExplorer.tsxindex 5c9b509795..a5d97c22ef 100644--- a/apps/carbon/app/modules/items/ui/Item/BoMExplorer.tsx+++ b/apps/carbon/app/modules/items/ui/Item/BoMExplorer.tsx@@ -15,7 +15,7 @@ import type { FlatTreeItem } from "~/components/TreeView/TreeView";import { TreeView, useTree } from "~/components/TreeView/TreeView";import { path } from "~/utils/path";import type { Method, MethodItemType } from "../../types";-import { MethodIcon } from "./MethodIcon";+import { MethodIcon, MethodItemTypeIcon } from "./MethodIcon";type BoMExplorerProps = {itemType: MethodItemType;@@ -24,6 +24,7 @@ type BoMExplorerProps = {};const BoMExplorer = ({ itemType, methods, selectedId }: BoMExplorerProps) => {+ console.log({ methods });const [filterText, setFilterText] = useState("");const parentRef = useRef<HTMLDivElement>(null);@@ -99,94 +100,93 @@ const BoMExplorer = ({ itemType, methods, selectedId }: BoMExplorerProps) => {getNodeProps={getNodeProps}getTreeProps={getTreeProps}renderNode={({ node, state }) => (- <>- <div- className={cn(- "flex h-8 cursor-pointer items-center overflow-hidden rounded-sm pr-2",- state.selected- ? "bg-muted hover:bg-muted/90"- : "bg-transparent hover:bg-muted/90"- )}- onClick={() => {- selectNode(node.id);- if (node.data.isRoot) {- navigate(getRootLink(itemType, itemId));- } else {- navigate(- getMaterialLink(- itemType,- itemId,- node.data.materialMakeMethodId ??- node.data.methodType.toLowerCase(),- node.data.methodMaterialId- )- );- }- }}- >- <div className="flex h-8 items-center">- {Array.from({ length: node.level }).map((_, index) => (- <TaskLine key={index} isSelected={state.selected} />- ))}- <div- className={cn(- "flex h-8 w-4 items-center",- node.hasChildren && "hover:bg-accent"- )}- onClick={(e) => {- e.stopPropagation();- if (e.altKey) {- if (state.expanded) {- collapseAllBelowDepth(node.level);- } else {- expandAllBelowDepth(node.level);- }+ <div+ key={node.id}+ className={cn(+ "flex h-8 cursor-pointer items-center overflow-hidden rounded-sm pr-2",+ state.selected+ ? "bg-muted hover:bg-muted/90"+ : "bg-transparent hover:bg-muted/90"+ )}+ onClick={() => {+ selectNode(node.id);+ if (node.data.isRoot) {+ navigate(getRootLink(itemType, itemId));+ } else {+ navigate(+ getMaterialLink(+ itemType,+ itemId,+ node.data.materialMakeMethodId ??+ node.data.methodType.toLowerCase(),+ node.data.methodMaterialId+ )+ );+ }+ }}+ >+ <div className="flex h-8 items-center">+ {Array.from({ length: node.level }).map((_, index) => (+ <TaskLine key={index} isSelected={state.selected} />+ ))}+ <div+ className={cn(+ "flex h-8 w-4 items-center",+ node.hasChildren && "hover:bg-accent"+ )}+ onClick={(e) => {+ e.stopPropagation();+ if (e.altKey) {+ if (state.expanded) {+ collapseAllBelowDepth(node.level);} else {- toggleExpandNode(node.id);+ expandAllBelowDepth(node.level);}- scrollToNode(node.id);- }}- >- {node.hasChildren ? (- state.expanded ? (- <LuChevronDown className="h-4 w-4 text-gray-400" />- ) : (- <LuChevronUp className="h-4 w-4 text-gray-400" />- )+ } else {+ toggleExpandNode(node.id);+ }+ scrollToNode(node.id);+ }}+ >+ {node.hasChildren ? (+ state.expanded ? (+ <LuChevronDown className="h-4 w-4 text-gray-400" />) : (- <div className="h-8 w-4" />- )}- </div>+ <LuChevronUp className="h-4 w-4 text-gray-400" />+ )+ ) : (+ <div className="h-8 w-4" />+ )}</div>+ </div>- <div className="flex w-full items-center justify-between gap-2">- <div- className={cn(- "flex items-center gap-2 overflow-x-hidden",- node.level > 1 && "opacity-50"- )}- >- <MethodIcon- type={- // node.data.isRoot ? "Method" :- node.data.methodType- }- className="h-4 min-h-4 w-4 min-w-4 flex-shrink-0"- />- <NodeText node={node} />- </div>- <div className="flex items-center gap-1">- {node.data.isRoot ? (- <Badge variant="outline" className="text-xs">- Method- </Badge>- ) : (- <NodeQuantity node={node} />- )}- </div>+ <div className="flex w-full items-center justify-between gap-2">+ <div+ className={cn(+ "flex items-center gap-2 overflow-x-hidden",+ node.level > 1 && "opacity-50"+ )}+ >+ <MethodIcon+ type={+ // node.data.isRoot ? "Method" :+ node.data.methodType+ }+ className="h-4 min-h-4 w-4 min-w-4 flex-shrink-0"+ />+ <NodeText node={node} />+ </div>+ <div className="flex items-center gap-1">+ {node.data.isRoot ? (+ <Badge variant="outline" className="text-xs">+ Method+ </Badge>+ ) : (+ <NodeData node={node} />+ )}</div></div>- </>+ </div>)}/></VStack>@@ -203,11 +203,16 @@ function NodeText({ node }: { node: FlatTreeItem<Method> }) {);}-function NodeQuantity({ node }: { node: FlatTreeItem<Method> }) {+function NodeData({ node }: { node: FlatTreeItem<Method> }) {return (- <Badge className="text-xs" variant="outline">- {node.data.quantity}- </Badge>+ <HStack spacing={1}>+ <Badge className="text-xs" variant="outline">+ {node.data.quantity}+ </Badge>+ <Badge variant="secondary">+ <MethodItemTypeIcon type={node.data.itemType} />+ </Badge>+ </HStack>);}