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: fix changes to realtime data

Brad Barbin4365d1632cf5
diff --git a/apps/carbon/app/hooks/useNanoStore.ts b/apps/carbon/app/hooks/useNanoStore.ts
index 6865e2a78e..480fccabd9 100644
--- a/apps/carbon/app/hooks/useNanoStore.ts
+++ b/apps/carbon/app/hooks/useNanoStore.ts
@@ -3,20 +3,20 @@ import idb from "localforage";
import { type WritableAtom } from "nanostores";
import { useCallback } from "react";
-export function useNanoStore<T>(atom: WritableAtom<T>, localKey?: string) {
+export function useNanoStore<T>(atom: WritableAtom<T>, idbKey?: string) {
const value = useStore(atom);
const set = useCallback(
- (value: T | ((current: T) => T)) => {
+ (value: T | ((current: T) => T), initial = false) => {
if (typeof value === "function") {
atom.set((value as (current: T) => T)(atom.get()));
- if (localKey) idb.setItem(localKey, atom.get());
+ if (idbKey && !initial) idb.setItem(idbKey, atom.get());
} else {
atom.set(value);
- if (localKey) idb.setItem(localKey, value);
+ if (idbKey && !initial) idb.setItem(idbKey, value);
}
},
- [atom, localKey]
+ [atom, idbKey]
);
return [value, set] as const;
diff --git a/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx b/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
index ad7b2e9c91..9b969ea9dc 100644
--- a/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
+++ b/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
@@ -1,37 +1,33 @@
import idb from "localforage";
import { useEffect } from "react";
-import { flushSync } from "react-dom";
import { useSupabase } from "~/lib/supabase";
import { useCustomers, useParts, useSuppliers } from "~/stores";
+import type { Part } from "~/stores/parts";
+import type { ListItem } from "~/types";
let hydrated = false;
+let hydratedFromServer = false;
const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
const { supabase, accessToken } = useSupabase();
- const [, setParts] = useParts([]);
+ const [, setParts] = useParts();
const [, setSuppliers] = useSuppliers();
const [, setCustomers] = useCustomers();
const hydrate = async () => {
if (!hydrated) {
- const localData = await Promise.all([
- idb.getItem("customers"),
- idb.getItem("parts"),
- idb.getItem("suppliers"),
- ]);
-
hydrated = true;
- if (localData.every((data) => data !== null)) {
- const [localCustomers, localParts, localSuppliers] = localData;
-
- setCustomers(localCustomers);
- setParts(localParts);
- setSuppliers(localSuppliers);
-
- flushSync(() => setLoading(false));
- }
+ idb.getItem("customers").then((data) => {
+ if (data && !hydratedFromServer) setCustomers(data as ListItem[], true);
+ });
+ idb.getItem("parts").then((data) => {
+ if (data && !hydratedFromServer) setParts(data as Part[], true);
+ });
+ idb.getItem("suppliers").then((data) => {
+ if (data && !hydratedFromServer) setSuppliers(data as ListItem[], true);
+ });
}
if (!supabase || !accessToken) return;
@@ -51,11 +47,11 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
throw new Error("Failed to fetch core data");
}
+ hydratedFromServer = true;
+
setParts(parts.data ?? []);
setSuppliers(suppliers.data ?? []);
setCustomers(customers.data ?? []);
-
- setLoading(false);
};
useEffect(() => {
@@ -220,21 +216,4 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
return <>{children}</>;
};
-// function Loading() {
-// return (
-// <div className="flex flex-col h-screen w-screen items-center justify-center">
-// <img
-// src="/carbon-logo-dark.png"
-// alt="Carbon Logo"
-// className="block dark:hidden max-w-[100px]"
-// />
-// <img
-// src="/carbon-logo-light.png"
-// alt="Carbon Logo"
-// className="hidden dark:block max-w-[100px]"
-// />
-// </div>
-// );
-// }
-
export default RealtimeDataProvider;
diff --git a/apps/carbon/app/stores/customers.ts b/apps/carbon/app/stores/customers.ts
index 3cba9f97c3..e41e5b0992 100644
--- a/apps/carbon/app/stores/customers.ts
+++ b/apps/carbon/app/stores/customers.ts
@@ -3,4 +3,4 @@ import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
const $customersStore = atom<ListItem[]>([]);
-export const useCustomers = () => useNanoStore($customersStore, "customers");
+export const useCustomers = () => useNanoStore($customersStore, "customer");
diff --git a/apps/carbon/app/stores/parts.ts b/apps/carbon/app/stores/parts.ts
index 6656e0126c..f0353b537c 100644
--- a/apps/carbon/app/stores/parts.ts
+++ b/apps/carbon/app/stores/parts.ts
@@ -3,7 +3,9 @@ import { atom, computed } from "nanostores";
import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
-type Part = ListItem & { replenishmentSystem: "Buy" | "Make" | "Buy and Make" };
+export type Part = ListItem & {
+ replenishmentSystem: "Buy" | "Make" | "Buy and Make";
+};
const $partsStore = atom<Part[]>([]);
@@ -15,6 +17,6 @@ const $manufacturedPartsStore = computed($partsStore, (part) =>
part.filter((i) => i.replenishmentSystem === "Make")
);
-export const useParts = () => useNanoStore<Part[]>($partsStore, "parts");
+export const useParts = () => useNanoStore<Part[]>($partsStore, "part");
export const usePurchasedParts = () => useValue($purchasedPartsStore);
export const useManufacturedParts = () => useValue($manufacturedPartsStore);
diff --git a/apps/carbon/app/stores/suppliers.ts b/apps/carbon/app/stores/suppliers.ts
index f25c1ad281..9bc86a4c49 100644
--- a/apps/carbon/app/stores/suppliers.ts
+++ b/apps/carbon/app/stores/suppliers.ts
@@ -3,4 +3,4 @@ import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
const $suppliersStore = atom<ListItem[]>([]);
-export const useSuppliers = () => useNanoStore($suppliersStore, "suppliers");
+export const useSuppliers = () => useNanoStore($suppliersStore, "supplier");