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(erp): keep the date range filter in step with the URL

naveenkashec24c703daa4
diff --git a/apps/erp/app/components/Table/components/Filter/DateRangeFilter.tsx b/apps/erp/app/components/Table/components/Filter/DateRangeFilter.tsx
index 8cdde481be..2a5d83f21f 100644
--- a/apps/erp/app/components/Table/components/Filter/DateRangeFilter.tsx
+++ b/apps/erp/app/components/Table/components/Filter/DateRangeFilter.tsx
@@ -5,12 +5,12 @@
import { useDebounce } from "@carbon/react";
import type { CalendarDate } from "@internationalized/date";
import { parseDate } from "@internationalized/date";
-import { useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import DateRangeFields, {
type DateRangeValue
} from "~/components/DateRangeFields";
import { formatRangeFilter, parseRangeFilter } from "~/utils/query";
-import { useFilters } from "./useFilters";
+import { findFilterValue, useFilters } from "./useFilters";
function toCalendarDate(value: string | null): CalendarDate | null {
if (!value) return null;
@@ -25,24 +25,58 @@ type DateRangeFilterProps = {
accessorKey: string;
};
+function toRange(value: string | null): DateRangeValue {
+ const { from, to } = parseRangeFilter(value ?? "");
+ return { from: toCalendarDate(from), to: toCalendarDate(to) };
+}
+
/** `DateRangeFields` bound to the URL as `?filter=<key>:between:from,to`. */
const DateRangeFilter = ({ accessorKey }: DateRangeFilterProps) => {
const { getFilterValue, removeKey, setFilter } = useFilters();
+ const current = getFilterValue(accessorKey);
+
+ // The URL values this draft builds on: the one it was seeded from, then
+ // every value it has written since, oldest first. The URL landing on any
+ // other value is a change made elsewhere (Back / Forward, the filter
+ // removed), and that change wins over the draft.
+ const knownRef = useRef<(string | null)[]>([current]);
+ // A change made elsewhere re-seeds the fields with the URL's bounds, and an
+ // update still waiting on the debounce is dropped: it carries the
+ // generation it was typed in.
+ const generationRef = useRef(0);
+ const [seed, setSeed] = useState({ generation: 0, value: current });
- const [defaultValue] = useState<DateRangeValue>(() => {
- const { from, to } = parseRangeFilter(getFilterValue(accessorKey) ?? "");
- return { from: toCalendarDate(from), to: toCalendarDate(to) };
- });
+ useEffect(() => {
+ const index = knownRef.current.indexOf(current);
+ if (index >= 0) {
+ // One of our writes landed; older values are history now, so going
+ // Back to one of them counts as a change made elsewhere
+ knownRef.current = knownRef.current.slice(index);
+ return;
+ }
+ knownRef.current = [current];
+ generationRef.current += 1;
+ setSeed({ generation: generationRef.current, value: current });
+ }, [current]);
- const apply = (range: DateRangeValue | null) => {
+ const apply = (range: DateRangeValue | null, generation: number) => {
// From after To: keep whatever the URL already says
- if (!range) return;
+ if (!range || generation !== generationRef.current) return;
+ // The browser's URL, not this render's: on Back the popover can unmount
+ // (and flush) after the URL changed but before this component re-rendered
+ const live = findFilterValue(
+ new URLSearchParams(window.location.search).getAll("filter"),
+ accessorKey
+ );
+ if (!knownRef.current.includes(live)) return;
+
const value = formatRangeFilter(
range.from?.toString(),
range.to?.toString()
);
- // Already what the URL says — closing the popover replays the last change
- if (value === getFilterValue(accessorKey)) return;
+ // Already the latest value — closing the popover replays the last change
+ if (value === knownRef.current[knownRef.current.length - 1]) return;
+ knownRef.current.push(value);
if (value) {
setFilter(accessorKey, value, "between");
} else {
@@ -56,13 +90,18 @@ const DateRangeFilter = ({ accessorKey }: DateRangeFilterProps) => {
const applyRef = useRef(apply);
applyRef.current = apply;
const debouncedApply = useDebounce(
- (next: DateRangeValue | null) => applyRef.current(next),
+ (next: DateRangeValue | null, generation: number) =>
+ applyRef.current(next, generation),
400,
true
);
return (
- <DateRangeFields defaultValue={defaultValue} onChange={debouncedApply} />
+ <DateRangeFields
+ key={seed.generation}
+ defaultValue={toRange(seed.value)}
+ onChange={(range) => debouncedApply(range, seed.generation)}
+ />
);
};
diff --git a/apps/erp/app/components/Table/components/Filter/useFilters.tsx b/apps/erp/app/components/Table/components/Filter/useFilters.tsx
index 1c0bae2613..febc25b58f 100644
--- a/apps/erp/app/components/Table/components/Filter/useFilters.tsx
+++ b/apps/erp/app/components/Table/components/Filter/useFilters.tsx
@@ -4,6 +4,12 @@
import { useUrlParams } from "~/hooks";
+/** A key's value in a list of `filter` params (`key:operator:value`), whatever the operator */
+export function findFilterValue(filters: string[], key: string): string | null {
+ const filter = filters.find((f) => f.split(":")[0] === key);
+ return filter?.split(":")[2] ?? null;
+}
+
export function useFilters() {
const [params, setParams] = useUrlParams();
const urlFiltersParams = params.getAll("filter");
@@ -63,10 +69,8 @@ export function useFilters() {
};
// The key's value exactly as written in the URL, whatever the operator
- const getFilterValue = (searchKey: string): string | null => {
- const filter = urlFiltersParams.find((f) => f.split(":")[0] === searchKey);
- return filter?.split(":")[2] ?? null;
- };
+ const getFilterValue = (searchKey: string): string | null =>
+ findFilterValue(urlFiltersParams, searchKey);
const addFilter = (newKey: string, newValue: string, isArray = false) => {
if (hasFilterKey(newKey)) {