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 client-side image rendering when possible

Brad Barbinae8f55f56e1f
diff --git a/apps/carbon/app/components/CadModel.tsx b/apps/carbon/app/components/CadModel.tsx
index f026c43668..376667b036 100644
--- a/apps/carbon/app/components/CadModel.tsx
+++ b/apps/carbon/app/components/CadModel.tsx
@@ -13,6 +13,7 @@ import { convertKbToString } from "@carbon/utils";
import { useFetcher } from "@remix-run/react";
import { nanoid } from "nanoid";
import { useState } from "react";
+import { flushSync } from "react-dom";
import { useDropzone } from "react-dropzone";
import { LuUploadCloud } from "react-icons/lu";
import { useUser } from "~/hooks";
@@ -52,15 +53,68 @@ const CadModel = ({
const fetcher = useFetcher<{}>();
const [file, setFile] = useState<File | null>(null);
+ const [id, setId] = useState<string | null>(null);
+
+ const onDataUrl = async (dataUrl: string) => {
+ if (!id || !file) return;
+
+ const base64Data = dataUrl.split(",")[1];
+ const byteCharacters = atob(base64Data);
+ const byteNumbers = new Array(byteCharacters.length);
+
+ for (let i = 0; i < byteCharacters.length; i++) {
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
+ }
+
+ const byteArray = new Uint8Array(byteNumbers);
+ const thumbnailFile = new File([byteArray], "thumbnail.png", {
+ type: "image/png",
+ });
+
+ if (!carbon) {
+ toast.error("Failed to initialize carbon client");
+ return;
+ }
+
+ const thumbnailId = nanoid();
+ const thumbnailPath = `${companyId}/thumbnails/${thumbnailId}.png`;
+
+ const thumbnailUpload = await carbon.storage
+ .from("private")
+ .upload(thumbnailPath, thumbnailFile, {
+ upsert: true,
+ contentType: "image/png",
+ });
+
+ if (thumbnailUpload.error) {
+ toast.error("Failed to upload thumbnail");
+ }
+
+ const update = await carbon
+ .from("modelUpload")
+ .update({
+ thumbnailPath: thumbnailUpload.data!.path,
+ })
+ .eq("id", id);
+
+ if (update.error) {
+ toast.error("Failed to update model upload");
+ }
+ };
const onFileChange = async (file: File | null) => {
+ const modelId = nanoid();
+ flushSync(() => {
+ setId(modelId);
+ });
+
setFile(file);
+
if (file) {
if (!carbon) {
toast.error("Failed to initialize carbon client");
return;
}
- const modelId = nanoid();
const fileExtension = file.name.split(".").pop();
const fileName = `${companyId}/models/${modelId}.${fileExtension}`;
@@ -118,6 +172,7 @@ const CadModel = ({
file={file}
url={modelPath ? getPrivateUrl(modelPath) : null}
mode={mode}
+ onDataUrl={onDataUrl}
/>
) : (
<CadModelUpload
diff --git a/apps/carbon/app/routes/api+/model.upload.ts b/apps/carbon/app/routes/api+/model.upload.ts
index 931dd20506..8cfa7c63f7 100644
--- a/apps/carbon/app/routes/api+/model.upload.ts
+++ b/apps/carbon/app/routes/api+/model.upload.ts
@@ -1,10 +1,6 @@
// import { error } from "@carbon/auth";
-import { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
-import { flash } from "@carbon/auth/session.server";
-import { tasks } from "@trigger.dev/sdk/v3";
import { json, type ActionFunctionArgs } from "@vercel/remix";
-import type { modelThumbnailTask } from "~/trigger/model-thumbnail";
export const config = { runtime: "nodejs" };
@@ -76,18 +72,6 @@ export async function action({ request }: ActionFunctionArgs) {
throw new Error("Failed to record upload: " + modelRecord.error.message);
}
- try {
- await tasks.trigger<typeof modelThumbnailTask>("model-thumbnail", {
- modelId,
- companyId,
- });
- } catch (err) {
- return json(
- {},
- await flash(request, error(err, "Failed to generate thumbnail for model"))
- );
- }
-
return json({
success: true,
});
diff --git a/packages/react/src/ModelViewer.tsx b/packages/react/src/ModelViewer.tsx
index c36294fd11..b592d672a0 100644
--- a/packages/react/src/ModelViewer.tsx
+++ b/packages/react/src/ModelViewer.tsx
@@ -28,10 +28,12 @@ export function ModelViewer({
file,
url,
mode = "dark",
+ onDataUrl,
}: {
file: File | null;
url: string | null;
mode?: "dark" | "light";
+ onDataUrl?: (dataUrl: string) => void;
}) {
const parentDiv = useRef<HTMLDivElement>(null);
const viewerRef = useRef<OV.EmbeddedViewer | null>(null);
@@ -79,6 +81,9 @@ export function ModelViewer({
camera.up = new OV.Coord3D(0, 1, 0);
viewer3D.SetCamera(camera);
}
+
+ const dataUrl = viewer3D.GetImageAsDataUrl(300, 300, true);
+ onDataUrl?.(dataUrl);
}
setIsLoading(false);
@@ -162,17 +167,14 @@ export function ModelViewer({
ref={parentDiv}
role={"img"}
aria-label="Canvas showing the model in the 3D Viewer"
- className="h-full w-full items-center justify-center rounded-lg relative border border-border dark:border-0 dark:shadow-[0px_1px_0px_0px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_rgba(0,0,0,.1),0px_2px_2px_0px_rgba(0,0,0,.1),0px_4px_4px_0px_rgba(0,0,0,.1),0px_8px_8px_0px_rgba(0,0,0,.1)] bg-red-500 min-h-[400px] shadow-md"
+ className="h-full w-full items-center justify-center rounded-lg relative border border-border dark:border-0 dark:shadow-[0px_1px_0px_0px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_rgba(0,0,0,.1),0px_2px_2px_0px_rgba(0,0,0,.1),0px_4px_4px_0px_rgba(0,0,0,.1),0px_8px_8px_0px_rgba(0,0,0,.1)] bg-gradient-to-bl from-card from-50% via-card to-background min-h-[400px] shadow-md"
>
{isLoading ? (
<div className="absolute inset-0 bg-card h-full w-full flex items-center justify-center">
<Spinner className="w-10 h-10" />
</div>
) : (
- <span
- id="model-viewer-canvas"
- className="w-[1px] h-[1px] bg-transparent"
- />
+ <pre id="model-viewer-canvas" aria-hidden className="sr-only" />
)}
</div>
</>