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(onboarding): persist dark mode on prod + optimistic checklist toggles

Sidwebworksa4dbf9a2ab10
diff --git a/apps/erp/app/services/mode.server.ts b/apps/erp/app/services/mode.server.ts
index d9985961f4..2c81e0116f 100644
--- a/apps/erp/app/services/mode.server.ts
+++ b/apps/erp/app/services/mode.server.ts
@@ -14,17 +14,18 @@ export function getMode(request: Request): Mode | null {
}
export function setMode(mode: Mode | "system") {
- if (mode === "system") {
- return cookie.serialize(cookieName, "", { path: "/", maxAge: -1 });
- } else {
- const cookieOptions: cookie.SerializeOptions = {
- path: "/",
- maxAge: 31536000
- };
+ const cookieDomain = getCookieDomain(DOMAIN);
+ const cookieOptions: cookie.SerializeOptions = {
+ path: "/",
+ sameSite: "lax",
+ secure: !!cookieDomain,
+ domain: cookieDomain,
+ maxAge: mode === "system" ? -1 : 31536000
+ };
- const cookieDomain = getCookieDomain(DOMAIN);
- if (cookieDomain) cookieOptions.domain = cookieDomain;
-
- return cookie.serialize(cookieName, mode, cookieOptions);
- }
+ return cookie.serialize(
+ cookieName,
+ mode === "system" ? "" : mode,
+ cookieOptions
+ );
}
diff --git a/apps/erp/app/services/theme.server.ts b/apps/erp/app/services/theme.server.ts
index a5e57aca8f..937b348cba 100644
--- a/apps/erp/app/services/theme.server.ts
+++ b/apps/erp/app/services/theme.server.ts
@@ -22,13 +22,14 @@ export function getTheme(request: Request): Theme {
}
export function setTheme(theme: string) {
+ const cookieDomain = getCookieDomain(DOMAIN);
const cookieOptions: cookie.SerializeOptions = {
path: "/",
+ sameSite: "lax",
+ secure: !!cookieDomain,
+ domain: cookieDomain,
maxAge: 31536000
};
- const cookieDomain = getCookieDomain(DOMAIN);
- if (cookieDomain) cookieOptions.domain = cookieDomain;
-
return cookie.serialize(cookieName, theme, cookieOptions);
}
diff --git a/apps/mes/app/services/mode.server.ts b/apps/mes/app/services/mode.server.ts
index c8b3579151..2c81e0116f 100644
--- a/apps/mes/app/services/mode.server.ts
+++ b/apps/mes/app/services/mode.server.ts
@@ -1,4 +1,4 @@
-import { DOMAIN } from "@carbon/auth";
+import { DOMAIN, getCookieDomain } from "@carbon/auth";
import type { Mode } from "@carbon/utils";
import * as cookie from "cookie";
@@ -14,17 +14,18 @@ export function getMode(request: Request): Mode | null {
}
export function setMode(mode: Mode | "system") {
- if (mode === "system") {
- return cookie.serialize(cookieName, "", { path: "/", maxAge: -1 });
- } else {
- const cookieOptions: cookie.SerializeOptions = {
- path: "/",
- maxAge: 31536000
- };
+ const cookieDomain = getCookieDomain(DOMAIN);
+ const cookieOptions: cookie.SerializeOptions = {
+ path: "/",
+ sameSite: "lax",
+ secure: !!cookieDomain,
+ domain: cookieDomain,
+ maxAge: mode === "system" ? -1 : 31536000
+ };
- if (DOMAIN && !DOMAIN.startsWith("localhost")) {
- cookieOptions.domain = DOMAIN;
- }
- return cookie.serialize(cookieName, mode, cookieOptions);
- }
+ return cookie.serialize(
+ cookieName,
+ mode === "system" ? "" : mode,
+ cookieOptions
+ );
}
diff --git a/apps/mes/app/services/theme.server.ts b/apps/mes/app/services/theme.server.ts
index 28887f48bb..43b8fcd486 100644
--- a/apps/mes/app/services/theme.server.ts
+++ b/apps/mes/app/services/theme.server.ts
@@ -1,3 +1,4 @@
+import { DOMAIN, getCookieDomain } from "@carbon/auth";
import * as cookie from "cookie";
const cookieName = "theme";
@@ -21,5 +22,12 @@ export function getTheme(request: Request): Theme {
}
export function setTheme(theme: string) {
- return cookie.serialize(cookieName, theme, { path: "/", maxAge: 31536000 });
+ const cookieDomain = getCookieDomain(DOMAIN);
+ return cookie.serialize(cookieName, theme, {
+ path: "/",
+ sameSite: "lax",
+ secure: !!cookieDomain,
+ domain: cookieDomain,
+ maxAge: 31536000
+ });
}
diff --git a/packages/onboarding/src/ui/state/hubStore.ts b/packages/onboarding/src/ui/state/hubStore.ts
index 95ea15ab21..b5c0547cbe 100644
--- a/packages/onboarding/src/ui/state/hubStore.ts
+++ b/packages/onboarding/src/ui/state/hubStore.ts
@@ -4,9 +4,16 @@
// hydrates this store from fresh loader data (`setData`). Components read slices
// via selector hooks (so a change to one slice doesn't re-render every consumer)
// and write ONLY through `dispatch`, which round-trips to the `/state` server
-// action; Supabase Realtime then revalidates the loader and the cycle repeats.
-// There is no optimistic local mutation and no client-owned domain state here —
-// `setData` is the only writer of business fields, and only the provider calls it.
+// action (`serverDispatch`); Supabase Realtime then revalidates the loader and
+// the cycle repeats. `setData` is the authoritative writer of business fields —
+// only the provider calls it, and it always rebuilds from the server snapshot.
+//
+// One exception, for responsiveness: `dispatch` applies an OPTIMISTIC patch to
+// `checkMap` for `setCheck` toggles (checkboxes/gates) before the round-trip, so
+// a tick reflects instantly instead of after the full submit→revalidate cycle.
+// This is self-reconciling — the next `setData` rebuilds `checkMap` from server
+// truth, so a rejected or differing write reverts on its own. No other intent is
+// patched locally; everything else waits for the loader.
//
// `checkMap` / `fieldMap` are cheap derived lookups rebuilt on hydration so view
// components do O(1) reads instead of each rebuilding a Map per render.
@@ -57,7 +64,10 @@ export type ResolveScreenUrl = (appKey: string) => string | undefined;
export interface HubState extends HubData, HubFlags {
checkMap: Map<string, string>;
fieldMap: Map<string, string>;
+ // Public write path: optimistic checkMap patch (setCheck only) + round-trip.
dispatch: (m: HubMutation) => void;
+ // The raw server round-trip the route injects; `dispatch` wraps it.
+ serverDispatch: (m: HubMutation) => void;
resolveScreenUrl: ResolveScreenUrl;
// Resolve a training video key to a watch URL (academy or video), via the ERP
// trainingConfig the route injects. Same shape as resolveScreenUrl.
@@ -66,7 +76,7 @@ export interface HubState extends HubData, HubFlags {
setData: (
data: HubData,
flags: HubFlags,
- dispatch: (m: HubMutation) => void,
+ serverDispatch: (m: HubMutation) => void,
resolveScreenUrl: ResolveScreenUrl,
resolveVideoUrl: ResolveScreenUrl
) => void;
@@ -98,21 +108,32 @@ export const HUB_INITIAL: HubData & HubFlags = {
export function createHubStore(initial: Partial<HubData & HubFlags> = {}) {
const seed = { ...HUB_INITIAL, ...initial };
- return createStore<HubState>()((set) => ({
+ return createStore<HubState>()((set, get) => ({
...seed,
checkMap: stateMap(seed.checkStates),
fieldMap: fieldMap(seed.fieldValues),
- // Real dispatch + resolvers are injected by the provider via setData on mount.
- dispatch: () => undefined,
+ // Stable wrapper, created once. Optimistically reflects a checkbox/gate toggle
+ // in checkMap so the UI updates instantly, then round-trips. The next setData
+ // rebuilds checkMap from server truth, reconciling (or reverting) the patch.
+ dispatch: (m) => {
+ if (m.intent === "setCheck") {
+ const checkMap = new Map(get().checkMap);
+ checkMap.set(m.itemKey, m.value);
+ set({ checkMap });
+ }
+ get().serverDispatch(m);
+ },
+ // Real server dispatch + resolvers are injected by the provider via setData.
+ serverDispatch: () => undefined,
resolveScreenUrl: () => undefined,
resolveVideoUrl: () => undefined,
- setData: (data, flags, dispatch, resolveScreenUrl, resolveVideoUrl) =>
+ setData: (data, flags, serverDispatch, resolveScreenUrl, resolveVideoUrl) =>
set({
...data,
...flags,
checkMap: stateMap(data.checkStates),
fieldMap: fieldMap(data.fieldValues),
- dispatch,
+ serverDispatch,
resolveScreenUrl,
resolveVideoUrl
})