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.

fix(cad): unique thumbnail path so regeneration updates the UI (#1216)

Sid323429d29021
diff --git a/apps/erp/app/components/ItemThumnailUpload.tsx b/apps/erp/app/components/ItemThumnailUpload.tsx
index 90f496d4f7..51ae5fc069 100644
--- a/apps/erp/app/components/ItemThumnailUpload.tsx
+++ b/apps/erp/app/components/ItemThumnailUpload.tsx
@@ -4,7 +4,7 @@ import { Button, File as FileUpload, HStack, toast } from "@carbon/react";
import { Trans, useLingui } from "@lingui/react/macro";
import { nanoid } from "nanoid";
import type { ChangeEvent } from "react";
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useRef, useState } from "react";
import { LuRefreshCw } from "react-icons/lu";
import { useUser } from "~/hooks";
import { getPrivateUrl } from "~/utils/path";
@@ -73,11 +73,27 @@ export function ItemThumbnailUpload({
toast.success(t`Thumbnail removed`);
}, [carbon, itemId, modelId, t]);
+ const mountedRef = useRef(true);
+ useEffect(() => {
+ mountedRef.current = true;
+ return () => {
+ mountedRef.current = false;
+ };
+ }, []);
+
const [isRegenerating, setIsRegenerating] = useState(false);
const onRegenerate = useCallback(async () => {
- if (!modelId) return;
+ if (!modelId || !carbon) return;
setIsRegenerating(true);
try {
+ // Snapshot the current model thumbnail so we can detect the new one.
+ const before = await carbon
+ .from("modelUpload")
+ .select("thumbnailPath")
+ .eq("id", modelId)
+ .maybeSingle();
+ const beforePath = before.data?.thumbnailPath ?? null;
+
const body = new FormData();
body.append("modelUploadId", modelId);
const res = await fetch("/api/model/thumbnail", {
@@ -85,14 +101,34 @@ export function ItemThumbnailUpload({
body
});
if (!res.ok) throw new Error(`thumbnail ${res.status}`);
- // The render is async — the new thumbnail lands after the job runs.
- toast.success(t`Regenerating thumbnail…`);
+ toast.info(t`Regenerating thumbnail…`);
+
+ // The render runs async in the background. Poll for the fresh path (unique
+ // per generation) and swap the image in when it lands; bounded so it never
+ // spins forever.
+ const deadline = Date.now() + 90_000;
+ while (mountedRef.current && Date.now() < deadline) {
+ await new Promise((r) => setTimeout(r, 3000));
+ if (!mountedRef.current) return;
+ const cur = await carbon
+ .from("modelUpload")
+ .select("thumbnailPath")
+ .eq("id", modelId)
+ .maybeSingle();
+ const curPath = cur.data?.thumbnailPath ?? null;
+ if (curPath && curPath !== beforePath) {
+ setThumbnailPath(getPrivateUrl(curPath));
+ toast.success(t`Thumbnail updated`);
+ return;
+ }
+ }
+ if (mountedRef.current) toast.info(t`Thumbnail is still generating`);
} catch {
- toast.error(t`Failed to regenerate thumbnail`);
+ if (mountedRef.current) toast.error(t`Failed to regenerate thumbnail`);
} finally {
- setIsRegenerating(false);
+ if (mountedRef.current) setIsRegenerating(false);
}
- }, [modelId, t]);
+ }, [modelId, carbon, t]);
const onFileChange = useCallback(
async (e: ChangeEvent<HTMLInputElement>) => {
diff --git a/packages/jobs/src/inngest/functions/tasks/model-thumbnail.ts b/packages/jobs/src/inngest/functions/tasks/model-thumbnail.ts
index 3abb0e88ac..63a9462a62 100644
--- a/packages/jobs/src/inngest/functions/tasks/model-thumbnail.ts
+++ b/packages/jobs/src/inngest/functions/tasks/model-thumbnail.ts
@@ -5,6 +5,7 @@ import {
SUPABASE_URL,
VERCEL_URL
} from "@carbon/env";
+import { nanoid } from "nanoid";
import { inngest } from "../../client";
export const modelThumbnailFunction = inngest.createFunction(
@@ -34,6 +35,15 @@ export const modelThumbnailFunction = inngest.createFunction(
logger.info("Starting model-thumbnail task", { payload: event.data });
const client = getCarbonServiceRole();
+ // The previous thumbnail's path — deleted after the new one lands so a
+ // regeneration doesn't leak an orphan (the filename is now unique per run).
+ const previous = await client
+ .from("modelUpload")
+ .select("thumbnailPath")
+ .eq("id", modelId)
+ .maybeSingle();
+ const previousPath = previous.data?.thumbnailPath ?? null;
+
const url = getModelUrl(modelId);
const imageUrl = `${SUPABASE_URL}/functions/v1/thumbnail`;
@@ -55,7 +65,12 @@ export const modelThumbnailFunction = inngest.createFunction(
type: "image/png"
});
- const fileName = `${modelId}.png`;
+ // Unique filename per generation: the preview is served from a STABLE
+ // proxy URL (`/file/preview/...`), so reusing `{modelId}.png` would leave a
+ // regenerated thumbnail showing the browser-cached old image. A fresh path
+ // changes the URL (cache-bust) and the stored `thumbnailPath` value (so the
+ // UI actually re-renders).
+ const fileName = `${modelId}-${nanoid(8)}.png`;
const thumbnailFile = new File([blob], fileName, {
type: "image/png"
});
@@ -74,6 +89,7 @@ export const modelThumbnailFunction = inngest.createFunction(
if (error) {
logger.error("Failed to upload thumbnail", { error });
+ throw new Error(`Failed to upload thumbnail: ${error.message}`);
}
const result = await client
@@ -87,6 +103,17 @@ export const modelThumbnailFunction = inngest.createFunction(
logger.error("Failed to update thumbnail path", {
error: result.error
});
+ throw new Error(
+ `Failed to update thumbnail path: ${result.error.message}`
+ );
+ }
+
+ // Drop the superseded thumbnail (best-effort — never fail the run over it).
+ if (previousPath && previousPath !== data?.path) {
+ await client.storage
+ .from("private")
+ .remove([previousPath])
+ .catch(() => undefined);
}
});
}
diff --git a/packages/locale/locales/de/erp.po b/packages/locale/locales/de/erp.po
index 8d60740a11..24fc3484f6 100644
--- a/packages/locale/locales/de/erp.po
+++ b/packages/locale/locales/de/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Bis {0}"
msgid "Thumbnail"
msgstr "Miniaturansicht"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Vorschaubild entfernt"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Miniaturansicht hochgeladen"
diff --git a/packages/locale/locales/en/erp.po b/packages/locale/locales/en/erp.po
index 798c40e9c4..677c564f60 100644
--- a/packages/locale/locales/en/erp.po
+++ b/packages/locale/locales/en/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Through {0}"
msgid "Thumbnail"
msgstr "Thumbnail"
+msgid "Thumbnail is still generating"
+msgstr "Thumbnail is still generating"
+
msgid "Thumbnail removed"
msgstr "Thumbnail removed"
+msgid "Thumbnail updated"
+msgstr "Thumbnail updated"
+
msgid "Thumbnail uploaded"
msgstr "Thumbnail uploaded"
diff --git a/packages/locale/locales/es/erp.po b/packages/locale/locales/es/erp.po
index 8ff1bfd508..58a651674e 100644
--- a/packages/locale/locales/es/erp.po
+++ b/packages/locale/locales/es/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Hasta {0}"
msgid "Thumbnail"
msgstr "Miniatura"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Miniatura eliminada"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Miniatura cargada"
diff --git a/packages/locale/locales/fr/erp.po b/packages/locale/locales/fr/erp.po
index 0100e3baeb..7cc7423845 100644
--- a/packages/locale/locales/fr/erp.po
+++ b/packages/locale/locales/fr/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Jusqu'au {0}"
msgid "Thumbnail"
msgstr "Miniature"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Miniature supprimée"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Miniature téléchargée"
diff --git a/packages/locale/locales/hi/erp.po b/packages/locale/locales/hi/erp.po
index 207d530f27..200bce3f7a 100644
--- a/packages/locale/locales/hi/erp.po
+++ b/packages/locale/locales/hi/erp.po
@@ -12988,9 +12988,15 @@ msgstr "{0} तक"
msgid "Thumbnail"
msgstr "थंबनेल"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "थंबनेल हटाया गया"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "थंबनेल अपलोड किया गया"
diff --git a/packages/locale/locales/it/erp.po b/packages/locale/locales/it/erp.po
index 48c4b8f0a0..7ccb3851e3 100644
--- a/packages/locale/locales/it/erp.po
+++ b/packages/locale/locales/it/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Fino a {0}"
msgid "Thumbnail"
msgstr "Miniatura"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Miniatura rimossa"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Miniatura caricata"
diff --git a/packages/locale/locales/ja/erp.po b/packages/locale/locales/ja/erp.po
index 75e3ea503f..7a0bed889e 100644
--- a/packages/locale/locales/ja/erp.po
+++ b/packages/locale/locales/ja/erp.po
@@ -12988,9 +12988,15 @@ msgstr "{0}まで"
msgid "Thumbnail"
msgstr "サムネイル"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "サムネイルが削除されました"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "サムネイルがアップロードされました"
diff --git a/packages/locale/locales/ko/erp.po b/packages/locale/locales/ko/erp.po
index 8a04e53e9b..3dda073836 100644
--- a/packages/locale/locales/ko/erp.po
+++ b/packages/locale/locales/ko/erp.po
@@ -12988,9 +12988,15 @@ msgstr "{0}까지"
msgid "Thumbnail"
msgstr "썸네일"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "썸네일 삭제됨"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "썸네일 업로드됨"
diff --git a/packages/locale/locales/pl/erp.po b/packages/locale/locales/pl/erp.po
index f4a5523b28..b16ed40f02 100644
--- a/packages/locale/locales/pl/erp.po
+++ b/packages/locale/locales/pl/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Do {0}"
msgid "Thumbnail"
msgstr "Miniatura"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Miniatura usunięta"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Miniatura przesłana"
diff --git a/packages/locale/locales/pt/erp.po b/packages/locale/locales/pt/erp.po
index 8ae09e6f3a..a095d35856 100644
--- a/packages/locale/locales/pt/erp.po
+++ b/packages/locale/locales/pt/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Até {0}"
msgid "Thumbnail"
msgstr "Miniatura"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Miniatura removida"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Miniatura enviada"
diff --git a/packages/locale/locales/ru/erp.po b/packages/locale/locales/ru/erp.po
index 6423479388..024f310b50 100644
--- a/packages/locale/locales/ru/erp.po
+++ b/packages/locale/locales/ru/erp.po
@@ -12988,9 +12988,15 @@ msgstr "До {0}"
msgid "Thumbnail"
msgstr "Миниатюра"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Миниатюра удалена"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Миниатюра загружена"
diff --git a/packages/locale/locales/tr/erp.po b/packages/locale/locales/tr/erp.po
index fecac5304f..8ed18ef388 100644
--- a/packages/locale/locales/tr/erp.po
+++ b/packages/locale/locales/tr/erp.po
@@ -12988,9 +12988,15 @@ msgstr "Şu tarihe kadar {0}"
msgid "Thumbnail"
msgstr "Küçük Resim"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "Küçük resim kaldırıldı"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "Küçük resim yüklendi"
diff --git a/packages/locale/locales/zh/erp.po b/packages/locale/locales/zh/erp.po
index 1ab20e01b0..0eece696d8 100644
--- a/packages/locale/locales/zh/erp.po
+++ b/packages/locale/locales/zh/erp.po
@@ -12988,9 +12988,15 @@ msgstr "至 {0}"
msgid "Thumbnail"
msgstr "缩略图"
+msgid "Thumbnail is still generating"
+msgstr ""
+
msgid "Thumbnail removed"
msgstr "缩略图已移除"
+msgid "Thumbnail updated"
+msgstr ""
+
msgid "Thumbnail uploaded"
msgstr "缩略图已上传"