From 6eaff102a9fe70d10732c1f63fa8ffcd0f367b69 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 5 Nov 2026 21:09:57 -0500 Subject: [PATCH] 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 * Delete app/hooks/useTableRequest.test.tsx --------- Co-authored-by: Claude Opus 5.5 --- app/hooks/useTableRequest.ts | 23 +++++++++--- shared/components/Calendar.tsx | 8 +++++ shared/utils/date.test.ts | 66 ++++++++++++++++++++++++++++++++++ shared/utils/date.ts | 52 +++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 5 deletions(-) diff --git a/app/hooks/useTableRequest.ts b/app/hooks/useTableRequest.ts index 7c845b62da..fac00153d9 100644 --- a/app/hooks/useTableRequest.ts +++ b/app/hooks/useTableRequest.ts @@ -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 = { - 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>; reqParams: Omit; @@ -28,6 +33,7 @@ export function useTableRequest({ reqFn, reqParams, }: Props): Response { + const [rows, setRows] = useState([]); const [hasNext, setHasNext] = useState(false); const [offset, setOffset] = useState({ value: INITIAL_OFFSET }); const prevParamsRef = useRef(reqParams); @@ -48,9 +54,11 @@ export function useTableRequest({ [] ); - 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({ } 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 diff --git a/shared/components/Calendar.tsx b/shared/components/Calendar.tsx index 03dd8bbe6b..344ec814ad 100644 --- a/shared/components/Calendar.tsx +++ b/shared/components/Calendar.tsx @@ -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; + } `; diff --git a/shared/utils/date.test.ts b/shared/utils/date.test.ts index 281979dbef..204e76ff41 100644 --- a/shared/utils/date.test.ts +++ b/shared/utils/date.test.ts @@ -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(""); + }); +}); diff --git a/shared/utils/date.ts b/shared/utils/date.ts index 0ce363b5a3..365f3667e8 100644 --- a/shared/utils/date.ts +++ b/shared/utils/date.ts @@ -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",