From 6966ecde2ec563be4da2a87f0576c4cd3255ad95 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:58:28 +1100 Subject: [PATCH 01/13] Move run age under the time on the activity table --- web/src/features/activity/RecentRunTable.tsx | 63 +++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 94358af..3f974ce 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -5,8 +5,8 @@ import { useState, type CSSProperties } from "react"; import { api, type Schemas } from "@/api/client"; import { RunLink } from "@/features/runs/RunLink"; import { + formatAge, formatGap, - formatSince, formatTime, trackName, } from "@/features/runs/time"; @@ -86,7 +86,6 @@ export function RecentRunTable({ - WhenPlayerMapTime @@ -107,12 +106,6 @@ export function RecentRunTable({ } as CSSProperties } > - - {/* Counted from now, which differs between the server's render and the browser's. */} - - - {/* Against the record when the run finished: what a new WR took off, or how far behind it was. */} - {run.gap != null && ( - 0 - ? "text-danger" - : "text-text-subtle", + + + + {formatTime(run.time)} + + {/* Against the record when the run finished: what a new WR took off, or how far behind it was. */} + {run.gap != null && ( + 0 + ? "text-danger" + : "text-text-subtle", + )} + > + {run.gap < 0 + ? `(−${formatTime(-run.gap)})` + : `(${formatGap(run.gap)})`} + )} - > - {run.gap < 0 - ? `−${formatTime(-run.gap)}` - : formatGap(run.gap)} - )} - - {formatTime(run.time)} - + {/* Counted from now, which differs between the server's render and the browser's. */} + + {run.world_record ? ( From 7e63acecc4a33f882b199b8fb48b5eb57c28440e Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:09:25 +1100 Subject: [PATCH 02/13] Add replay buttons to the activity table and narrow the page --- web/src/app/activity/page.tsx | 2 +- web/src/features/activity/RecentRunTable.tsx | 4 +- web/src/features/replays/ReplayLink.tsx | 33 ++++++++++++++ web/src/features/replays/ReplayPlayer.tsx | 6 ++- web/src/features/runs/RunDialogHost.tsx | 48 ++++++++++++++++++-- web/src/features/runs/runDialog.ts | 17 +++++-- web/src/ui/Dialog.tsx | 5 +- web/src/ui/Page.tsx | 5 +- 8 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 web/src/features/replays/ReplayLink.tsx diff --git a/web/src/app/activity/page.tsx b/web/src/app/activity/page.tsx index b93150e..c7e864d 100644 --- a/web/src/app/activity/page.tsx +++ b/web/src/app/activity/page.tsx @@ -13,7 +13,7 @@ export const metadata: Metadata = { export default function ActivityPage({ searchParams }: PageProps<"/activity">) { return ( - +

Activity

diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 3f974ce..88d42d2 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -3,6 +3,7 @@ // loads older runs a page at a time. import { useState, type CSSProperties } from "react"; import { api, type Schemas } from "@/api/client"; +import { ReplayLink } from "@/features/replays/ReplayLink"; import { RunLink } from "@/features/runs/RunLink"; import { formatAge, @@ -144,7 +145,8 @@ export function RecentRunTable({
- + + ) { + if ( + event.metaKey || + event.ctrlKey || + event.shiftKey || + event.altKey || + event.button !== 0 + ) { + return; + } + event.preventDefault(); + openRun(runId, { replay: true }); + } + return ( + + + + ); +} diff --git a/web/src/features/replays/ReplayPlayer.tsx b/web/src/features/replays/ReplayPlayer.tsx index e1ebbcb..e6b1741 100644 --- a/web/src/features/replays/ReplayPlayer.tsx +++ b/web/src/features/replays/ReplayPlayer.tsx @@ -70,17 +70,21 @@ type Prepared = { map: string; loading: ReturnType }; export function ReplayPlayer({ run, styleNames, + startNow = false, }: { run: Schemas["RunDetail"]; /** Ranked styles' names by key, for a compared run of another style. */ styleNames: Record; + /** Start without waiting for a press, as in the replay dialog. */ + startNow?: boolean; }) { const [pressedPhase, setPhase] = useState({ kind: "poster" }); - const autoStart = useSyncExternalStore( + const openedAtAnchor = useSyncExternalStore( subscribeToHash, openedAtReplay, () => false, ); + const autoStart = startNow || openedAtAnchor; const phase: Phase = pressedPhase.kind === "poster" && autoStart ? { kind: "loading" } diff --git a/web/src/features/runs/RunDialogHost.tsx b/web/src/features/runs/RunDialogHost.tsx index a8b0241..edf8d75 100644 --- a/web/src/features/runs/RunDialogHost.tsx +++ b/web/src/features/runs/RunDialogHost.tsx @@ -2,6 +2,7 @@ // The run dialog, over whichever page is open. It opens the moment a run link is clicked and loads the run // inside, so the page behind never reloads (lead, 2026-10-19). It shows what everyone sees; staff act on the // run on its own page. Pre-2.0 runs open in it too, with what the old site kept; they have no run page. +import dynamic from "next/dynamic"; import { useSearchParams } from "next/navigation"; import { useEffect, useState } from "react"; import { api, type Schemas } from "@/api/client"; @@ -12,7 +13,13 @@ import { Button, ButtonLink } from "@/ui/Button"; import { Dialog } from "@/ui/Dialog"; import { Tooltip } from "@/ui/Tooltip"; import { RunDetailList } from "./RunDetailList"; -import { closeRun, PRE_RUN_PARAM, RUN_PARAM } from "./runDialog"; +import { closeRun, PRE_RUN_PARAM, REPLAY_PARAM, RUN_PARAM } from "./runDialog"; + +// The viewer is large, so it loads only once a replay is opened, not with every page. +const ReplayPlayer = dynamic( + () => import("@/features/replays/ReplayPlayer").then((m) => m.ReplayPlayer), + { ssr: false }, +); type Run = | { legacy: false; run: Schemas["RunDetail"] } @@ -52,10 +59,12 @@ async function loadRun(id: string, legacy: boolean): Promise { export function RunDialogHost() { const params = useSearchParams(); const runId = params.get(RUN_PARAM); + const replayId = params.get(REPLAY_PARAM); const preRunId = params.get(PRE_RUN_PARAM); - const id = runId ?? preRunId; - const legacy = runId == null && preRunId != null; - const key = id && `${legacy ? "pre" : "run"}-${id}`; + const id = runId ?? replayId ?? preRunId; + const replay = runId == null && replayId != null; + const legacy = runId == null && replayId == null && preRunId != null; + const key = id && `${legacy ? "pre" : replay ? "replay" : "run"}-${id}`; const [loaded, setLoaded] = useState(); const [names, setNames] = useState>(new Map()); @@ -82,6 +91,37 @@ export function RunDialogHost() { const shown = loaded?.key === key ? loaded : undefined; const run = shown && "run" in shown ? shown : undefined; + if (replay) { + return ( + + Run page + + + } + > + {!shown ? null : run && !run.legacy ? ( + run.run.replay ? ( + + ) : ( +

This run has no replay.

+ ) + ) : ( +

{"error" in shown && shown.error}

+ )} +
+ ); + } + return ( void; title: ReactNode; children: ReactNode; footer?: ReactNode; + /** `lg` for a large view such as a replay. */ + size?: "md" | "lg"; }) { const dialog = useRef(null); const closingQuietly = useRef(false); @@ -62,7 +65,7 @@ export function Dialog({ }} aria-labelledby={titleId} // The element sits wherever its opener is, such as a table cell, and would inherit that text layout. - className="border-border-strong bg-surface text-text shadow-dialog backdrop:bg-bg/75 open:motion-enter m-auto w-[min(100%-2rem,36rem)] rounded-lg border p-0 text-left text-base font-normal whitespace-normal" + className={`border-border-strong bg-surface text-text shadow-dialog backdrop:bg-bg/75 open:motion-enter m-auto rounded-lg border p-0 text-left text-base font-normal whitespace-normal ${size === "lg" ? "w-[min(100%-2rem,64rem)]" : "w-[min(100%-2rem,36rem)]"}`} >

diff --git a/web/src/ui/Page.tsx b/web/src/ui/Page.tsx index 3747f87..2b8891e 100644 --- a/web/src/ui/Page.tsx +++ b/web/src/ui/Page.tsx @@ -13,10 +13,10 @@ export function Page({ children, }: { /** - * `narrow` is a reading width for text-heavy pages. `fill` is for pages inside a section layout that + * `narrow` is a reading width for text-heavy pages; `medium` for a single table that would stretch thin. `fill` is for pages inside a section layout that * already sets the width and padding, such as the staff area with its sidebar. */ - width?: "wide" | "narrow" | "fill"; + width?: "wide" | "medium" | "narrow" | "fill"; className?: string; children: ReactNode; }) { @@ -27,6 +27,7 @@ export function Page({ "flex w-full flex-1 flex-col", width === "fill" ? "min-w-0" : "mx-auto px-4 py-10", width === "narrow" && "max-w-3xl", + width === "medium" && "max-w-4xl", width === "wide" && "max-w-6xl", className, )} From 4a690ef468d4aba793fe1943978dfb5e1f4d544c Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:12:09 +1100 Subject: [PATCH 03/13] Move the replay button after the time gap so they line up --- web/src/features/activity/RecentRunTable.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 88d42d2..9d50755 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -146,7 +146,6 @@ export function RecentRunTable({ - )} + {/* Counted from now, which differs between the server's render and the browser's. */} From 7192916c2daedf3d30d88db740099f2d03a7e013 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:18:20 +1100 Subject: [PATCH 04/13] Give the replay button its own column between time and place --- web/src/features/activity/RecentRunTable.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 9d50755..258f07a 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -90,7 +90,11 @@ export function RecentRunTable({ Player Map Time - + {/* Both as narrow as their content, so the play button sits midway between the time and the place. */} + + Replay + + Place

@@ -144,8 +148,8 @@ export function RecentRunTable({ - - + + )} - {/* Counted from now, which differs between the server's render and the browser's. */} + + + {run.world_record ? ( WR From 7937b9ea284cd800394725e1a8f302ce07fdf46a Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:20:23 +1100 Subject: [PATCH 05/13] Pull the replay button in close to the time and place --- web/src/features/activity/RecentRunTable.tsx | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 258f07a..08aa761 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -89,12 +89,14 @@ export function RecentRunTable({ PlayerMap - Time - {/* Both as narrow as their content, so the play button sits midway between the time and the place. */} - + + Time + + {/* Narrow padding keeps the play button close to the time and the place. */} + Replay - + Place @@ -147,7 +149,7 @@ export function RecentRunTable({ )} - + - + - + {run.world_record ? ( WR ) : run.placement != null ? ( From 175679c68cdd5450630709a7bd8465a81fdcca96 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:46:44 +1100 Subject: [PATCH 06/13] Turn the activity page into one board with paged runs and quieter filter menus --- web/src/app/activity/page.tsx | 31 +- web/src/features/activity/RecentRunTable.tsx | 329 ++++++++++-------- web/src/features/activity/RecentRuns.tsx | 105 +++--- web/src/features/dailyTotals/DailyTotals.tsx | 14 +- .../features/dailyTotals/DailyTotalsChart.tsx | 159 +++++---- web/src/features/replays/ReplayLink.tsx | 2 +- web/src/ui/ChoiceMenu.tsx | 7 +- web/src/ui/Menu.tsx | 7 +- web/src/ui/Table.tsx | 12 +- 9 files changed, 389 insertions(+), 277 deletions(-) diff --git a/web/src/app/activity/page.tsx b/web/src/app/activity/page.tsx index 769fcfa..c142c7a 100644 --- a/web/src/app/activity/page.tsx +++ b/web/src/app/activity/page.tsx @@ -1,5 +1,5 @@ -// Activity page: runs and world records per day, then the latest runs, narrowed by style, track, lowest tier or -// world records only. +// Activity page: one board. Runs and world records per day, then the latest runs, narrowed by style, track, +// lowest tier or world records only. import type { Metadata } from "next"; import { RecentRuns } from "@/features/activity/RecentRuns"; import { DailyTotals } from "@/features/dailyTotals/DailyTotals"; @@ -12,19 +12,20 @@ export const metadata: Metadata = { export default function ActivityPage({ searchParams }: PageProps<"/activity">) { return ( - -

Activity

- - {searchParams.then(({ style, track, tier, show }) => ( - - ))} + +
+ + {searchParams.then(({ style, track, tier, show }) => ( + + ))} +
); } diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 08aa761..f33c207 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -1,7 +1,8 @@ "use client"; -// The latest runs as a table: who, which board, the time, and where it placed when it finished. "Show more" -// loads older runs a page at a time. -import { useState, type CSSProperties } from "react"; +// The latest runs as a table: who, which board, the time, and where it placed when it finished. Previous and +// Next step through older runs a page at a time. +import { ChevronLeft, ChevronRight } from "lucide-react"; +import { useRef, useState, type CSSProperties } from "react"; import { api, type Schemas } from "@/api/client"; import { ReplayLink } from "@/features/replays/ReplayLink"; import { RunLink } from "@/features/runs/RunLink"; @@ -14,7 +15,6 @@ import { import { Avatar } from "@/ui/Avatar"; import { Badge } from "@/ui/Badge"; import { Button } from "@/ui/Button"; -import { Card } from "@/ui/Card"; import { classNames } from "@/ui/classNames"; import { Flag } from "@/ui/Flag"; import { Link } from "@/ui/Link"; @@ -35,6 +35,7 @@ type Query = { track?: "main" | "bonus"; min_tier?: number; records?: boolean; + limit?: number; }; export function RecentRunTable({ @@ -50,17 +51,36 @@ export function RecentRunTable({ /** With every style shown, each run names its own. */ showStyle: boolean; }) { - const [runs, setRuns] = useState(first.runs); - const [cursor, setCursor] = useState(first.next_cursor); + // Pages already seen stay loaded, so going back is instant. + const [pages, setPages] = useState([first]); + const [index, setIndex] = useState(0); const [loading, setLoading] = useState(false); const [failed, setFailed] = useState(false); + const board = useRef(null); + const rows = useRef(null); + // Held at the first page's height, so a short last page doesn't pull the page controls up. + const [minHeight, setMinHeight] = useState(); const tierCount = first.tier_count; + const page = pages[index] ?? first; + const runs = page.runs; - async function showMore(after: string) { + function show(next: number) { + setMinHeight((held) => held ?? rows.current?.offsetHeight); + setIndex(next); + // Keep the table's top in view when a page is turned from the bottom of a long page. + const top = board.current?.getBoundingClientRect().top ?? 0; + if (top < 0) board.current?.scrollIntoView({ block: "start" }); + } + + async function showNext() { + if (pages[index + 1]) return show(index + 1); + if (!page.next_cursor) return; setLoading(true); setFailed(false); const loaded = await api - .GET("/v1/runs", { params: { query: { ...query, cursor: after } } }) + .GET("/v1/runs", { + params: { query: { ...query, cursor: page.next_cursor } }, + }) .then((result) => result.data) .catch(() => undefined); setLoading(false); @@ -68,158 +88,181 @@ export function RecentRunTable({ setFailed(true); return; } - setRuns((shown) => [...shown, ...loaded.runs]); - setCursor(loaded.next_cursor); + setPages((seen) => [...seen.slice(0, index + 1), loaded]); + show(index + 1); } if (runs.length === 0) { return ( - +

{query.records ? "No world records match these choices." : "No runs match these choices."} - +

); } return ( -
-
- - - Player - Map - - Time - - {/* Narrow padding keeps the play button close to the time and the place. */} - - Replay - - - Place - - - - - {runs.map((run, index) => ( - - - - - - {run.player.name} - - - - - - - {run.tier != null && tierCount != null && ( - - )} - - {run.map} - - {(run.track > 0 || showStyle) && ( - - {[ - run.track > 0 && trackName(run.track), - showStyle && (styleNames[run.style] ?? run.style), - ] - .filter(Boolean) - .join(" · ")} - - )} - - - - - - +
+
+ + + Player + Map + + Time + + {/* Narrow padding keeps the play button close to the time and the place. */} + + Replay + + + Place + + + + {/* Keyed by page, so a turned page's rows fade in like the first. */} + + {runs.map((run, row) => ( + + + + - {formatTime(run.time)} - - {/* Against the record when the run finished: what a new WR took off, or how far behind it was. */} - {run.gap != null && ( - 0 - ? "text-danger" - : "text-text-subtle", - )} - > - {run.gap < 0 - ? `(−${formatTime(-run.gap)})` - : `(${formatGap(run.gap)})`} + + {run.player.name} + + + + + + + {run.tier != null && tierCount != null && ( + + )} + + {run.map} + + {(run.track > 0 || showStyle) && ( + + {[ + run.track > 0 && trackName(run.track), + showStyle && (styleNames[run.style] ?? run.style), + ] + .filter(Boolean) + .join(" · ")} )} - {/* Counted from now, which differs between the server's render and the browser's. */} - - - - - - - - {run.world_record ? ( - WR - ) : run.placement != null ? ( - - + + + + + + {formatTime(run.time)} + + {/* Against the record when the run finished: what a new WR took off, or how far behind it was. */} + {run.gap != null && ( + 0 + ? "text-danger" + : "text-text-subtle", + )} + > + {run.gap < 0 + ? `(−${formatTime(-run.gap)})` + : `(${formatGap(run.gap)})`} + + )} + + {/* Counted from now, which differs between the server's render and the browser's. */} + - ) : ( - – - )} - - - ))} - -
-
- {failed && ( - - More couldn't be loaded. Try again. - - )} - {cursor && ( - - )} + + + + + + {run.world_record ? ( + WR + ) : run.placement != null ? ( + + + + ) : ( + – + )} + + + ))} + +
+ {(index > 0 || page.next_cursor) && ( + + )} ); } diff --git a/web/src/features/activity/RecentRuns.tsx b/web/src/features/activity/RecentRuns.tsx index 87ba140..e9c7a0d 100644 --- a/web/src/features/activity/RecentRuns.tsx +++ b/web/src/features/activity/RecentRuns.tsx @@ -41,6 +41,7 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) { track: filters.track === "all" ? undefined : filters.track, min_tier: filters.tier, records: filters.records || undefined, + limit: 15, }; const result = await api .GET("/v1/runs", { params: { query } }) @@ -61,62 +62,70 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) { }; return ( -
- - - {styles.keys.length > 1 && ( +
+
+ ({ - value: key, - label: styles.name(key), - href: href({ style: key }), - })), + { + value: "all", + label: "All runs", + href: href({ records: false }), + }, + { + value: "records", + label: "World records", + href: href({ records: true }), + }, ]} - value={filters.style ?? ""} + value={filters.records ? "records" : "all"} /> - )} - ({ - value: t.key, - label: t.label, - href: href({ track: t.key }), - }))} - value={filters.track} - /> - {/* Tiers are the community's to set; until a tier count is published there's nothing to filter by. */} - {tierCount != null && ( + {styles.keys.length > 1 && ( + ({ + value: key, + label: styles.name(key), + href: href({ style: key }), + })), + ]} + value={filters.style ?? ""} + /> + )} ({ - value: String(i + 1), - label: `Tier ${i + 1}`, - href: href({ tier: i + 1 }), - })), - ]} - value={filters.tier ? String(filters.tier) : ""} + label="Track" + options={tracks.map((t) => ({ + value: t.key, + label: t.label, + href: href({ track: t.key }), + }))} + value={filters.track} /> - )} - + {/* Tiers are the community's to set; until a tier count is published there's nothing to filter by. */} + {tierCount != null && ( + ({ + value: String(i + 1), + label: `Tier ${i + 1}`, + href: href({ tier: i + 1 }), + })), + ]} + value={filters.tier ? String(filters.tier) : ""} + /> + )} + +
{!first ? ( -

Runs couldn't be loaded. Try again in a moment.

+

+ Runs couldn't be loaded. Try again in a moment. +

) : ( // Keyed by the filters, so a new choice starts again from the newest runs and they all fade in. ); } diff --git a/web/src/features/dailyTotals/DailyTotalsChart.tsx b/web/src/features/dailyTotals/DailyTotalsChart.tsx index ddc43bd..bde99b4 100644 --- a/web/src/features/dailyTotals/DailyTotalsChart.tsx +++ b/web/src/features/dailyTotals/DailyTotalsChart.tsx @@ -5,6 +5,7 @@ import { type ReactNode, useState } from "react"; import type { Schemas } from "@/api/client"; import { Card } from "@/ui/Card"; +import { classNames } from "@/ui/classNames"; import { LineChart } from "@/ui/LineChart"; import { Segmented } from "@/ui/Segmented"; @@ -52,9 +53,13 @@ interface Point { export function DailyTotalsChart({ data, initialRange, + heading, + plain = false, }: { data: DailyTotalsData; initialRange: TotalsRange; + heading?: string; + plain?: boolean; }) { const [range, setRange] = useState(initialRange); const [active, setActive] = useState(null); @@ -77,35 +82,93 @@ export function DailyTotalsChart({ const bucketDays = current?.bucket_days ?? 1; const rangeLabel = ranges.find((r) => r.value === range)?.label ?? ""; - return ( - -
- {shown && ( -

- {data.kind === "players" ? ( - - {shown.y === 1 ? "player" : "players"} with a recorded time - - ) : ( - <> - {/* Each number in its line's color, so the line reads as the legend. */} - - {shown.y === 1 ? "run" : "runs"} - - - - {shown.y2 === 1 ? "world record" : "world records"} - - - )} - - - {when(shown.day, bucketDays)} - -

+ const stats = shown && ( +

+ {data.kind === "players" ? ( + + {shown.y === 1 ? "player" : "players"} with a recorded time + + ) : ( + <> + {/* Each number in its line's color, so the line reads as the legend. */} + + {shown.y === 1 ? "run" : "runs"} + + + + {shown.y2 === 1 ? "world record" : "world records"} + + + )} + + + {when(shown.day, bucketDays)} + +

+ ); + + const plot = + points.length < 2 ? ( +

+ {current + ? "Not enough days yet to draw a chart." + : "This range couldn't be loaded."} +

+ ) : ( +
+ ({ + x: Date.parse(p.day), + y: p.y, + y2: p.y2, + }))} + active={active} + onActive={setActive} + formatValue={(value) => axis.format(Math.round(value))} + /> + {/* Indented by the value labels' width, so the dates sit under the plot's ends. */} +
+ {date.format(new Date(points[0]?.day ?? 0))} + {date.format(new Date(points.at(-1)?.day ?? 0))} +
+
+ ); + + const frame = ( + <> +
+ {heading ? ( +
+

{heading}

+ {stats} +
+ ) : ( + stats )}
- - {points.length < 2 ? ( -

- {current - ? "Not enough days yet to draw a chart." - : "This range couldn't be loaded."} -

- ) : ( -
- ({ - x: Date.parse(p.day), - y: p.y, - y2: p.y2, - }))} - active={active} - onActive={setActive} - formatValue={(value) => axis.format(Math.round(value))} - /> - {/* Indented by the value labels' width, so the dates sit under the plot's ends. */} -
- {date.format(new Date(points[0]?.day ?? 0))} - {date.format(new Date(points.at(-1)?.day ?? 0))} -
-
- )} - + {plot} + ); + + if (plain) return
{frame}
; + return {frame}; } function Stat({ diff --git a/web/src/features/replays/ReplayLink.tsx b/web/src/features/replays/ReplayLink.tsx index 6bfa728..1bc3db2 100644 --- a/web/src/features/replays/ReplayLink.tsx +++ b/web/src/features/replays/ReplayLink.tsx @@ -25,7 +25,7 @@ export function ReplayLink({ runId }: { runId: number }) { onClick={onClick} aria-label="Watch replay" title="Watch replay" - className="text-text-muted hover:bg-accent-soft hover:text-accent inline-flex size-6 shrink-0 items-center justify-center rounded-md transition-colors duration-(--duration-fast)" + className="text-text-muted hover:text-accent inline-flex size-6 shrink-0 items-center justify-center rounded-md transition-colors duration-(--duration-fast)" > diff --git a/web/src/ui/ChoiceMenu.tsx b/web/src/ui/ChoiceMenu.tsx index bb683eb..287a71d 100644 --- a/web/src/ui/ChoiceMenu.tsx +++ b/web/src/ui/ChoiceMenu.tsx @@ -43,12 +43,15 @@ export function ChoiceMenu({ <> {label} {chosen?.label ?? value} - + } // On a phone each menu takes a full row, label on the left and value on the right, so menus of different // widths line up as one column instead of wrapping unevenly. - triggerClassName="border-border bg-surface hover:bg-surface-raised flex h-9 w-full items-center gap-2 rounded-md border px-3 text-sm whitespace-nowrap transition-colors duration-(--duration-fast) sm:inline-flex sm:w-auto" + triggerClassName="group bg-text/4 hover:bg-text/8 aria-expanded:bg-text/8 flex h-8 w-full items-center gap-1.5 rounded-md px-2.5 text-sm whitespace-nowrap transition-colors duration-(--duration-fast) sm:inline-flex sm:w-auto" > {options.map((option) => { const className = classNames( diff --git a/web/src/ui/Menu.tsx b/web/src/ui/Menu.tsx index 7fe8813..81be014 100644 --- a/web/src/ui/Menu.tsx +++ b/web/src/ui/Menu.tsx @@ -95,7 +95,7 @@ export function Menu({ }} // A long list (the Maps page's sorts) scrolls inside the menu rather than running off a short screen. className={classNames( - "border-border-strong bg-surface-overlay text-text shadow-popover motion-pop m-0 mt-2 max-h-[min(28rem,70vh)] min-w-48 overflow-y-auto overscroll-contain rounded-md border p-1 text-sm", + "border-border-strong bg-surface-overlay text-text shadow-popover motion-pop m-0 mt-1 max-h-[min(28rem,70vh)] min-w-44 overflow-y-auto overscroll-contain rounded-lg border p-1 text-sm", matchTriggerWidth && "max-sm:min-w-[anchor-size(width)]", )} > @@ -105,8 +105,9 @@ export function Menu({ ); } +// The overlay is lighter than surface-raised, so hover lifts with a tint of the text color instead. const itemClasses = - "flex h-9 w-full items-center gap-2 rounded-sm px-3 text-left outline-none hover:bg-surface-raised focus-visible:bg-surface-raised [&_svg]:size-4 [&_svg]:text-text-muted"; + "flex h-8 w-full items-center gap-2 rounded-md px-2.5 text-left outline-none transition-colors duration-(--duration-fast) hover:bg-text/8 focus-visible:bg-text/8 [&_svg]:size-4 [&_svg]:text-text-muted aria-checked:[&_svg]:text-accent"; export function MenuItem({ className, ...props }: ComponentProps<"button">) { return ( @@ -132,5 +133,5 @@ export function MenuLink({ className, ...props }: ComponentProps) { } export function MenuSeparator() { - return
; + return
; } diff --git a/web/src/ui/Table.tsx b/web/src/ui/Table.tsx index 0c219b4..47d0e9a 100644 --- a/web/src/ui/Table.tsx +++ b/web/src/ui/Table.tsx @@ -2,14 +2,22 @@ import type { ComponentProps } from "react"; import { classNames } from "./classNames"; -export function Table({ className, ...props }: ComponentProps<"table">) { +export function Table({ + flush = false, + className, + ...props +}: { + /** No frame of its own. The parent supplies the surface, as on the activity board. */ + flush?: boolean; +} & ComponentProps<"table">) { return ( // Wide tables scroll sideways on small screens instead of squeezing their columns. `relative` keeps // absolutely placed content, such as a visually hidden column name, inside the scrolling box. Never // vertically: rows rising into place (`motion-enter`) overflow for a moment, which flashed a scrollbar.
From 447179a76ef76687ad80c4332e62874f3f7da7c8 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:55:57 +1100 Subject: [PATCH 07/13] Page activity runs from a hundred at a time, with page numbers in the address and ok-style filters --- web/src/app/activity/page.tsx | 3 +- web/src/features/activity/RecentRunTable.tsx | 209 ++++++++++++------- web/src/features/activity/RecentRuns.tsx | 92 ++++++-- web/src/ui/ChoiceMenu.tsx | 9 +- web/src/ui/Menu.tsx | 5 + web/src/ui/Pager.tsx | 154 ++++++++++++-- 6 files changed, 354 insertions(+), 118 deletions(-) diff --git a/web/src/app/activity/page.tsx b/web/src/app/activity/page.tsx index c142c7a..df4720b 100644 --- a/web/src/app/activity/page.tsx +++ b/web/src/app/activity/page.tsx @@ -15,13 +15,14 @@ export default function ActivityPage({ searchParams }: PageProps<"/activity">) {
- {searchParams.then(({ style, track, tier, show }) => ( + {searchParams.then(({ style, track, tier, show, page }) => ( ))} diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index f33c207..e760d42 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -1,8 +1,16 @@ "use client"; -// The latest runs as a table: who, which board, the time, and where it placed when it finished. Previous and -// Next step through older runs a page at a time. -import { ChevronLeft, ChevronRight } from "lucide-react"; -import { useRef, useState, type CSSProperties } from "react"; +// The latest runs as a table: who, which board, the time, and where it placed when it finished. Runs come a +// hundred at a time and are paged here, so turning a page is instant; the next hundred load in the background. +import { useSearchParams } from "next/navigation"; +import { + useCallback, + useEffect, + useRef, + useState, + type CSSProperties, + type FormEvent, + type MouseEvent, +} from "react"; import { api, type Schemas } from "@/api/client"; import { ReplayLink } from "@/features/replays/ReplayLink"; import { RunLink } from "@/features/runs/RunLink"; @@ -14,11 +22,11 @@ import { } from "@/features/runs/time"; import { Avatar } from "@/ui/Avatar"; import { Badge } from "@/ui/Badge"; -import { Button } from "@/ui/Button"; import { classNames } from "@/ui/classNames"; import { Flag } from "@/ui/Flag"; import { Link } from "@/ui/Link"; import { Medal } from "@/ui/Medal"; +import { Pager } from "@/ui/Pager"; import { Table, TableBody, @@ -29,7 +37,7 @@ import { } from "@/ui/Table"; import { TierBadge } from "@/ui/TierBadge"; -type Page = Schemas["RecentRuns"]; +type Runs = Schemas["RecentRuns"]; type Query = { style?: string; track?: "main" | "bonus"; @@ -41,55 +49,124 @@ type Query = { export function RecentRunTable({ first, query, + address, + page: firstPage, + pageSize, styleNames, showStyle, }: { - first: Page; - /** The filters `first` was loaded with, for loading the pages after it. */ + /** The newest runs, enough to reach `page`. */ + first: Runs; + /** The filters `first` was loaded with, for loading the runs after it. */ query: Query; + /** The page's address with these filters, which the page number is added to. */ + address: string; + page: number; + pageSize: number; styleNames: Record; /** With every style shown, each run names its own. */ showStyle: boolean; }) { - // Pages already seen stay loaded, so going back is instant. - const [pages, setPages] = useState([first]); - const [index, setIndex] = useState(0); - const [loading, setLoading] = useState(false); + const [runs, setRuns] = useState(first.runs); + const [cursor, setCursor] = useState(first.next_cursor ?? null); const [failed, setFailed] = useState(false); + const [waiting, setWaiting] = useState(false); + // The latest runs and cursor for the loops below, which outlive the render that started them. + const latest = useRef({ + runs: first.runs, + cursor: first.next_cursor ?? null, + }); + const inFlight = useRef | null>(null); const board = useRef(null); const rows = useRef(null); // Held at the first page's height, so a short last page doesn't pull the page controls up. const [minHeight, setMinHeight] = useState(); const tierCount = first.tier_count; - const page = pages[index] ?? first; - const runs = page.runs; - function show(next: number) { + // The address holds the page, so a reload, a shared link and the back button all land on it. + const asked = Number(useSearchParams().get("page") ?? firstPage); + const pageCount = Math.max(1, Math.ceil(runs.length / pageSize)); + const page = Math.min(Math.max(1, Math.floor(asked) || 1), pageCount); + const shown = runs.slice((page - 1) * pageSize, page * pageSize); + + const pageHref = useCallback( + (p: number) => + p > 1 + ? `${address}${address.includes("?") ? "&" : "?"}page=${p}` + : address, + [address], + ); + + /** Loads the next hundred runs, once however many ask; false when there are none or they failed. */ + const loadMore = useCallback((): Promise => { + if (inFlight.current) return inFlight.current; + const after = latest.current.cursor; + if (!after) return Promise.resolve(false); + inFlight.current = api + .GET("/v1/runs", { params: { query: { ...query, cursor: after } } }) + .then((result) => result.data) + .catch(() => undefined) + .then((next) => { + inFlight.current = null; + setFailed(!next); + if (!next) return false; + latest.current = { + runs: [...latest.current.runs, ...next.runs], + cursor: next.next_cursor ?? null, + }; + setRuns(latest.current.runs); + setCursor(latest.current.cursor); + return true; + }); + return inFlight.current; + }, [query]); + + // Keeps a few pages loaded past the one shown, so Next never waits. + useEffect(() => { + if (cursor && !failed && runs.length < (page + 4) * pageSize) + void loadMore(); + }, [cursor, failed, runs.length, page, pageSize, loadMore]); + + async function go(target: number) { + // A typed page past what's loaded walks forward to it, or to the last page if the runs end first. + while (latest.current.runs.length < target * pageSize) { + setWaiting(true); + if (!(await loadMore())) break; + } + setWaiting(false); + const count = Math.max(1, Math.ceil(latest.current.runs.length / pageSize)); setMinHeight((held) => held ?? rows.current?.offsetHeight); - setIndex(next); + window.history.pushState(null, "", pageHref(Math.min(target, count))); // Keep the table's top in view when a page is turned from the bottom of a long page. const top = board.current?.getBoundingClientRect().top ?? 0; if (top < 0) board.current?.scrollIntoView({ block: "start" }); } - async function showNext() { - if (pages[index + 1]) return show(index + 1); - if (!page.next_cursor) return; - setLoading(true); - setFailed(false); - const loaded = await api - .GET("/v1/runs", { - params: { query: { ...query, cursor: page.next_cursor } }, - }) - .then((result) => result.data) - .catch(() => undefined); - setLoading(false); - if (!loaded) { - setFailed(true); + // The pager's links and box work without this; here they turn the page without asking the server. + function onPagerClick(event: MouseEvent) { + const link = (event.target as HTMLElement).closest("a"); + if ( + !link || + event.button !== 0 || + event.metaKey || + event.ctrlKey || + event.shiftKey || + event.altKey + ) return; - } - setPages((seen) => [...seen.slice(0, index + 1), loaded]); - show(index + 1); + // Stopped here too, as Next's Link and Form navigate even when the event is already prevented. + event.preventDefault(); + event.stopPropagation(); + void go(Number(new URL(link.href).searchParams.get("page") ?? 1)); + } + + function onPagerSubmit(event: FormEvent) { + event.preventDefault(); + event.stopPropagation(); + const form = event.target as HTMLFormElement; + const typed = Math.floor(Number(new FormData(form).get("page"))); + form.reset(); + if (typed >= 1) void go(typed); } if (runs.length === 0) { @@ -104,7 +181,15 @@ export function RecentRunTable({ return (
-
+
@@ -123,8 +208,8 @@ export function RecentRunTable({ {/* Keyed by page, so a turned page's rows fade in like the first. */} - - {runs.map((run, row) => ( + + {shown.map((run, row) => (
- {(index > 0 || page.next_cursor) && ( - + +
+ )} + {failed && ( +

+ Older runs couldn't be loaded. Try again in a moment. +

)}
); diff --git a/web/src/features/activity/RecentRuns.tsx b/web/src/features/activity/RecentRuns.tsx index e9c7a0d..8173c96 100644 --- a/web/src/features/activity/RecentRuns.tsx +++ b/web/src/features/activity/RecentRuns.tsx @@ -1,5 +1,5 @@ // The Activity page's runs: the filter menus, read from and written to the address, and the first page of runs. -import { api } from "@/api/client"; +import { api, type Schemas } from "@/api/client"; import { currentStyles } from "@/features/ruleset/styles"; import { ChoiceBar } from "@/ui/ChoiceBar"; import { ChoiceMenu } from "@/ui/ChoiceMenu"; @@ -19,8 +19,22 @@ export interface RecentRunsParams { tier?: string; /** "records": world records only. */ show?: string; + page?: string; } +const PAGE_SIZE = 15; +/** Runs per request. A hundred cost the API about what fifteen do, so most pages turn without a request. */ +const CHUNK = 100; +/** How far a typed page number may walk before giving up, at one request per hundred runs. */ +const MAX_CHUNKS = 20; + +type RunsQuery = { + style?: string; + track?: "main" | "bonus"; + min_tier?: number; + records?: boolean; +}; + interface Filters { style?: string; track: Track; @@ -29,24 +43,30 @@ interface Filters { } export async function RecentRuns({ params }: { params: RecentRunsParams }) { - const styles = await currentStyles(); - const filters: Filters = { - style: styles.keys.find((key) => key === params.style), - track: tracks.find((t) => t.key === params.track)?.key ?? "all", - tier: Math.floor(Number(params.tier)) || undefined, - records: params.show === "records", - }; - const query = { - style: filters.style, - track: filters.track === "all" ? undefined : filters.track, - min_tier: filters.tier, - records: filters.records || undefined, - limit: 15, - }; - const result = await api - .GET("/v1/runs", { params: { query } }) - .catch(() => undefined); - const first = result?.data; + const track = tracks.find((t) => t.key === params.track)?.key ?? "all"; + const tier = Math.floor(Number(params.tier)) || undefined; + const records = params.show === "records"; + const page = Math.max(1, Math.floor(Number(params.page)) || 1); + const queryFor = (style: string | undefined): RunsQuery => ({ + style, + track: track === "all" ? undefined : track, + min_tier: tier, + records: records || undefined, + }); + + // The runs don't wait for the ruleset: the style in the address is almost always one it ranks, and only a + // stale one costs a second request. + const [styles, guessed] = await Promise.all([ + currentStyles(), + loadRuns(queryFor(params.style || undefined), page), + ]); + const style = styles.keys.find((key) => key === params.style); + const query = queryFor(style); + const first = + style === (params.style || undefined) + ? guessed + : await loadRuns(query, page); + const filters: Filters = { style, track, tier, records }; const tierCount = first?.tier_count ?? undefined; // Only what differs from the defaults goes in the address, so the plain page stays /activity. @@ -131,7 +151,10 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) { [key, styles.name(key)]), )} @@ -141,3 +164,32 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) {
); } + +/** Runs from the newest, enough to reach `page`; undefined when the first request fails. */ +async function loadRuns( + query: RunsQuery, + page: number, +): Promise { + const get = (cursor?: string) => + api + .GET("/v1/runs", { + params: { query: { ...query, limit: CHUNK, cursor } }, + }) + .then((result) => result.data) + .catch(() => undefined); + const first = await get(); + if (!first) return undefined; + const runs = [...first.runs]; + let cursor = first.next_cursor; + for ( + let chunk = 1; + cursor && runs.length < page * PAGE_SIZE && chunk < MAX_CHUNKS; + chunk++ + ) { + const next = await get(cursor); + if (!next) break; + runs.push(...next.runs); + cursor = next.next_cursor; + } + return { ...first, runs, next_cursor: cursor }; +} diff --git a/web/src/ui/ChoiceMenu.tsx b/web/src/ui/ChoiceMenu.tsx index 287a71d..936fcd9 100644 --- a/web/src/ui/ChoiceMenu.tsx +++ b/web/src/ui/ChoiceMenu.tsx @@ -45,17 +45,20 @@ export function ChoiceMenu({ {chosen?.label ?? value} } // On a phone each menu takes a full row, label on the left and value on the right, so menus of different // widths line up as one column instead of wrapping unevenly. - triggerClassName="group bg-text/4 hover:bg-text/8 aria-expanded:bg-text/8 flex h-8 w-full items-center gap-1.5 rounded-md px-2.5 text-sm whitespace-nowrap transition-colors duration-(--duration-fast) sm:inline-flex sm:w-auto" + // Darker than the surface it sits on, like a well, so it reads as a control without competing with the data. + triggerClassName="group border-border bg-bg hover:border-border-strong hover:bg-surface-raised aria-expanded:border-border-strong aria-expanded:bg-surface-raised flex h-9 w-full items-center gap-2 rounded-md border pr-2.5 pl-3 text-sm font-medium whitespace-nowrap transition-colors duration-(--duration-fast) sm:inline-flex sm:w-auto" > {options.map((option) => { const className = classNames( - (option.quiet || option.disabled) && "text-text-subtle", + option.quiet || option.disabled + ? "text-text-subtle" + : "text-text-muted hover:text-text aria-checked:text-accent", option.disabled && "cursor-not-allowed", ); const content = ( diff --git a/web/src/ui/Menu.tsx b/web/src/ui/Menu.tsx index 81be014..2af025d 100644 --- a/web/src/ui/Menu.tsx +++ b/web/src/ui/Menu.tsx @@ -7,6 +7,7 @@ import { type ToggleEvent, useId, useRef, + useState, } from "react"; import { Link } from "./Link"; import { anchoredStyle, anchorStyle } from "./anchor"; @@ -35,6 +36,8 @@ export function Menu({ }) { const id = useId(); const menu = useRef(null); + // Mirrored onto the trigger, so its open look can follow the popover. + const [open, setOpen] = useState(false); const items = () => Array.from( menu.current?.querySelectorAll( @@ -43,6 +46,7 @@ export function Menu({ ); function onToggle(event: ToggleEvent) { + setOpen(event.newState === "open"); if (event.newState === "open") items()[0]?.focus(); } @@ -67,6 +71,7 @@ export function Menu({ type="button" popoverTarget={id} aria-haspopup="menu" + aria-expanded={open} aria-label={label} style={anchorStyle(id)} className={triggerClassName} diff --git a/web/src/ui/Pager.tsx b/web/src/ui/Pager.tsx index e1cd260..37cf00e 100644 --- a/web/src/ui/Pager.tsx +++ b/web/src/ui/Pager.tsx @@ -1,39 +1,147 @@ -// Previous and next links under a paged list, with where the reader is. +// Page links under a paged list: both ends, the current page and its neighbours, and a box for going straight to +// a page. import { ChevronLeft, ChevronRight } from "lucide-react"; -import { ButtonLink } from "./Button"; +import Form from "next/form"; +import type { ReactNode } from "react"; +import { classNames } from "./classNames"; +import { Link } from "./Link"; -/** Renders nothing when everything fits on one page. Pages count from 1. */ +const PARAM = "page"; + +const cell = + "inline-flex h-8 min-w-8 items-center justify-center rounded-md px-2 text-sm tabular-nums transition-colors duration-(--duration-fast)"; +const link = "text-text-muted hover:bg-text/8 hover:text-text"; + +/** + * Renders nothing when everything fits on one page. Pages count from 1 and sit in the address as `?page=`, so + * `href(1)` also gives the address the box keeps. A list whose length isn't known yet passes the pages loaded so + * far and `more`, and its last page shows as a gap. + */ export function Pager({ page, pageCount, + more = false, href, }: { page: number; pageCount: number; + more?: boolean; href: (page: number) => string; }) { - if (pageCount <= 1) return null; + if (pageCount <= 1 && !more) return null; + const last = more ? pageCount + 1 : pageCount; + const shown = [ + ...new Set([1, page - 1, page, page + 1, ...(more ? [] : [pageCount])]), + ] + .filter((p) => p >= 1 && p <= last) + .sort((a, b) => a - b); + const base = new URL(href(1), "http://site"); + base.searchParams.delete(PARAM); + + const items = []; + let previous = 0; + for (const p of shown) { + if (p - previous > 1) items.push(); + items.push( + + {p} + , + ); + previous = p; + } + if (more) items.push(); + return ( -
diff --git a/web/src/features/maps/MapList.tsx b/web/src/features/maps/MapList.tsx index 2569051..a812560 100644 --- a/web/src/features/maps/MapList.tsx +++ b/web/src/features/maps/MapList.tsx @@ -348,6 +348,8 @@ export async function MapList({ params }: { params: MapListParams }) { href({ page: p })} /> diff --git a/web/src/features/staff/maps/UnplayedMaps.tsx b/web/src/features/staff/maps/UnplayedMaps.tsx index 0606e99..ecb28ce 100644 --- a/web/src/features/staff/maps/UnplayedMaps.tsx +++ b/web/src/features/staff/maps/UnplayedMaps.tsx @@ -82,7 +82,13 @@ export async function UnplayedMaps({ ))} - + ); } diff --git a/web/src/ui/Pager.tsx b/web/src/ui/Pager.tsx index 37cf00e..553035e 100644 --- a/web/src/ui/Pager.tsx +++ b/web/src/ui/Pager.tsx @@ -10,7 +10,8 @@ const PARAM = "page"; const cell = "inline-flex h-8 min-w-8 items-center justify-center rounded-md px-2 text-sm tabular-nums transition-colors duration-(--duration-fast)"; -const link = "text-text-muted hover:bg-text/8 hover:text-text"; +const link = "text-text-muted hover:bg-surface-raised hover:text-text"; +const number = new Intl.NumberFormat("en"); /** * Renders nothing when everything fits on one page. Pages count from 1 and sit in the address as `?page=`, so @@ -21,11 +22,16 @@ export function Pager({ page, pageCount, more = false, + total, + noun, href, }: { page: number; pageCount: number; more?: boolean; + /** How many items there are in all, shown before the page with `noun`: "3,599 runs". */ + total?: number; + noun?: string; href: (page: number) => string; }) { if (pageCount <= 1 && !more) return null; @@ -50,7 +56,9 @@ export function Pager({ aria-current={p === page ? "page" : undefined} className={classNames( cell, - p === page ? "bg-accent-soft text-accent font-bold" : link, + p === page + ? "bg-accent-soft text-accent ring-accent/30 font-bold ring-1 ring-inset" + : link, )} > {p} @@ -66,8 +74,9 @@ export function Pager({ className="flex flex-wrap items-center gap-x-4 gap-y-2" > - Page {page} - {!more && ` of ${pageCount}`} + {total != null && `${number.format(total)} ${noun} · `} + {total != null ? "page" : "Page"} {number.format(page)} + {!more && ` of ${number.format(pageCount)}`} + ); @@ -121,7 +136,7 @@ function Arrow({ }) { if (!href) return ( - + {children} ); From da2f4d719803577c88d7ce5dca82a20916d0a0a2 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:19:13 +1100 Subject: [PATCH 09/13] Fix the activity pager jumping back to the first-loaded page and calm its hovers --- web/src/features/activity/RecentRunTable.tsx | 31 +++++++++++++++----- web/src/features/activity/RecentRuns.tsx | 1 - web/src/ui/Pager.tsx | 4 +-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/web/src/features/activity/RecentRunTable.tsx b/web/src/features/activity/RecentRunTable.tsx index 84d4e99..9b6481f 100644 --- a/web/src/features/activity/RecentRunTable.tsx +++ b/web/src/features/activity/RecentRunTable.tsx @@ -50,7 +50,6 @@ export function RecentRunTable({ first, query, address, - page: firstPage, pageSize, styleNames, showStyle, @@ -61,7 +60,6 @@ export function RecentRunTable({ query: Query; /** The page's address with these filters, which the page number is added to. */ address: string; - page: number; pageSize: number; styleNames: Record; /** With every style shown, each run names its own. */ @@ -78,6 +76,7 @@ export function RecentRunTable({ cursor: first.next_cursor ?? null, }); const inFlight = useRef | null>(null); + const turns = useRef(0); const board = useRef(null); const rows = useRef(null); // Held at the first page's height, so a short last page doesn't pull the page controls up. @@ -85,9 +84,13 @@ export function RecentRunTable({ const tierCount = first.tier_count; // The address holds the page, so a reload, a shared link and the back button all land on it. - const asked = Number(useSearchParams().get("page") ?? firstPage); + // No `page` means page 1, never the page the table first opened on. + const asked = Math.max( + 1, + Math.floor(Number(useSearchParams().get("page") ?? 1)) || 1, + ); const pageCount = Math.max(1, Math.ceil(runs.length / pageSize)); - const page = Math.min(Math.max(1, Math.floor(asked) || 1), pageCount); + const page = Math.min(asked, pageCount); const shown = runs.slice((page - 1) * pageSize, page * pageSize); const pageHref = useCallback( @@ -122,23 +125,35 @@ export function RecentRunTable({ return inFlight.current; }, [query]); - // Keeps a few pages loaded past the one shown, so Next never waits. + // Keeps a few pages loaded past the one asked for, so Next never waits. An address past what's loaded, as + // after going back to a far page, walks forward to it the same way. useEffect(() => { - if (cursor && !failed && runs.length < (page + 4) * pageSize) + if (cursor && !failed && runs.length < (asked + 4) * pageSize) void loadMore(); - }, [cursor, failed, runs.length, page, pageSize, loadMore]); + }, [cursor, failed, runs.length, asked, pageSize, loadMore]); + + // Past the last run, the address is corrected to the last page rather than showing one page and naming another. + useEffect(() => { + if (!cursor && asked > pageCount) + window.history.replaceState(null, "", pageHref(pageCount)); + }, [cursor, asked, pageCount, pageHref]); async function go(target: number) { + const turn = ++turns.current; // A typed page past what's loaded walks forward to it, or to the last page if the runs end first. while (latest.current.runs.length < target * pageSize) { setWaiting(true); if (!(await loadMore())) break; } + // A later click wins over one still loading. + if (turn !== turns.current) return; setWaiting(false); setTurned(true); const count = Math.max(1, Math.ceil(latest.current.runs.length / pageSize)); + const next = pageHref(Math.min(target, count)); setMinHeight((held) => held ?? rows.current?.offsetHeight); - window.history.pushState(null, "", pageHref(Math.min(target, count))); + if (next !== window.location.pathname + window.location.search) + window.history.pushState(null, "", next); // Keep the table's top in view when a page is turned from the bottom of a long page. const top = board.current?.getBoundingClientRect().top ?? 0; if (top < 0) board.current?.scrollIntoView({ block: "start" }); diff --git a/web/src/features/activity/RecentRuns.tsx b/web/src/features/activity/RecentRuns.tsx index 8173c96..bd076dc 100644 --- a/web/src/features/activity/RecentRuns.tsx +++ b/web/src/features/activity/RecentRuns.tsx @@ -153,7 +153,6 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) { first={first} query={{ ...query, limit: CHUNK }} address={href({})} - page={page} pageSize={PAGE_SIZE} styleNames={Object.fromEntries( styles.keys.map((key) => [key, styles.name(key)]), diff --git a/web/src/ui/Pager.tsx b/web/src/ui/Pager.tsx index 553035e..2810fde 100644 --- a/web/src/ui/Pager.tsx +++ b/web/src/ui/Pager.tsx @@ -10,7 +10,7 @@ const PARAM = "page"; const cell = "inline-flex h-8 min-w-8 items-center justify-center rounded-md px-2 text-sm tabular-nums transition-colors duration-(--duration-fast)"; -const link = "text-text-muted hover:bg-surface-raised hover:text-text"; +const link = "text-text-muted hover:bg-text/6 hover:text-text"; const number = new Intl.NumberFormat("en"); /** @@ -116,7 +116,7 @@ export function Pager({ From c599f6dd0f91f4e3d5eb8556dbc684a53d86cf92 Mon Sep 17 00:00:00 2001 From: zasbu <136494048+zasbu@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:23:45 +1100 Subject: [PATCH 10/13] Light the pager's Go button only when a reachable page is typed --- web/src/ui/Pager.tsx | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/web/src/ui/Pager.tsx b/web/src/ui/Pager.tsx index 2810fde..77019c9 100644 --- a/web/src/ui/Pager.tsx +++ b/web/src/ui/Pager.tsx @@ -2,7 +2,7 @@ // a page. import { ChevronLeft, ChevronRight } from "lucide-react"; import Form from "next/form"; -import type { ReactNode } from "react"; +import { type ReactNode, useId } from "react"; import { classNames } from "./classNames"; import { Link } from "./Link"; @@ -34,6 +34,7 @@ export function Pager({ noun?: string; href: (page: number) => string; }) { + const box = useId(); if (pageCount <= 1 && !more) return null; const last = more ? pageCount + 1 : pageCount; const shown = [ @@ -102,21 +103,23 @@ export function Pager({ {[...base.searchParams].map(([name, value]) => ( ))} -
- {(pageCount > 1 || cursor) && ( + {(pageCount > 1 || more) && (
@@ -363,3 +418,14 @@ function mapHref(run: Schemas["RecentRun"]): string { if (run.track > 0) search.set("track", String(run.track)); return `/maps/${run.map}?${search}`; } + +function runsIn(loaded: RunChunks): number { + return Object.values(loaded.chunks).reduce( + (sum, runs) => sum + runs.length, + 0, + ); +} + +function pagesIn(loaded: RunChunks, pageSize: number): number { + return Math.max(1, Math.ceil((loaded.total ?? runsIn(loaded)) / pageSize)); +} diff --git a/web/src/features/activity/RecentRuns.tsx b/web/src/features/activity/RecentRuns.tsx index bd076dc..7673d9c 100644 --- a/web/src/features/activity/RecentRuns.tsx +++ b/web/src/features/activity/RecentRuns.tsx @@ -1,9 +1,9 @@ // The Activity page's runs: the filter menus, read from and written to the address, and the first page of runs. -import { api, type Schemas } from "@/api/client"; +import { api } from "@/api/client"; import { currentStyles } from "@/features/ruleset/styles"; import { ChoiceBar } from "@/ui/ChoiceBar"; import { ChoiceMenu } from "@/ui/ChoiceMenu"; -import { RecentRunTable } from "./RecentRunTable"; +import { RecentRunTable, type RunChunks } from "./RecentRunTable"; const tracks = [ { key: "all", label: "All" }, @@ -23,9 +23,9 @@ export interface RecentRunsParams { } const PAGE_SIZE = 15; -/** Runs per request. A hundred cost the API about what fifteen do, so most pages turn without a request. */ -const CHUNK = 100; -/** How far a typed page number may walk before giving up, at one request per hundred runs. */ +/** Runs per request, six pages: ninety cost the API about what fifteen do, so most pages turn without a request. */ +const CHUNK = 90; +/** How far an address may walk an API without offsets before giving up, at one request per chunk. */ const MAX_CHUNKS = 20; type RunsQuery = { @@ -67,7 +67,7 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) { ? guessed : await loadRuns(query, page); const filters: Filters = { style, track, tier, records }; - const tierCount = first?.tier_count ?? undefined; + const tierCount = first?.tierCount ?? undefined; // Only what differs from the defaults goes in the address, so the plain page stays /activity. const href = (choice: Partial) => { @@ -164,31 +164,49 @@ export async function RecentRuns({ params }: { params: RecentRunsParams }) { ); } -/** Runs from the newest, enough to reach `page`; undefined when the first request fails. */ +/** The chunk holding `page`; undefined when the first request fails. */ async function loadRuns( query: RunsQuery, page: number, -): Promise { - const get = (cursor?: string) => +): Promise { + const get = (from: { offset?: number; cursor?: string }) => api .GET("/v1/runs", { - params: { query: { ...query, limit: CHUNK, cursor } }, + params: { query: { ...query, limit: CHUNK, ...from } }, }) .then((result) => result.data) .catch(() => undefined); - const first = await get(); + const wanted = Math.floor(((page - 1) * PAGE_SIZE) / CHUNK); + const first = await get({ offset: wanted * CHUNK }); if (!first) return undefined; - const runs = [...first.runs]; - let cursor = first.next_cursor; + const tierCount = first.tier_count ?? null; + + // An API that counts its runs honours the offset, so this is the page's own chunk. One past the end is asked + // again for the last chunk. + if (typeof first.total === "number") { + const last = Math.max(0, Math.ceil(first.total / CHUNK) - 1); + const page = wanted > last ? await get({ offset: last * CHUNK }) : first; + if (!page) return undefined; + return { + chunks: { [Math.min(wanted, last)]: page.runs }, + total: page.total, + cursor: null, + tierCount, + }; + } + + // One that doesn't (the beta before `total`) ignored the offset and sent the newest runs, so walk forward. + const chunks: Record = { 0: first.runs }; + let cursor = first.next_cursor ?? null; for ( let chunk = 1; - cursor && runs.length < page * PAGE_SIZE && chunk < MAX_CHUNKS; + cursor && chunk <= wanted && chunk < MAX_CHUNKS; chunk++ ) { - const next = await get(cursor); + const next = await get({ cursor }); if (!next) break; - runs.push(...next.runs); - cursor = next.next_cursor; + chunks[chunk] = next.runs; + cursor = next.next_cursor ?? null; } - return { ...first, runs, next_cursor: cursor }; + return { chunks, total: null, cursor, tierCount }; }