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
fix: add local storage and logging for training panels
Gaurav Bhatt5c49cb85c01b
diff --git a/apps/erp/app/hooks/useTrainingPanel.ts b/apps/erp/app/hooks/useTrainingPanel.tsindex 7228c5bc19..18fca70587 100644--- a/apps/erp/app/hooks/useTrainingPanel.ts+++ b/apps/erp/app/hooks/useTrainingPanel.ts@@ -1,33 +1,75 @@-import { useMemo } from "react";+import { useEffect, useRef } from "react";import { useFetcher, useLocation } from "react-router";import { path } from "~/utils/path";import { getTrainingForPath, getTrainingKey } from "~/utils/training";import { useUser } from "./useUser";const FLAG_PREFIX = "training:";+const MAX_IMPRESSIONS = 3;++function safeLocalGet(key: string): string | null {+ try {+ return localStorage.getItem(key);+ } catch {+ return null;+ }+}++function safeLocalSet(key: string, value: string) {+ try {+ localStorage.setItem(key, value);+ } catch {}+}export function useTrainingPanel() {- const { pathname } = useLocation();+ const { pathname, key: locationKey } = useLocation();const { flags } = useUser();const fetcher = useFetcher({ key: "training-dismiss" });+ const lastCountedKey = useRef<string | null>(null);- const training = useMemo(() => getTrainingForPath(pathname), [pathname]);- const trainingKey = useMemo(() => getTrainingKey(pathname), [pathname]);-+ const training = getTrainingForPath(pathname);+ const trainingKey = getTrainingKey(pathname);const flagKey = trainingKey ? `${FLAG_PREFIX}${trainingKey}` : null;- // Optimistic: check if there's a pending dismiss for THIS flagconst pendingDismissFlag = fetcher.formData?.get("flag") as string | null;const isPendingDismiss = pendingDismissFlag === flagKey;-- const isDismissed = flagKey- ? isPendingDismiss || flags[flagKey] === true+ const isDbDismissed = flagKey ? flags[flagKey] === true : false;+ const isLocalDismissed = flagKey+ ? safeLocalGet(`training_dismissed_${flagKey}`) === "true": false;- const isOpen = !!training && !!flagKey && !isDismissed;+ const impressionCount = flagKey+ ? parseInt(safeLocalGet(`training_impressions_${flagKey}`) ?? "0", 10)+ : 0;+ const tooManyImpressions = impressionCount >= MAX_IMPRESSIONS;++ const isDismissed = isPendingDismiss || isDbDismissed || isLocalDismissed;+ const isOpen = !!training && !!flagKey && !isDismissed && !tooManyImpressions;++ // Count impressions per navigation (locationKey deduplicates StrictMode remounts)+ useEffect(() => {+ if (isOpen && flagKey && lastCountedKey.current !== locationKey) {+ lastCountedKey.current = locationKey;+ const key = `training_impressions_${flagKey}`;+ const count = parseInt(safeLocalGet(key) ?? "0", 10);+ safeLocalSet(key, String(count + 1));+ }+ }, [isOpen, flagKey, locationKey]);++ // Once the impression cap is hit, sync the dismiss to DB on the next visit+ useEffect(() => {+ if (tooManyImpressions && flagKey && !isDismissed) {+ safeLocalSet(`training_dismissed_${flagKey}`, "true");+ fetcher.submit(+ { intent: "flag", flag: flagKey, value: "true" },+ { method: "POST", action: path.to.acknowledge }+ );+ }+ }, [tooManyImpressions, flagKey, isDismissed, fetcher]);const dismiss = () => {if (!flagKey) return;+ safeLocalSet(`training_dismissed_${flagKey}`, "true");fetcher.submit({ intent: "flag", flag: flagKey, value: "true" },{ method: "POST", action: path.to.acknowledge }diff --git a/apps/erp/app/routes/x+/acknowledge.tsx b/apps/erp/app/routes/x+/acknowledge.tsxindex f4d15c9b76..32f38959e1 100644--- a/apps/erp/app/routes/x+/acknowledge.tsx+++ b/apps/erp/app/routes/x+/acknowledge.tsx@@ -42,12 +42,20 @@ export async function action({ request }: ActionFunctionArgs) {const { flag, value } = parsed.data;- const { data: user } = await client+ const { data: user, error: readError } = await client.from("user").select("flags").eq("id", userId).single();+ if (readError) {+ console.error(+ `[acknowledge] Failed to read flags for user ${userId}:`,+ readError+ );+ return { success: false, message: "Failed to read user flags" };+ }+const currentFlags = (user?.flags as Record<string, boolean> | null) ?? {};const updatedFlags = { ...currentFlags, [flag]: value };@@ -57,7 +65,10 @@ export async function action({ request }: ActionFunctionArgs) {.eq("id", userId);if (updateResult.error) {- console.error(updateResult.error);+ console.error(+ `[acknowledge] Failed to write flag "${flag}" for user ${userId}:`,+ updateResult.error+ );return { success: false, message: "Failed to update flag" };}