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.

docs: example for 3d model upload

Brad Barbinf71a5dd8ac73
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 0000000000..61bdf81040
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,35 @@
+# Examples
+
+This folder contains examples for interacting with Carbon through the API to accomplish different things.
+
+In most cases, we first define an API client like this, and then selectively add methods. Each example will have different methods to accomplish some task. For more details, we recommend checking out the `.service.ts` files in the `/apps` repo.
+
+```ts
+import type { PostgrestError, SupabaseClient } from "@supabase/supabase-js";
+import { createClient } from "@supabase/supabase-js";
+import {
+ CARBON_API_KEY,
+ CARBON_API_URL,
+ CARBON_APP_URL,
+ CARBON_COMPANY_ID,
+ CARBON_PUBLIC_KEY,
+} from "~/config";
+
+class CarbonClient {
+ private readonly appUrl: string = CARBON_APP_URL;
+ private readonly client: SupabaseClient;
+ private readonly companyId: string = CARBON_COMPANY_ID;
+ constructor() {
+ this.client = createClient(CARBON_API_URL, CARBON_PUBLIC_KEY, {
+ global: {
+ headers: {
+ "carbon-key": CARBON_API_KEY,
+ },
+ },
+ });
+ }
+}
+
+const carbon = new CarbonClient();
+export { carbon };
+```
diff --git a/examples/upload-3d-model/README.md b/examples/upload-3d-model/README.md
index b66183fda6..618b07ffcb 100644
--- a/examples/upload-3d-model/README.md
+++ b/examples/upload-3d-model/README.md
@@ -2,7 +2,7 @@
This example demonstrates how to upload an RFQ with a 3D model through the Carbon API.
-First run Carbon locally, and make sure your company ID and API key are set in the `.env` file.
+First run Carbon locally, and make sure your company ID and API key are set in your `.env` or `.env.local` file.
```bash
npm run dev
@@ -12,3 +12,9 @@ npm run dev
cd examples/upload-3d-model
npm run start
```
+
+### Relevant Files:
+
+- `./components/ModelUpload.tsx`
+- `./lib/carbon.server.ts`
+- `./routes/_index.tsx`
diff --git a/examples/upload-3d-model/app/components/ModelUpload.tsx b/examples/upload-3d-model/app/components/ModelUpload.tsx
new file mode 100644
index 0000000000..bf7c0c7318
--- /dev/null
+++ b/examples/upload-3d-model/app/components/ModelUpload.tsx
@@ -0,0 +1,116 @@
+import { cn, Spinner, toast } from "@carbon/react";
+import { convertKbToString } from "@carbon/utils";
+import { useDropzone } from "react-dropzone";
+import { LuCloudUpload } from "react-icons/lu";
+
+type ModelUploadProps = {
+ url?: string;
+ file: File | null;
+ className?: string;
+ onFileChange: (file: File | null) => void;
+};
+
+const fileSizeLimitMb = 50;
+const supportedModelTypes = ["stp", "step"];
+
+export const ModelUpload = ({
+ url,
+ file,
+ onFileChange,
+ className,
+}: ModelUploadProps) => {
+ const hasFile = !!file || !!url;
+
+ const { isDragActive, getRootProps, getInputProps } = useDropzone({
+ accept: {
+ "application/step": [".stp", ".step"],
+ },
+ disabled: hasFile,
+ multiple: false,
+ maxSize: fileSizeLimitMb * 1024 * 1024, // 50 MB
+ onDropAccepted: (acceptedFiles) => {
+ const file = acceptedFiles[0];
+ const fileSizeLimit = fileSizeLimitMb * 1024 * 1024;
+
+ const fileExtension = file.name.split(".").pop()?.toLowerCase();
+ if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
+ toast.error("File type not supported");
+
+ return;
+ }
+
+ if (file.size > fileSizeLimit) {
+ toast.error(`File size too big (max. ${fileSizeLimitMb} MB)`);
+ return;
+ }
+
+ onFileChange(file);
+ },
+ onDropRejected: (fileRejections) => {
+ const { errors } = fileRejections[0];
+ let message;
+ if (errors[0].code === "file-too-large") {
+ message = `File size too big (max. ${fileSizeLimitMb} MB)`;
+ } else if (errors[0].code === "file-invalid-type") {
+ message = "File type not supported";
+ } else {
+ message = errors[0].message;
+ }
+ toast.error(message);
+ },
+ });
+
+ return (
+ <div
+ {...getRootProps()}
+ className={cn(
+ "group flex flex-col flex-grow rounded-lg border border-border bg-gradient-to-tr from-background to-card text-card-foreground shadow-sm w-full",
+ !hasFile &&
+ "cursor-pointer hover:border-primary/30 hover:border-dashed hover:to-primary/10 hover:via-card border-2 border-dashed",
+ className
+ )}
+ >
+ <input {...getInputProps()} name="file" className="sr-only" />
+ <div className="flex flex-col h-full w-full p-4">
+ <div className="flex flex-col flex-grow items-center justify-center gap-2 p-6">
+ {file && (
+ <>
+ <p className="text-base font-medium text-card-foreground">
+ {file.name}
+ </p>
+ <p className="text-muted-foreground text-xs group-hover:text-foreground">
+ {convertKbToString(Math.ceil(file.size / 1024))}
+ </p>
+ <Spinner className="size-12" />
+ </>
+ )}
+ {!file && (
+ <>
+ <div
+ className={cn(
+ "p-4 bg-accent rounded-full group-hover:bg-primary"
+ )}
+ >
+ <LuCloudUpload className="mx-auto size-12 text-muted-foreground group-hover:text-primary-foreground" />
+ </div>
+ {isDragActive ? (
+ <p className="text-lg text-muted-foreground group-hover:text-foreground mt-8">
+ Drop file here
+ </p>
+ ) : (
+ <>
+ <p className="text-base font-medium text-muted-foreground group-hover:text-foreground mt-8">
+ Choose file to upload or drag and drop
+ </p>
+ <p className="text-xs text-muted-foreground group-hover:text-foreground">
+ Supports {supportedModelTypes.join(", ")} files
+ </p>
+ </>
+ )}
+ </>
+ )}
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/examples/upload-3d-model/app/routes/_index.tsx b/examples/upload-3d-model/app/routes/_index.tsx
index b7303f04e3..735b84a1f0 100644
--- a/examples/upload-3d-model/app/routes/_index.tsx
+++ b/examples/upload-3d-model/app/routes/_index.tsx
@@ -1,3 +1,86 @@
-export default function IndexRoute() {
- return <h3 className="text-2xl font-bold">RFQ Upload</h3>;
+import {
+ Copy,
+ Heading,
+ Input,
+ InputGroup,
+ InputLeftAddon,
+ toast,
+ TooltipProvider,
+} from "@carbon/react";
+import { useFetcher } from "@remix-run/react";
+import { json } from "@vercel/remix";
+import { useEffect, useState } from "react";
+
+import { ModelUpload } from "~/components/ModelUpload";
+
+import type { ActionFunctionArgs } from "@vercel/remix";
+import { carbon } from "~/lib/carbon.server";
+
+export async function action({ request }: ActionFunctionArgs) {
+ const formData = await request.formData();
+ const file = formData.get("file");
+
+ if (!file || !(file instanceof File)) {
+ console.warn("No valid file provided in request");
+ return json({ error: "No file provided", data: null }, { status: 400 });
+ }
+
+ const upload = await carbon.uploadModel(file);
+
+ if (upload.error) {
+ return json({ error: upload.error.message, data: null }, { status: 500 });
+ }
+
+ return json(upload);
+}
+
+export default function Route() {
+ const [file, setFile] = useState<File | null>(null);
+ const [url, setUrl] = useState<string | null>(null);
+ const fetcher = useFetcher<typeof action>();
+
+ useEffect(() => {
+ if (fetcher.data?.error) {
+ toast.error(fetcher.data.error);
+ setFile(null);
+ setUrl(null);
+ }
+ if (fetcher.data?.data) {
+ setFile(null);
+ setUrl(fetcher.data.data.url);
+ }
+ }, [fetcher.data]);
+
+ const onFileChange = async (file: File | null) => {
+ setFile(file);
+
+ if (!file) return;
+
+ const formData = new FormData();
+ formData.append("file", file);
+
+ fetcher.submit(formData, {
+ method: "post",
+ encType: "multipart/form-data",
+ });
+ };
+
+ return (
+ <TooltipProvider>
+ <div className="flex h-screen w-screen flex-col items-center justify-center p-4">
+ <div className="max-w-xl text-center flex flex-col gap-8">
+ <Heading size="h1">Upload a 3D Model</Heading>
+ {url && (
+ <InputGroup>
+ <Input value={url} />
+ <InputLeftAddon className="border-none">
+ <Copy text={url} />
+ </InputLeftAddon>
+ </InputGroup>
+ )}
+ <ModelUpload file={file} onFileChange={onFileChange} />
+ </div>
+ </div>
+ </TooltipProvider>
+ );
}
diff --git a/examples/upload-3d-model/package.json b/examples/upload-3d-model/package.json
index c769ea2124..3f9863aada 100644
--- a/examples/upload-3d-model/package.json
+++ b/examples/upload-3d-model/package.json
@@ -2,6 +2,8 @@
"name": "upload-3d-model",
"type": "module",
"dependencies": {
+ "@carbon/react": "*",
+ "@carbon/utils": "*",
"@remix-run/dev": "~2.11.2",
"@remix-run/eslint-config": "~2.11.2",
"@remix-run/react": "~2.11.2",
@@ -12,7 +14,8 @@
"non.geist": "~1.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
- "react-dropzone": "^14.2.3"
+ "react-dropzone": "^14.2.3",
+ "react-icons": "^5.4.0"
},
"devDependencies": {
"@types/react": "^18.2.0",
diff --git a/examples/upload-3d-model/vite.config.ts b/examples/upload-3d-model/vite.config.ts
index a413ea02e2..8569d8cafc 100644
--- a/examples/upload-3d-model/vite.config.ts
+++ b/examples/upload-3d-model/vite.config.ts
@@ -22,7 +22,7 @@ export default defineConfig({
global: "globalThis",
},
ssr: {
- noExternal: ["tailwind-merge"],
+ noExternal: ["react-dropzone", "react-icons", "tailwind-merge"],
},
server: {
port: 5000,
diff --git a/package-lock.json b/package-lock.json
index 14008c1bfe..0d308b18f2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -487,7 +487,38 @@
}
}
},
+ "examples/upload-3d-model": {
+ "dependencies": {
+ "@carbon/react": "*",
+ "@carbon/utils": "*",
+ "@remix-run/dev": "~2.11.2",
+ "@remix-run/eslint-config": "~2.11.2",
+ "@remix-run/react": "~2.11.2",
+ "@remix-run/serve": "~2.11.2",
+ "@supabase/functions-js": "2.1.5",
+ "@supabase/supabase-js": "^2.40.0",
+ "@vercel/remix": "~2.11.2",
+ "non.geist": "~1.0.3",
+ "react": "^18.2.0",
+ "react-dom": "^18.2.0",
+ "react-dropzone": "^14.2.3",
+ "react-icons": "^5.4.0"
+ },
+ "devDependencies": {
+ "@types/react": "^18.2.0",
+ "@types/react-dom": "^18.2.0",
+ "autoprefixer": "10.4.14",
+ "cross-env": "7.0.3",
+ "nodemon": "^3.0.1",
+ "postcss": "^8.4.33",
+ "tailwindcss": "3.4.6",
+ "typescript": "^5.2.2",
+ "vite": "^5.3.3",
+ "vite-tsconfig-paths": "^4.3.2"
+ }
+ },
"examples/upload-rfq": {
+ "extraneous": true,
"dependencies": {
"@remix-run/dev": "~2.11.2",
"@remix-run/eslint-config": "~2.11.2",
@@ -34116,8 +34147,8 @@
"node": ">=10"
}
},
- "node_modules/upload-rfq": {
- "resolved": "examples/upload-rfq",
+ "node_modules/upload-3d-model": {
+ "resolved": "examples/upload-3d-model",
"link": true
},
"node_modules/uri-js": {