feat: Add server-driven rows to useTableRequest and date range helpers (#14483)

* feat: Add server-driven rows to useTableRequest and date range helpers

- useTableRequest: `data` is now optional, when omitted the hook displays the
  rows returned by the server for the current params. Needed when server-side
  filtering cannot be reproduced against a client store.
- Add `dateToTimestamp` and `dateRangeToReadable` localized date formatters.
- Calendar: style the days between the start and end of a range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Delete app/hooks/useTableRequest.test.tsx

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tom MoorandClaude Opus 5.5 authored and GitHub committed 2026-11-06 02:09:57 +00:00
1 parent d3aeabef51
commit 6eaff102a9
4 files changed
+144 -5

No files matched your search

+18 -5
View File
@@ -1,5 +1,5 @@
import type { ColumnSort } from "@tanstack/react-table";
import { orderBy } from "es-toolkit/compat";
import { orderBy, uniqBy } from "es-toolkit/compat";
import { useState, useRef, useCallback, useEffect } from "react";
import type { FetchPageParams, PaginatedResponse } from "~/stores/base/Store";
import { PAGINATION_SYMBOL } from "~/stores/base/Store";
@@ -9,7 +9,12 @@ const INITIAL_OFFSET = 0;
const PAGE_SIZE = 25;
type Props<T> = {
data: T[];
/**
* The rows to display, usually derived from a store. When omitted the rows
* returned by the server for the current params are displayed, which is
* required when the server filtering can't be reproduced on the client.
*/
data?: T[];
sort: ColumnSort;
reqFn: (params: FetchPageParams) => Promise<PaginatedResponse<T>>;
reqParams: Omit<FetchPageParams, "offset" | "limit">;
@@ -28,6 +33,7 @@ export function useTableRequest<T extends { id: string }>({
reqFn,
reqParams,
}: Props<T>): Response<T> {
const [rows, setRows] = useState<T[]>([]);
const [hasNext, setHasNext] = useState(false);
const [offset, setOffset] = useState({ value: INITIAL_OFFSET });
const prevParamsRef = useRef(reqParams);
@@ -48,9 +54,11 @@ export function useTableRequest<T extends { id: string }>({
[]
);
const sortedData = data
? orderBy(data, sortRef.current.id, sortRef.current.desc ? "desc" : "asc")
: undefined;
const sortedData = orderBy(
data ?? rows,
sortRef.current.id,
sortRef.current.desc ? "desc" : "asc"
);
const next = !loading && hasNext ? nextPage : undefined;
@@ -71,6 +79,11 @@ export function useTableRequest<T extends { id: string }>({
}
sortRef.current = sort; // Change sort once we receive a response from server - avoids flicker with stale data.
setRows((prev) =>
offset.value === INITIAL_OFFSET
? response
: uniqBy([...prev, ...response], "id")
);
const pagination = response[PAGINATION_SYMBOL];
setHasNext(
!!pagination && pagination.offset + response.length < pagination.total
+8
View File
@@ -164,4 +164,12 @@ const Wrapper = styled.div`
font-weight: 500;
opacity: 1;
}
/* The days between the start and end of a range are softly highlighted. */
.rdp-day_range_middle.rdp-day_selected,
.rdp-day_range_middle.rdp-day_selected:hover {
background: ${s("listItemHoverBackground")};
color: ${s("text")};
font-weight: 400;
}
`;
+66
View File
@@ -1,6 +1,8 @@
import {
dateRangeToReadable,
dateToReadable,
dateToRelativeReadable,
dateToTimestamp,
hasTimeComponent,
parseISODate,
toISODate,
@@ -168,3 +170,67 @@ describe("dateToRelativeReadable", () => {
).toBe("Today um 13:00");
});
});
describe("dateToTimestamp", () => {
const iso = new Date(2026, 10, 5, 19, 48, 12).toISOString();
it("includes the year and seconds", () => {
const result = dateToTimestamp(iso, "en_US");
expect(result).toContain("Nov 5, 2026");
expect(result).toMatch(/7:48:12\sPM/);
});
it("follows the word order and hour cycle of the language", () => {
const german = dateToTimestamp(iso, "de_DE");
expect(german).toContain("05.11.2026");
expect(german).toContain("19:48:12");
const british = dateToTimestamp(iso, "en_GB");
expect(british).toContain("5 Nov 2026");
expect(british).toContain("19:48:12");
});
it("includes the time zone", () => {
const zone = new Intl.DateTimeFormat("en-US", { timeZoneName: "short" })
.formatToParts(new Date(iso))
.find((part) => part.type === "timeZoneName")?.value;
expect(dateToTimestamp(iso, "en_US")).toContain(zone);
});
it("falls back to English without a language", () => {
expect(dateToTimestamp(iso)).toEqual(dateToTimestamp(iso, "en_US"));
});
it("returns the original string when invalid", () => {
expect(dateToTimestamp("not a date", "en_US")).toEqual("not a date");
});
});
describe("dateRangeToReadable", () => {
const start = new Date(2026, 10, 2);
const end = new Date(2026, 10, 3);
it("collapses the parts shared between the dates", () => {
expect(dateRangeToReadable(start, end, "en_US")).toMatch(
/^Nov 2\s–\s3, 2026$/
);
});
it("follows the language", () => {
expect(dateRangeToReadable(start, end, "de_DE")).toContain("2026");
expect(dateRangeToReadable(start, end, "de_DE")).toContain("03.11");
});
it("uses a single date for the same day", () => {
expect(dateRangeToReadable(start, start, "en_US")).toEqual("Nov 2, 2026");
});
it("uses a single date when only one bound is set", () => {
expect(dateRangeToReadable(start, null, "en_US")).toEqual("Nov 2, 2026");
expect(dateRangeToReadable(null, end, "en_US")).toEqual("Nov 3, 2026");
});
it("is empty without bounds", () => {
expect(dateRangeToReadable(null, null, "en_US")).toEqual("");
});
});
+52
View File
@@ -508,6 +508,58 @@ export function dateToReadable(
return dateFormatter(tag, { ...options, ...timeOptions }).format(date);
}
/**
* Formats an ISO value into an exact, localized timestamp that always includes
* the year, seconds and time zone (e.g. "Nov 5, 2026, 7:48:12 PM EST"), for
* contexts such as audit trails where the moment must be unambiguous. The word
* order, numerals and hour cycle follow the language, the time zone is local.
*
* @param iso The ISO string.
* @param language The user's language preference.
* @returns the localized timestamp, or the original string when invalid.
*/
export function dateToTimestamp(
iso: string,
language?: keyof typeof locales | null
): string {
const date = new Date(iso);
if (isNaN(date.getTime())) {
return iso;
}
return dateFormatter(languageTag(language), {
dateStyle: "medium",
timeStyle: "long",
}).format(date);
}
/**
* Formats a range of days into a localized label, collapsing the parts that are
* shared between the two dates (e.g. "Nov 2 – 3, 2026"). A range that is only
* bounded on one side is labelled with its single date.
*
* @param start The first day of the range.
* @param end The last day of the range.
* @param language The user's language preference.
* @returns the localized label.
*/
export function dateRangeToReadable(
start: Date | null,
end: Date | null,
language?: keyof typeof locales | null
): string {
const formatter = dateFormatter(languageTag(language), {
dateStyle: "medium",
});
if (start && end) {
return formatter.formatRange(start, end);
}
const date = start ?? end;
return date ? formatter.format(date) : "";
}
/**
* Formats a date mention's stored ISO value into a relative, localized,
* human-readable label with increasing granularity. Returns "Today",