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.

feat: use defer for method trees

Brad Barbin11389d2ae709
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx
index 70a670a5bf..4897266a97 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBreadcrumbs.tsx
@@ -39,14 +39,14 @@ const QuoteBreadcrumbs = () => {
const routeData = useRouteData<{
quote: Quotation;
lines: QuotationLine[];
- methods: Tree<QuoteMethod>[];
+ methods: Promise<Tree<QuoteMethod>[]> | Tree<QuoteMethod>[];
}>(path.to.quote(quoteId));
const line = routeData?.lines.find((line) => line.id === lineId);
const { pathname } = useLocation();
- const methodTree = routeData?.methods.find(
- (m) => m.data.quoteLineId === line?.id
- );
+ const methodTree = Array.isArray(routeData?.methods)
+ ? routeData?.methods.find((m) => m.data.quoteLineId === line?.id)
+ : undefined;
const hasMethods = methodTree?.children && methodTree.children.length > 0;
const isGetMethodLoading =
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx
index e28833378e..b6b3dddbc7 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteExplorer.tsx
@@ -38,7 +38,11 @@ import DeleteQuoteLine from "./DeleteQuoteLine";
import QuoteBoMExplorer from "./QuoteBoMExplorer";
import QuoteLineForm from "./QuoteLineForm";
-export default function QuoteExplorer() {
+type QuoteExplorerProps = {
+ methods: Tree<QuoteMethod>[];
+};
+
+export default function QuoteExplorer({ methods }: QuoteExplorerProps) {
const { quoteId } = useParams();
if (!quoteId) throw new Error("Could not find quoteId");
const quoteData = useRouteData<{ quote: Quotation; lines: QuotationLine[] }>(
@@ -98,6 +102,7 @@ export default function QuoteExplorer() {
isDisabled={isDisabled}
line={line}
onDelete={onDeleteLine}
+ methods={methods}
/>
))
) : (
@@ -154,9 +159,15 @@ type QuoteLineItemProps = {
line: QuotationLine;
isDisabled: boolean;
onDelete: (line: QuotationLine) => void;
+ methods: Tree<QuoteMethod>[];
};
-function QuoteLineItem({ line, isDisabled, onDelete }: QuoteLineItemProps) {
+function QuoteLineItem({
+ line,
+ isDisabled,
+ onDelete,
+ methods,
+}: QuoteLineItemProps) {
const { quoteId, lineId } = useParams();
if (!quoteId) throw new Error("Could not find quoteId");
const permissions = usePermissions();
@@ -170,14 +181,8 @@ function QuoteLineItem({ line, isDisabled, onDelete }: QuoteLineItemProps) {
}
});
- const quoteData = useRouteData<{ methods: Tree<QuoteMethod>[] }>(
- path.to.quote(quoteId)
- );
-
- const methodTree = quoteData?.methods?.find(
- (m) => m.data.quoteLineId === line.id
- );
- const methods = methodTree ? flattenTree(methodTree) : [];
+ const methodTree = methods.find((m) => m.data.quoteLineId === line.id);
+ const flattenedMethods = methodTree ? flattenTree(methodTree) : [];
const isSelected = lineId === line.id;
const onLineClick = (line: QuotationLine) => {
@@ -257,7 +262,7 @@ function QuoteLineItem({ line, isDisabled, onDelete }: QuoteLineItemProps) {
line.methodType === "Make" &&
permissions.can("update", "sales") && (
<VStack className="border-b border-border p-1">
- <QuoteBoMExplorer methods={methods} />
+ <QuoteBoMExplorer methods={flattenedMethods} />
</VStack>
)}
</VStack>
diff --git a/apps/carbon/app/routes/x+/job+/$jobId.tsx b/apps/carbon/app/routes/x+/job+/$jobId.tsx
index b7ce7909a7..283c276f7b 100644
--- a/apps/carbon/app/routes/x+/job+/$jobId.tsx
+++ b/apps/carbon/app/routes/x+/job+/$jobId.tsx
@@ -7,13 +7,16 @@ import {
VStack,
} from "@carbon/react";
import {
- json,
+ Await,
+ defer,
Outlet,
redirect,
useLoaderData,
useParams,
} from "@remix-run/react";
import type { LoaderFunctionArgs } from "@vercel/remix";
+import { Suspense } from "react";
+import { ExplorerSkeleton } from "~/components/Skeletons";
import { flattenTree } from "~/components/TreeView";
import {
getJob,
@@ -23,7 +26,6 @@ import {
JobHeader,
} from "~/modules/production";
import JobBreadcrumbs from "~/modules/production/ui/Jobs/JobBreadcrumbs";
-import {} from "~/modules/sales";
import { requirePermissions } from "~/services/auth/auth.server";
import { flash } from "~/services/session.server";
import type { Handle } from "~/utils/handle";
@@ -43,6 +45,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const { jobId } = params;
if (!jobId) throw new Error("Could not find jobId");
+
const job = await getJob(client, jobId);
if (job.error) {
@@ -54,19 +57,12 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const files = await getJobDocuments(client, companyId, job.data);
- const method = await getJobMethodTree(client, jobId);
-
- if (method.error) {
- throw redirect(
- path.to.jobs,
- await flash(request, error(method.error, "Failed to load job method"))
- );
- }
+ const methodPromise = getJobMethodTree(client, jobId);
- return json({
+ return defer({
job: job.data,
- files: files,
- method: method.data ?? [],
+ files,
+ method: methodPromise,
});
}
@@ -76,7 +72,6 @@ export default function JobRoute() {
if (!jobId) throw new Error("Could not find jobId");
const { method } = useLoaderData<typeof loader>();
- const flattenedTree = method.length > 0 ? flattenTree(method[0]) : [];
return (
<div className="flex flex-col h-[calc(100vh-49px)] w-full">
@@ -95,7 +90,27 @@ export default function JobRoute() {
>
<ScrollArea className="h-[calc(100vh-99px)]">
<div className="grid w-full h-full overflow-hidden p-2">
- <JobBoMExplorer method={flattenedTree} />
+ <Suspense fallback={<ExplorerSkeleton />}>
+ <Await
+ resolve={method}
+ errorElement={
+ <div className="p-2 text-red-500">
+ Error loading job tree.
+ </div>
+ }
+ >
+ {(resolvedMethod) => (
+ <JobBoMExplorer
+ method={
+ resolvedMethod.data &&
+ resolvedMethod.data.length > 0
+ ? flattenTree(resolvedMethod.data[0])
+ : []
+ }
+ />
+ )}
+ </Await>
+ </Suspense>
</div>
</ScrollArea>
</ResizablePanel>
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
index a60b50c84b..64ae72c772 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
@@ -120,12 +120,15 @@ export default function QuoteLine() {
useRealtime("quoteMaterial", `quoteLineId=eq.${lineId}`);
useRealtime("quoteOperation", `quoteLineId=eq.${lineId}`);
- const quoteData = useRouteData<{ methods: Tree<QuoteMethod>[] }>(
- path.to.quote(quoteId)
- );
+ const quoteData = useRouteData<{
+ methods: Promise<Tree<QuoteMethod>[]> | Tree<QuoteMethod>[];
+ }>(path.to.quote(quoteId));
const methodTree = useMemo(
- () => quoteData?.methods?.find((m) => m.data.quoteLineId === line.id),
+ () =>
+ Array.isArray(quoteData?.methods)
+ ? quoteData.methods.find((m) => m.data.quoteLineId === line.id)
+ : undefined,
[quoteData, line.id]
);
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.new.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.new.tsx
index 861a0cff4e..1955188f74 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.new.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.new.tsx
@@ -51,22 +51,24 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
const quoteLineId = createQuotationLine.data.id;
- const upsertMethod = await upsertQuoteLineMethod(serviceRole, {
- quoteId,
- quoteLineId,
- itemId: data.itemId,
- companyId,
- userId,
- });
+ if (data.methodType === "Make") {
+ const upsertMethod = await upsertQuoteLineMethod(serviceRole, {
+ quoteId,
+ quoteLineId,
+ itemId: data.itemId,
+ companyId,
+ userId,
+ });
- if (upsertMethod.error) {
- throw redirect(
- path.to.quoteLine(quoteId, quoteLineId),
- await flash(
- request,
- error(upsertMethod.error, "Failed to create quote line method.")
- )
- );
+ if (upsertMethod.error) {
+ throw redirect(
+ path.to.quoteLine(quoteId, quoteLineId),
+ await flash(
+ request,
+ error(upsertMethod.error, "Failed to create quote line method.")
+ )
+ );
+ }
}
throw redirect(path.to.quoteLine(quoteId, quoteLineId));
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
index 1d2075a840..3e3494337a 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
@@ -6,9 +6,11 @@ import {
ScrollArea,
VStack,
} from "@carbon/react";
-import { Outlet, useParams } from "@remix-run/react";
+import { Await, Outlet, useLoaderData, useParams } from "@remix-run/react";
import type { LoaderFunctionArgs } from "@vercel/remix";
-import { json, redirect } from "@vercel/remix";
+import { defer, redirect } from "@vercel/remix";
+import { Suspense } from "react";
+import { ExplorerSkeleton } from "~/components/Skeletons";
import {
getOpportunityByQuote,
getOpportunityDocuments,
@@ -42,25 +44,15 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const { quoteId } = params;
if (!quoteId) throw new Error("Could not find quoteId");
- const [
- quote,
- shipment,
- payment,
- lines,
- methods,
- prices,
- // shippingTerms,
- opportunity,
- ] = await Promise.all([
- getQuote(client, quoteId),
- getQuoteShipment(client, quoteId),
- getQuotePayment(client, quoteId),
- getQuoteLines(client, quoteId),
- getQuoteMethodTrees(client, quoteId),
- getQuoteLinePricesByQuoteId(client, quoteId),
- // getShippingTermsList(client, companyId),
- getOpportunityByQuote(client, quoteId),
- ]);
+ const [quote, shipment, payment, lines, prices, opportunity] =
+ await Promise.all([
+ getQuote(client, quoteId),
+ getQuoteShipment(client, quoteId),
+ getQuotePayment(client, quoteId),
+ getQuoteLines(client, quoteId),
+ getQuoteLinePricesByQuoteId(client, quoteId),
+ getOpportunityByQuote(client, quoteId),
+ ]);
if (!opportunity.data) throw new Error("Failed to get opportunity record");
const files = await getOpportunityDocuments(
@@ -93,15 +85,14 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
- return json({
+ return defer({
quote: quote.data,
lines: lines.data ?? [],
- methods: methods.data ?? [],
+ methods: getQuoteMethodTrees(client, quoteId),
files: files.data ?? [],
prices: prices.data ?? [],
shipment: shipment.data,
payment: payment.data,
- // shippingTerms: shippingTerms.data ?? [],
opportunity: opportunity.data,
});
}
@@ -110,6 +101,7 @@ export default function QuoteRoute() {
const params = useParams();
const { quoteId } = params;
if (!quoteId) throw new Error("Could not find quoteId");
+ const { methods } = useLoaderData<typeof loader>();
return (
<div className="flex flex-col h-[calc(100vh-49px)] w-full">
@@ -128,7 +120,33 @@ export default function QuoteRoute() {
>
<ScrollArea className="h-[calc(100vh-99px)]">
<div className="grid w-full h-full overflow-hidden">
- <QuoteExplorer />
+ <Suspense
+ fallback={
+ <div className="p-2">
+ <ExplorerSkeleton />
+ </div>
+ }
+ >
+ <Await
+ resolve={methods}
+ errorElement={
+ <div className="p-2 text-red-500">
+ Error loading quote tree.
+ </div>
+ }
+ >
+ {(resolvedMethods) => (
+ <QuoteExplorer
+ methods={
+ resolvedMethods.data &&
+ resolvedMethods.data.length > 0
+ ? resolvedMethods.data
+ : []
+ }
+ />
+ )}
+ </Await>
+ </Suspense>
</div>
</ScrollArea>
</ResizablePanel>