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
refactor: move quote explorer into it's own component
Brad Barbin46b6aaa0a538
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsxnew file mode 100644index 0000000000..71742e92de--- /dev/null+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx@@ -0,0 +1,239 @@+import { Button, cn, HStack, IconButton } from "@carbon/react";+import type { Params } from "@remix-run/react";+import { Link, useLocation, useParams } from "@remix-run/react";+import { useState } from "react";+import { AiOutlinePartition } from "react-icons/ai";+import { HiOutlineCube } from "react-icons/hi";+import { IoMdAdd } from "react-icons/io";+import { LuClock } from "react-icons/lu";+import { RxChevronDown } from "react-icons/rx";+import type { BillOfMaterialNode } from "~/modules/shared";+import { path } from "~/utils/path";+import { useQuotationMenu } from "./useQuotation";++const QuotationExplorerItem = ({+ type,+ id,+ parentId,+ label,+ meta,+ params,+ pathname,+}: Omit<BillOfMaterialNode, "children"> & {+ params: Params<string>;+ pathname: string;+}) => {+ let isActive = false;+ switch (type) {+ case "assemblies":+ return (+ <Button+ variant="ghost"+ className="w-full justify-between text-muted-foreground"+ asChild+ >+ <Link to={path.to.newQuoteAssembly(params.id!, id, parentId)}>+ <span>{label}</span>+ <IoMdAdd />+ </Link>+ </Button>+ );+ case "materials":+ return (+ <Button+ variant="ghost"+ className="w-full justify-between text-muted-foreground"+ asChild+ >+ <Link+ to={path.to.newQuoteMaterial(params.id!, meta.quoteLineId, meta.id)}+ >+ <span>{label}</span>+ <IoMdAdd />+ </Link>+ </Button>+ );+ case "operations":+ return (+ <Button+ variant="ghost"+ className="w-full justify-between text-muted-foreground"+ asChild+ >+ <Link to={path.to.newQuoteOperation(params.id!, id, parentId)}>+ <span>{label}</span>+ <IoMdAdd />+ </Link>+ </Button>+ );+ case "line":+ isActive = pathname === path.to.quoteLine(params.id!, id);++ return (+ <Button+ variant={isActive ? "primary" : "ghost"}+ className="flex-1 justify-between"+ asChild+ >+ <Link to={path.to.quoteLine(params.id!, id)} prefetch="intent">+ <span className="flex justify-start">+ <AiOutlinePartition className="w-4 h-4 mr-2" />+ {label}+ </span>+ <span+ className={cn(+ "rounded-full h-2 w-2 inline-block ml-2",+ meta?.status === "Complete" && "bg-green-500",+ meta?.status === "In Progress" && "bg-orange-500",+ meta?.status === "Draft" && "bg-zinc-500"+ )}+ />+ </Link>+ </Button>+ );+ case "assembly":+ isActive =+ pathname ===+ path.to.quoteAssembly(params.id!, meta.quoteLineId, meta.id);+ return (+ <Button+ leftIcon={<AiOutlinePartition />}+ variant={isActive ? "primary" : "ghost"}+ className="flex-1 justify-start"+ asChild+ >+ <Link+ to={path.to.quoteAssembly(params.id!, meta.quoteLineId, meta.id)}+ prefetch="intent"+ >+ {label}+ </Link>+ </Button>+ );+ case "operation":+ isActive = [+ path.to.quoteOperation(params.id!, meta.quoteLineId, meta.id),+ path.to.newQuoteMaterial(params.id!, meta.quoteLineId, meta.id),+ ].includes(pathname);+ return (+ <Button+ leftIcon={<LuClock />}+ variant={isActive ? "primary" : "ghost"}+ className="flex-1 justify-start"+ asChild+ >+ <Link+ to={path.to.quoteOperation(params.id!, meta.quoteLineId, meta.id)}+ prefetch="intent"+ >+ {label}+ </Link>+ </Button>+ );+ case "material":+ return (+ <Button+ leftIcon={<HiOutlineCube />}+ variant="ghost"+ className="flex-1 justify-start"+ asChild+ >+ <Link+ to={path.to.quoteOperation(params.id!, meta.quoteLineId, parentId!)}+ >+ {label}+ </Link>+ </Button>+ );+ case "parent":+ isActive = pathname === path.to.quoteDetails(id);+ return (+ <Button+ variant={isActive ? "primary" : "ghost"}+ className="flex-1 justify-start"+ asChild+ >+ <Link to={path.to.quote(params.id!)} prefetch="intent">+ {label}+ </Link>+ </Button>+ );+ default:+ throw new Error(`unknown type: ${type}`);+ }+};++const QuotationExplorer = () => {+ const { pathname } = useLocation();+ const params = useParams();+ if (!params.id) throw new Error("id not found");++ const menu = useQuotationMenu() as BillOfMaterialNode[];++ const [expandedNodes, setExpandedNodes] = useState<Record<string, boolean>>({+ [params.id]: true,+ });++ const toggleNode = (id: string) => {+ setExpandedNodes((prev) => ({+ ...prev,+ [id]: !prev[id],+ }));+ };++ const renderBillOfMaterial = (+ menu: BillOfMaterialNode[],+ level: number = 0+ ) => {+ return menu.map((node) => {+ return (+ <div className="w-full" role="group" key={node.id}>+ <HStack+ className="items-stretch w-full"+ spacing={0}+ style={{+ paddingLeft: `calc(${0.5 * level}rem)`,+ }}+ >+ <IconButton+ aria-label={expandedNodes[node.id] ? "Collapse" : "Expand"}+ onClick={() => toggleNode(node.id)}+ isDisabled={!node.children}+ icon={<RxChevronDown />}+ style={{+ transition: "transform .25s ease",+ transform:+ expandedNodes[node.id] ||+ !node.children ||+ node.children?.length === 0+ ? undefined+ : "rotate(-0.25turn)",+ }}+ variant="ghost"+ />+ <QuotationExplorerItem+ type={node.type}+ id={node.id}+ label={node.label}+ parentId={node.parentId}+ params={params}+ pathname={pathname}+ meta={node.meta}+ />+ </HStack>+ {node.children &&+ expandedNodes[node.id] &&+ renderBillOfMaterial(node.children, level + 1)}+ </div>+ );+ });+ };++ return (+ <div className="w-full h-full overflow-auto" role="tree">+ {renderBillOfMaterial(menu)}+ </div>+ );+};++export default QuotationExplorer;diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/QuotationSidebar.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/QuotationSidebar.tsxdeleted file mode 100644index 33e687e46e..0000000000--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/QuotationSidebar.tsx+++ /dev/null@@ -1,36 +0,0 @@-import { useParams } from "@remix-run/react";-import { DetailSidebar } from "~/components/Layout";-import { useRouteData } from "~/hooks";-import type {- Quotation,- QuotationAttachment,- QuotationLine,-} from "~/modules/sales";-import { path } from "~/utils/path";-import { useQuotationSidebar } from "./useQuotationSidebar";--const QuotationSidebar = () => {- const { id } = useParams();-- if (!id)- throw new Error(- "QuotationSidebar requires an id and could not find id in params"- );-- const routeData = useRouteData<{- quotation: Quotation;- quotationLines: QuotationLine[];- internalDocuments: QuotationAttachment[];- externalDocuments: QuotationAttachment[];- }>(path.to.quote(id));-- const links = useQuotationSidebar({- lines: routeData?.quotationLines.length ?? 0,- internalDocuments: routeData?.internalDocuments.length ?? 0,- externalDocuments: routeData?.externalDocuments.length ?? 0,- });-- return <DetailSidebar links={links} />;-};--export default QuotationSidebar;diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/index.ts b/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/index.tsdeleted file mode 100644index 11a64bd2d5..0000000000--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/index.ts+++ /dev/null@@ -1,3 +0,0 @@-import QuotationSidebar from "./QuotationSidebar";--export default QuotationSidebar;diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/useQuotationSidebar.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/useQuotationSidebar.tsxdeleted file mode 100644index 3f916954bf..0000000000--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationSidebar/useQuotationSidebar.tsx+++ /dev/null@@ -1,57 +0,0 @@-import { useParams } from "@remix-run/react";-import { BsBank, BsListCheck } from "react-icons/bs";-import {- HiOutlineDocumentArrowDown,- HiOutlineDocumentArrowUp,-} from "react-icons/hi2";-import { usePermissions } from "~/hooks";-import type { Role } from "~/types";-import { path } from "~/utils/path";--type Props = {- lines?: number;- externalDocuments?: number;- internalDocuments?: number;-};--export function useQuotationSidebar({- lines = 0,- internalDocuments = 0,- externalDocuments = 0,-}: Props) {- const { id } = useParams();- if (!id) throw new Error("id not found");-- const permissions = usePermissions();- return [- {- name: "Summary",- to: path.to.quoteDetails(id),- icon: BsBank,- },- {- name: "Lines",- to: path.to.quoteLines(id),- count: lines,- icon: BsListCheck,- },- {- name: "Internal Documents",- to: path.to.quoteInternalDocuments(id),- role: ["employee"],- count: internalDocuments,- icon: HiOutlineDocumentArrowDown,- },- {- name: "External Documents",- to: path.to.quoteExternalDocuments(id),- role: ["employee", "supplier"],- count: externalDocuments,- icon: HiOutlineDocumentArrowUp,- },- ].filter(- (item) =>- item.role === undefined ||- item.role.some((role) => permissions.is(role as Role))- );-}diff --git a/apps/carbon/app/modules/sales/ui/Quotation/index.ts b/apps/carbon/app/modules/sales/ui/Quotation/index.tsindex 0a3b7f3efd..24293ebb17 100644--- a/apps/carbon/app/modules/sales/ui/Quotation/index.ts+++ b/apps/carbon/app/modules/sales/ui/Quotation/index.ts@@ -1,5 +1,6 @@import QuotationAssemblyForm from "./QuotationAssemblyForm";import QuotationDocuments from "./QuotationDocuments";+import QuotationExplorer from "./QuotationExplorer";import QuotationForm from "./QuotationForm";import QuotationHeader from "./QuotationHeader";import QuotationLineForm from "./QuotationLineForm";@@ -7,12 +8,12 @@ import QuotationLineQuantities from "./QuotationLineQuantities";import QuotationMaterialForm from "./QuotationMaterialForm";import QuotationMaterialLines from "./QuotationMaterialLines";import QuotationOperationForm from "./QuotationOperationForm";-import QuotationSidebar from "./QuotationSidebar";import QuotationStatus from "./QuotationStatus";import { useQuotation } from "./useQuotation";export {QuotationAssemblyForm,QuotationDocuments,+ QuotationExplorer,QuotationForm,QuotationHeader,QuotationLineForm,@@ -20,7 +21,6 @@ export {QuotationMaterialForm,QuotationMaterialLines,QuotationOperationForm,- QuotationSidebar,QuotationStatus,useQuotation,};diff --git a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsxindex 03b15990d3..5210be7767 100644--- a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx+++ b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx@@ -9,25 +9,7 @@ import type {QuotationMaterial,QuotationOperation,} from "~/modules/sales";--type BillOfMaterialNodeType =- | "parent"- | "line"- | "assemblies"- | "operations"- | "materials"- | "assembly"- | "operation"- | "material";--type BillOfMaterialNode = {- id: string;- parentId?: string;- label: string;- type: BillOfMaterialNodeType;- meta?: any;- children?: BillOfMaterialNode[];-};+import type { BillOfMaterialNode } from "~/modules/shared";type Quote = {quote?: Quotation;@@ -80,7 +62,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {children: materialsByOperationId[operation.id]?.map((material) => ({id: material.id,parentId: operation.id,- label: material.partId,+ label: material.description,type: "material",meta: material,})),@@ -108,7 +90,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {itemsByLineId[assembly.quoteLineId].assemblies.push({id: assembly.id,parentId: assembly.parentAssemblyId ?? undefined,- label: assembly.partId,+ label: assembly.description,type: "assembly",meta: assembly,});@@ -136,7 +118,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {children: materialsByOperationId[operation.id]?.map((material) => ({id: material.id,parentId: operation.id,- label: material.partId,+ label: material.description,type: "material",meta: material,})),@@ -152,7 +134,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {type: "parent",children: store.lines.map<BillOfMaterialNode>((line) => ({id: line.id,- label: line.partId,+ label: line.description,type: "line",meta: line,children: [@@ -160,12 +142,14 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {id: line.id,label: "Assemblies",type: "assemblies",- children: [- ...(arrayToTree(itemsByLineId[line.id]?.assemblies, {- id: "id",- dataField: null,- }) as BillOfMaterialNode[]),- ],+ children: itemsByLineId[line.id]?.assemblies+ ? [+ ...(arrayToTree(itemsByLineId[line.id].assemblies, {+ id: "id",+ dataField: null,+ }) as BillOfMaterialNode[]),+ ]+ : [],},{id: line.id,diff --git a/apps/carbon/app/modules/shared/types.ts b/apps/carbon/app/modules/shared/types.tsindex 10613764b9..f521c6ce32 100644--- a/apps/carbon/app/modules/shared/types.ts+++ b/apps/carbon/app/modules/shared/types.ts@@ -1,5 +1,24 @@import type { getNotes } from "./shared.service";+export type BillOfMaterialNodeType =+ | "parent"+ | "line"+ | "assemblies"+ | "operations"+ | "materials"+ | "assembly"+ | "operation"+ | "material";++export type BillOfMaterialNode = {+ id: string;+ parentId?: string;+ label: string;+ type: BillOfMaterialNodeType;+ meta?: any;+ children?: BillOfMaterialNode[];+};+export type Note = NonNullable<Awaited<ReturnType<typeof getNotes>>["data"]>[number];diff --git a/apps/carbon/app/routes/x+/quote+/$id.tsx b/apps/carbon/app/routes/x+/quote+/$id.tsxindex cd08d36c39..869fc7df7d 100644--- a/apps/carbon/app/routes/x+/quote+/$id.tsx+++ b/apps/carbon/app/routes/x+/quote+/$id.tsx@@ -1,5 +1,4 @@import {- Button,HStack,Heading,IconButton,@@ -10,24 +9,21 @@ import {TooltipContent,TooltipTrigger,VStack,- cn,} from "@carbon/react";import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";import { json, redirect } from "@remix-run/node";-import type { Params } from "@remix-run/react";import {- Link,Outlet,useLoaderData,- useLocation,useNavigate,useParams,} from "@remix-run/react";-import { useEffect, useState } from "react";+import { useEffect } from "react";import { IoMdAdd } from "react-icons/io";import { CollapsibleSidebar } from "~/components/Layout";import { getLocationsList } from "~/modules/resources";import {+ QuotationExplorer,QuotationStatus,getQuote,getQuoteAssemblies,@@ -45,12 +41,6 @@ import type { Handle } from "~/utils/handle";import { path } from "~/utils/path";import { error } from "~/utils/result";-import { AiOutlinePartition } from "react-icons/ai";-import { HiOutlineCube } from "react-icons/hi";-import { LuClock } from "react-icons/lu";-import { RxChevronDown } from "react-icons/rx";-import { useQuotationMenu } from "~/modules/sales/ui/Quotation/useQuotation";-export const handle: Handle = {breadcrumb: "Quotations",to: path.to.quotes,@@ -167,7 +157,7 @@ export default function QuotationRoute() {</HStack></VStack><VStack className="h-[calc(100vh-183px)] p-2 w-full">- <BillOfMaterialExplorer />+ <QuotationExplorer /></VStack></CollapsibleSidebar><VStack className="p-2">@@ -186,361 +176,6 @@ export default function QuotationRoute() {<Outlet /></VStack>- {/* <QuotationHeader />- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">- <QuotationSidebar />- <Outlet />- </div> */}</div>);}--type BillOfMaterialNodeType =- | "parent"- | "line"- | "assemblies"- | "operations"- | "materials"- | "assembly"- | "operation"- | "material";--type BillOfMaterialNode = {- id: string;- parentId?: string;- label: string;- type: BillOfMaterialNodeType;- meta?: any;- children?: BillOfMaterialNode[];-};--// const data: BillOfMaterialNode[] = [-// {-// id: "0",-// label: "QUO000001",-// type: "parent",-// children: [-// {-// id: "cneklo3nm0lgr74492e0",-// label: "P00001233",-// type: "line",-// children: [-// {-// id: "cneklo3nm0lgr74492e0",-// label: "Assemblies",-// type: "assemblies",-// children: [-// {-// id: "4",-// label: "F5000123",-// type: "assembly",-// children: [-// {-// id: "5",-// label: "Assemblies",-// type: "assemblies",-// children: [],-// },-// {-// id: "6",-// label: "Operations",-// type: "operations",-// children: [-// {-// id: "7",-// label: "OP0003",-// type: "operation",-// children: [-// {-// id: "8",-// label: "Materials",-// type: "materials",-// children: [-// {-// id: "9",-// label: "RAW000001",-// type: "material",-// },-// {-// id: "10",-// label: "FAS000002",-// type: "material",-// },-// ],-// },-// ],-// },-// ],-// },-// ],-// },-// ],-// },-// {-// id: "cneklo3nm0lgr74492e0",-// label: "Operations",-// type: "operations",-// children: [-// {-// id: "12",-// label: "OP0001",-// type: "operation",-// children: [-// {-// id: "13",-// label: "Materials",-// type: "materials",-// children: [-// {-// id: "14",-// label: "RAW000003",-// type: "material",-// },-// ],-// },-// ],-// },-// {-// id: "13",-// label: "OP0002",-// type: "operation",-// children: [-// {-// id: "13",-// label: "Materials",-// type: "materials",-// },-// ],-// },-// ],-// },-// ],-// },-// ],-// },-// ];--const BillOfMaterialItem = ({- type,- id,- parentId,- label,- meta,- params,- pathname,-}: Omit<BillOfMaterialNode, "children"> & {- params: Params<string>;- pathname: string;-}) => {- let isActive = false;- switch (type) {- case "assemblies":- return (- <Button- variant="ghost"- className="w-full justify-between text-muted-foreground"- asChild- >- <Link to={path.to.newQuoteAssembly(params.id!, id, parentId)}>- <span>{label}</span>- <IoMdAdd />- </Link>- </Button>- );- case "materials":- return (- <Button- variant="ghost"- className="w-full justify-between text-muted-foreground"- asChild- >- <Link- to={path.to.newQuoteMaterial(params.id!, meta.quoteLineId, meta.id)}- >- <span>{label}</span>- <IoMdAdd />- </Link>- </Button>- );- case "operations":- return (- <Button- variant="ghost"- className="w-full justify-between text-muted-foreground"- asChild- >- <Link to={path.to.newQuoteOperation(params.id!, id, parentId)}>- <span>{label}</span>- <IoMdAdd />- </Link>- </Button>- );- case "line":- isActive = pathname === path.to.quoteLine(params.id!, id);-- return (- <Button- variant={isActive ? "primary" : "ghost"}- className="flex-1 justify-between"- asChild- >- <Link to={path.to.quoteLine(params.id!, id)} prefetch="intent">- <span className="flex justify-start">- <AiOutlinePartition className="w-4 h-4 mr-2" />- {label}- </span>- <span- className={cn(- "rounded-full h-2 w-2 inline-block ml-2",- meta?.status === "Complete" && "bg-green-500",- meta?.status === "In Progress" && "bg-orange-500",- meta?.status === "Draft" && "bg-zinc-500"- )}- />- </Link>- </Button>- );- case "assembly":- isActive =- pathname ===- path.to.quoteAssembly(params.id!, meta.quoteLineId, meta.id);- return (- <Button- leftIcon={<AiOutlinePartition />}- variant={isActive ? "primary" : "ghost"}- className="flex-1 justify-start"- asChild- >- <Link- to={path.to.quoteAssembly(params.id!, meta.quoteLineId, meta.id)}- prefetch="intent"- >- {label}- </Link>- </Button>- );- case "operation":- isActive = [- path.to.quoteOperation(params.id!, meta.quoteLineId, meta.id),- path.to.newQuoteMaterial(params.id!, meta.quoteLineId, meta.id),- ].includes(pathname);- return (- <Button- leftIcon={<LuClock />}- variant={isActive ? "primary" : "ghost"}- className="flex-1 justify-start"- asChild- >- <Link- to={path.to.quoteOperation(params.id!, meta.quoteLineId, meta.id)}- prefetch="intent"- >- {label}- </Link>- </Button>- );- case "material":- return (- <Button- leftIcon={<HiOutlineCube />}- variant="ghost"- className="flex-1 justify-start"- asChild- >- <Link- to={path.to.quoteOperation(params.id!, meta.quoteLineId, parentId!)}- >- {label}- </Link>- </Button>- );- case "parent":- isActive = pathname === path.to.quoteDetails(id);- return (- <Button- variant={isActive ? "primary" : "ghost"}- className="flex-1 justify-start"- asChild- >- <Link to={path.to.quote(params.id!)} prefetch="intent">- {label}- </Link>- </Button>- );- default:- throw new Error(`unknown type: ${type}`);- }-};--const BillOfMaterialExplorer = () => {- const { pathname } = useLocation();- const params = useParams();- if (!params.id) throw new Error("id not found");-- const menu = useQuotationMenu() as BillOfMaterialNode[];-- const [expandedNodes, setExpandedNodes] = useState<Record<string, boolean>>({- [params.id]: true,- });-- const toggleNode = (id: string) => {- setExpandedNodes((prev) => ({- ...prev,- [id]: !prev[id],- }));- };-- const renderBillOfMaterial = (- menu: BillOfMaterialNode[],- level: number = 0- ) => {- return menu.map((node) => {- return (- <div className="w-full" role="group" key={node.id}>- <HStack- className="items-stretch w-full"- spacing={0}- style={{- paddingLeft: `calc(${0.5 * level}rem)`,- }}- >- <IconButton- aria-label={expandedNodes[node.id] ? "Collapse" : "Expand"}- onClick={() => toggleNode(node.id)}- isDisabled={!node.children}- icon={<RxChevronDown />}- style={{- transition: "transform .25s ease",- transform:- expandedNodes[node.id] ||- !node.children ||- node.children?.length === 0- ? undefined- : "rotate(-0.25turn)",- }}- variant="ghost"- />- <BillOfMaterialItem- type={node.type}- id={node.id}- label={node.label}- parentId={node.parentId}- params={params}- pathname={pathname}- meta={node.meta}- />- </HStack>- {node.children &&- expandedNodes[node.id] &&- renderBillOfMaterial(node.children, level + 1)}- </div>- );- });- };-- return (- <div className="w-full h-full overflow-auto" role="tree">- {renderBillOfMaterial(menu)}- </div>- );-};