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: flash red or green on complete/scrap in mes

Brad Barbin4dded4cab4af
diff --git a/apps/erp/app/routes/api+/kanban.complete.$id.tsx b/apps/erp/app/routes/api+/kanban.complete.$id.tsx
index 0962a2a162..8a951c365a 100644
--- a/apps/erp/app/routes/api+/kanban.complete.$id.tsx
+++ b/apps/erp/app/routes/api+/kanban.complete.$id.tsx
@@ -1,12 +1,8 @@
import { notFound } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import type { Database } from "@carbon/database";
-import { Loading } from "@carbon/react";
-import { Await, useLoaderData } from "@remix-run/react";
import type { SupabaseClient } from "@supabase/supabase-js";
-import { defer, type LoaderFunctionArgs } from "@vercel/remix";
-import { Suspense } from "react";
-import { Redirect } from "~/components/Redirect";
+import { json, redirect, type LoaderFunctionArgs } from "@vercel/remix";
import { getKanban } from "~/modules/inventory";
import { getActiveJobOperationByJobId } from "~/modules/production";
import { path } from "~/utils/path";
@@ -60,24 +56,11 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const { id } = params;
if (!id) throw notFound("id not found");
- return defer(await handleKanbanComplete({ client, companyId, id }));
-}
+ const result = await handleKanbanComplete({ client, companyId, id });
-export default function KanbanRedirectRoute() {
- const promise = useLoaderData<typeof loader>();
+ if (result.error || !result.data) {
+ return json({ error: result.error }, { status: 400 });
+ }
- return (
- <div className="flex h-screen w-screen items-center justify-center">
- <Suspense fallback={<Loading className="size-8" isLoading />}>
- <Await resolve={promise}>
- {(resolvedPromise) => {
- if (resolvedPromise.error) {
- return <div>{resolvedPromise.error}</div>;
- }
- return <Redirect path={resolvedPromise?.data ?? ""} />;
- }}
- </Await>
- </Suspense>
- </div>
- );
+ throw redirect(result.data);
}
diff --git a/apps/mes/app/components/FlashOverlay.tsx b/apps/mes/app/components/FlashOverlay.tsx
new file mode 100644
index 0000000000..7ffbac6b7d
--- /dev/null
+++ b/apps/mes/app/components/FlashOverlay.tsx
@@ -0,0 +1,73 @@
+"use client";
+
+import { useEffect, useRef, useState } from "react";
+
+type FlashVariant = "success" | "error";
+
+class FlashOverlayManager {
+ private listeners: Set<(variant: FlashVariant) => void> = new Set();
+
+ flash(variant: FlashVariant) {
+ this.listeners.forEach((listener) => listener(variant));
+ }
+
+ subscribe(listener: (variant: FlashVariant) => void) {
+ this.listeners.add(listener);
+ return () => this.listeners.delete(listener);
+ }
+}
+
+export const flashOverlay = new FlashOverlayManager();
+
+export function FlashOverlay() {
+ const [activeVariant, setActiveVariant] = useState<FlashVariant | null>(null);
+ const [isVisible, setIsVisible] = useState(false);
+ const audioRef = useRef<HTMLAudioElement | null>(null);
+
+ useEffect(() => {
+ let timer: NodeJS.Timeout | null = null;
+
+ const unsubscribe = flashOverlay.subscribe((variant) => {
+ setActiveVariant(variant);
+ setIsVisible(true);
+
+ // Play victory sound for success
+ if (variant === "success") {
+ if (!audioRef.current) {
+ audioRef.current = new Audio("/victory.mp3");
+ }
+ audioRef.current.currentTime = 0;
+ audioRef.current.play().catch(() => {
+ // Ignore errors if audio can't play
+ });
+ }
+
+ if (timer) clearTimeout(timer);
+ timer = setTimeout(() => {
+ setIsVisible(false);
+ setActiveVariant(null);
+ }, 300);
+ });
+
+ return () => {
+ if (timer) clearTimeout(timer);
+ unsubscribe();
+ };
+ }, []);
+
+ if (!activeVariant) return null;
+
+ const gradientColor =
+ activeVariant === "success" ? "34, 197, 94" : "239, 68, 68";
+
+ return (
+ <div
+ className={`fixed inset-0 pointer-events-none z-[9999] transition-opacity duration-300 ${
+ isVisible ? "opacity-100" : "opacity-0"
+ }`}
+ style={{
+ background: `radial-gradient(circle, transparent 20%, rgba(${gradientColor}, 0.6) 100%)`,
+ }}
+ />
+ );
+}
diff --git a/apps/mes/app/root.tsx b/apps/mes/app/root.tsx
index 325f5e83ac..00d6a77922 100644
--- a/apps/mes/app/root.tsx
+++ b/apps/mes/app/root.tsx
@@ -1,7 +1,7 @@
import { CONTROLLED_ENVIRONMENT, error, getBrowserEnv } from "@carbon/auth";
import { getSessionFlash } from "@carbon/auth/session.server";
import { validator } from "@carbon/form";
-import { Button, Heading, toast } from "@carbon/react";
+import { Button, Heading, toast, Toaster } from "@carbon/react";
import type { Theme } from "@carbon/utils";
import { modeValidator, themes } from "@carbon/utils";
import {
@@ -22,6 +22,7 @@ import type {
} from "@vercel/remix";
import { json } from "@vercel/remix";
import React, { useEffect } from "react";
+import { FlashOverlay, flashOverlay } from "~/components/FlashOverlay";
import { getMode, setMode } from "~/services/mode.server";
import Background from "~/styles/background.css?url";
import NProgress from "~/styles/nprogress.css?url";
@@ -161,7 +162,7 @@ function Document({
</head>
<body className="h-full bg-background antialiased selection:bg-primary/10 selection:text-primary">
{children}
-
+ <Toaster position="bottom-right" visibleToasts={5} />
<ScrollRestoration />
<Scripts />
{!CONTROLLED_ENVIRONMENT && <Analytics />}
@@ -185,12 +186,20 @@ export default function App() {
}
}, [result]);
+ /* Flash Overlay */
+ useEffect(() => {
+ if (result?.flash) {
+ flashOverlay.flash(result.flash);
+ }
+ }, [result?.flash]);
+
/* Dark/Light Mode */
const mode = useMode();
return (
<Document mode={mode} theme={theme}>
<Outlet />
+ <FlashOverlay />
<script
dangerouslySetInnerHTML={{
__html: `window.env = ${JSON.stringify(env)}`,
diff --git a/apps/mes/app/routes/x+/adjustment.tsx b/apps/mes/app/routes/x+/adjustment.tsx
index d646fcef99..958e0fd3bb 100644
--- a/apps/mes/app/routes/x+/adjustment.tsx
+++ b/apps/mes/app/routes/x+/adjustment.tsx
@@ -30,7 +30,8 @@ export async function action({ request }: ActionFunctionArgs) {
if (itemLedger.error) {
const flashMessage =
- itemLedger.error === "Insufficient quantity for negative adjustment"
+ itemLedger.error.message ===
+ "Insufficient quantity for negative adjustment"
? "Insufficient quantity for negative adjustment"
: "Failed to create manual inventory adjustment";
diff --git a/apps/mes/app/routes/x+/complete.tsx b/apps/mes/app/routes/x+/complete.tsx
index 8887b42974..8c11b389d3 100644
--- a/apps/mes/app/routes/x+/complete.tsx
+++ b/apps/mes/app/routes/x+/complete.tsx
@@ -213,7 +213,10 @@ export async function action({ request }: ActionFunctionArgs) {
return json(
insertProduction.data,
- await flash(request, success("Production quantity recorded successfully"))
+ await flash(request, {
+ ...success("Production quantity recorded successfully"),
+ flash: "success",
+ })
);
}
}
diff --git a/apps/mes/app/routes/x+/end.$operationId.tsx b/apps/mes/app/routes/x+/end.$operationId.tsx
index cd86027429..aea93edba4 100644
--- a/apps/mes/app/routes/x+/end.$operationId.tsx
+++ b/apps/mes/app/routes/x+/end.$operationId.tsx
@@ -244,6 +244,9 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
throw redirect(
path.to.operation(operationId),
- await flash(request, success("Successfully completed part"))
+ await flash(request, {
+ ...success("Successfully completed part"),
+ flash: "success",
+ })
);
}
diff --git a/apps/mes/app/routes/x+/scrap.tsx b/apps/mes/app/routes/x+/scrap.tsx
index 540505fe2f..07f37da0cf 100644
--- a/apps/mes/app/routes/x+/scrap.tsx
+++ b/apps/mes/app/routes/x+/scrap.tsx
@@ -62,6 +62,9 @@ export async function action({ request }: ActionFunctionArgs) {
return json(
insertScrap.data,
- await flash(request, success("Scrap quantity recorded successfully"))
+ await flash(request, {
+ ...success("Scrap quantity recorded successfully"),
+ flash: "error",
+ })
);
}
diff --git a/apps/mes/public/victory.mp3 b/apps/mes/public/victory.mp3
new file mode 100644
index 0000000000..fdfa4d4a45
Binary files /dev/null and b/apps/mes/public/victory.mp3 differ
diff --git a/packages/auth/src/services/session.server.ts b/packages/auth/src/services/session.server.ts
index 02fcdc0bf4..bce3c74894 100644
--- a/packages/auth/src/services/session.server.ts
+++ b/packages/auth/src/services/session.server.ts
@@ -79,6 +79,9 @@ export async function flash(request: Request, result: Result) {
if (typeof result.success === "boolean") {
session.flash("success", result.success);
session.flash("message", result.message);
+ if (result.flash) {
+ session.flash("flash", result.flash);
+ }
}
return {
@@ -112,6 +115,7 @@ export async function getSessionFlash(request: Request) {
const result: Result = {
success: session.get("success") === true,
message: session.get("message"),
+ flash: session.get("flash") as "success" | "error" | undefined,
};
if (!result.message) return null;
diff --git a/packages/auth/src/types.ts b/packages/auth/src/types.ts
index 069e1611ef..e69d3e73da 100644
--- a/packages/auth/src/types.ts
+++ b/packages/auth/src/types.ts
@@ -31,4 +31,5 @@ export type Permission = {
export type Result = {
success: boolean;
message?: string;
+ flash?: "success" | "error";
};