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: read from indexedb for parts, suppliers, and customers on page load

Brad Barbined25b7809086
diff --git a/apps/carbon/app/hooks/useNanoStore.ts b/apps/carbon/app/hooks/useNanoStore.ts
index 2931968bb3..6865e2a78e 100644
--- a/apps/carbon/app/hooks/useNanoStore.ts
+++ b/apps/carbon/app/hooks/useNanoStore.ts
@@ -1,19 +1,22 @@
import { useStore } from "@nanostores/react";
+import idb from "localforage";
import { type WritableAtom } from "nanostores";
import { useCallback } from "react";
-export function useNanoStore<T>(atom: WritableAtom<T>) {
+export function useNanoStore<T>(atom: WritableAtom<T>, localKey?: string) {
const value = useStore(atom);
const set = useCallback(
(value: T | ((current: T) => T)) => {
if (typeof value === "function") {
atom.set((value as (current: T) => T)(atom.get()));
+ if (localKey) idb.setItem(localKey, atom.get());
} else {
atom.set(value);
+ if (localKey) idb.setItem(localKey, value);
}
},
- [atom]
+ [atom, localKey]
);
return [value, set] as const;
diff --git a/apps/carbon/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLines/PurchaseInvoiceLines.tsx b/apps/carbon/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLines/PurchaseInvoiceLines.tsx
index 73745b33a2..2347a9673b 100644
--- a/apps/carbon/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLines/PurchaseInvoiceLines.tsx
+++ b/apps/carbon/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLines/PurchaseInvoiceLines.tsx
@@ -195,7 +195,7 @@ const PurchaseInvoiceLines = () => {
return (
<>
- <Card className="w-full h-full">
+ <Card className="w-full h-full min-h-[50vh]">
<HStack className="justify-between items-start">
<CardHeader>
<CardTitle>Purchase Invoice Lines</CardTitle>
diff --git a/apps/carbon/app/modules/parts/ui/Part/PartSuppliers/PartSuppliers.tsx b/apps/carbon/app/modules/parts/ui/Part/PartSuppliers/PartSuppliers.tsx
index c46da401c2..0c121b55a7 100644
--- a/apps/carbon/app/modules/parts/ui/Part/PartSuppliers/PartSuppliers.tsx
+++ b/apps/carbon/app/modules/parts/ui/Part/PartSuppliers/PartSuppliers.tsx
@@ -104,7 +104,7 @@ const PartSuppliers = ({ partSuppliers }: PartSuppliersProps) => {
return (
<>
- <Card>
+ <Card className="w-full h-full min-h-[50vh]">
<HStack className="justify-between items-start">
<CardHeader>
<CardTitle>Suppliers</CardTitle>
diff --git a/apps/carbon/app/modules/parts/ui/Service/ServiceSuppliers/ServiceSuppliers.tsx b/apps/carbon/app/modules/parts/ui/Service/ServiceSuppliers/ServiceSuppliers.tsx
index fdafa6f924..15283e82b8 100644
--- a/apps/carbon/app/modules/parts/ui/Service/ServiceSuppliers/ServiceSuppliers.tsx
+++ b/apps/carbon/app/modules/parts/ui/Service/ServiceSuppliers/ServiceSuppliers.tsx
@@ -68,7 +68,7 @@ const ServiceSuppliers = ({ serviceSuppliers }: ServiceSuppliersProps) => {
return (
<>
- <Card>
+ <Card className="w-full h-full min-h-[50vh]">
<HStack className="justify-between items-start">
<CardHeader>
<CardTitle>Suppliers</CardTitle>
diff --git a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx
index bc3b838f45..a61e27b086 100644
--- a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx
@@ -283,7 +283,7 @@ const PurchaseOrderLines = () => {
return (
<>
- <Card className="w-full h-full">
+ <Card className="w-full h-full min-h-[50vh]">
<HStack className="justify-between items-start">
<CardHeader>
<CardTitle>Purchase Order Lines</CardTitle>
diff --git a/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx b/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
index 231c42951d..69b9fb7d3d 100644
--- a/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
+++ b/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
@@ -1,16 +1,40 @@
+import idb from "localforage";
import { useEffect, useState } from "react";
+import { flushSync } from "react-dom";
import { useSupabase } from "~/lib/supabase";
import { useCustomers, useParts, useSuppliers } from "~/stores";
+let hydrated = false;
+
const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
const [loading, setLoading] = useState(true);
const { supabase, accessToken } = useSupabase();
- const [, setParts] = useParts();
+ const [, setParts] = useParts([]);
const [, setSuppliers] = useSuppliers();
const [, setCustomers] = useCustomers();
const fetchData = 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));
+ }
+ }
+
if (!supabase || !accessToken) return;
const [parts, suppliers, customers] = await Promise.all([
@@ -36,9 +60,9 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
};
useEffect(() => {
- if (!supabase || !accessToken) return;
fetchData();
+ if (!supabase || !accessToken) return;
supabase.realtime.setAuth(accessToken);
const channel = supabase
.channel("realtime:core")
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.tsx
index 8c691df131..f953153c2b 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.tsx
@@ -54,7 +54,7 @@ export default function CustomerRoute() {
return (
<>
<CustomerHeader />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<CustomerSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/routes/x+/index.tsx b/apps/carbon/app/routes/x+/index.tsx
index b5509d1992..3319cc89c6 100644
--- a/apps/carbon/app/routes/x+/index.tsx
+++ b/apps/carbon/app/routes/x+/index.tsx
@@ -1,11 +1,5 @@
-import { type LoaderFunctionArgs } from "@remix-run/node";
-
import { Editor, useEditor } from "@carbon/react";
-export async function loader({ request }: LoaderFunctionArgs) {
- return null;
-}
-
export default function AppIndexRoute() {
const editor = useEditor(`<h2>Hello, World</h2><p>Welcome to Carbon!</p>`);
return <Editor editor={editor} className="h-[calc(100vh-98px)]" />;
diff --git a/apps/carbon/app/routes/x+/part+/$partId.tsx b/apps/carbon/app/routes/x+/part+/$partId.tsx
index 14ddc55026..0b0d7f7999 100644
--- a/apps/carbon/app/routes/x+/part+/$partId.tsx
+++ b/apps/carbon/app/routes/x+/part+/$partId.tsx
@@ -36,7 +36,7 @@ export default function PartRoute() {
return (
<>
<PartPreview />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<PartSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/routes/x+/person+/$personId.tsx b/apps/carbon/app/routes/x+/person+/$personId.tsx
index 2f707cd888..9666de8e3c 100644
--- a/apps/carbon/app/routes/x+/person+/$personId.tsx
+++ b/apps/carbon/app/routes/x+/person+/$personId.tsx
@@ -90,7 +90,7 @@ export default function PersonRoute() {
return (
<>
<PersonPreview />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<PersonSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/routes/x+/purchase-invoice+/$invoiceId.tsx b/apps/carbon/app/routes/x+/purchase-invoice+/$invoiceId.tsx
index a82631d9f9..07df262802 100644
--- a/apps/carbon/app/routes/x+/purchase-invoice+/$invoiceId.tsx
+++ b/apps/carbon/app/routes/x+/purchase-invoice+/$invoiceId.tsx
@@ -74,7 +74,7 @@ export default function PurchaseInvoiceRoute() {
return (
<>
<PurchaseInvoiceHeader />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<PurchaseInvoiceSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/routes/x+/purchase-order+/$orderId.tsx b/apps/carbon/app/routes/x+/purchase-order+/$orderId.tsx
index 9627f9204b..1a36047b76 100644
--- a/apps/carbon/app/routes/x+/purchase-order+/$orderId.tsx
+++ b/apps/carbon/app/routes/x+/purchase-order+/$orderId.tsx
@@ -87,7 +87,7 @@ export default function PurchaseOrderRoute() {
return (
<>
<PurchaseOrderHeader />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<PurchaseOrderSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/routes/x+/service+/$serviceId.tsx b/apps/carbon/app/routes/x+/service+/$serviceId.tsx
index 35b922bd00..ef3baf3c6b 100644
--- a/apps/carbon/app/routes/x+/service+/$serviceId.tsx
+++ b/apps/carbon/app/routes/x+/service+/$serviceId.tsx
@@ -39,7 +39,7 @@ export default function ServiceRoute() {
return (
<>
<ServicePreview />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<ServiceSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx
index b2ec238925..93d2c8136b 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.tsx
@@ -54,7 +54,7 @@ export default function SupplierRoute() {
return (
<>
<SupplierHeader />
- <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-x-4">
+ <div className="grid grid-cols-1 md:grid-cols-[1fr_4fr] h-full w-full gap-4">
<SupplierSidebar />
<Outlet />
</div>
diff --git a/apps/carbon/app/stores/customers.ts b/apps/carbon/app/stores/customers.ts
index 44eb0a75a6..3cba9f97c3 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);
+export const useCustomers = () => useNanoStore($customersStore, "customers");
diff --git a/apps/carbon/app/stores/parts.ts b/apps/carbon/app/stores/parts.ts
index 248b5ccf35..6656e0126c 100644
--- a/apps/carbon/app/stores/parts.ts
+++ b/apps/carbon/app/stores/parts.ts
@@ -15,6 +15,6 @@ const $manufacturedPartsStore = computed($partsStore, (part) =>
part.filter((i) => i.replenishmentSystem === "Make")
);
-export const useParts = () => useNanoStore<Part[]>($partsStore);
+export const useParts = () => useNanoStore<Part[]>($partsStore, "parts");
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 da7f34a8a5..f25c1ad281 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);
+export const useSuppliers = () => useNanoStore($suppliersStore, "suppliers");