From 7ef0f9983f745aed7e962da1040ae867176e0037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Fri, 25 Sep 2026 14:51:26 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=93=9A=20read=20every=20page=20of?= =?UTF-8?q?=20exams,=20events=20and=20users=20instead=20of=20capping?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - loadAllPages reads the first page for the total, then the rest in parallel batches of three, in order; the backend pages these lists but cannot search or filter them, so the page still filters in memory - exams, events and admin users drop the "first 100 of N" notice and its load-all button; the pager counts every row - TruncationNotice had no callers left and is removed --- src/components/ui/truncation-notice.test.tsx | 27 ------------------- src/components/ui/truncation-notice.tsx | 26 ------------------ src/lib/capped-list.test.ts | 19 ++++++++++++- src/lib/capped-list.ts | 28 ++++++++++++++++++++ src/pages/admin-users-page.tsx | 15 +++-------- src/pages/events-page.test.tsx | 22 +++++++-------- src/pages/events-page.tsx | 13 ++------- src/pages/exams-page.tsx | 16 +++-------- 8 files changed, 64 insertions(+), 102 deletions(-) delete mode 100644 src/components/ui/truncation-notice.test.tsx delete mode 100644 src/components/ui/truncation-notice.tsx diff --git a/src/components/ui/truncation-notice.test.tsx b/src/components/ui/truncation-notice.test.tsx deleted file mode 100644 index d829b46..0000000 --- a/src/components/ui/truncation-notice.test.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; -import { afterEach, expect, test, vi } from "vitest"; -import { TruncationNotice } from "@/components/ui/truncation-notice"; -import { PreferencesProvider } from "@/stores/preferences-context"; - -afterEach(cleanup); - -test("says how many rows were left out and loads the rest on request", () => { - const onLoadAll = vi.fn(); - render(() => ( - - - - )); - expect(screen.getByRole("status").textContent).toContain("340"); - fireEvent.click(screen.getByRole("button")); - expect(onLoadAll).toHaveBeenCalledOnce(); -}); - -test("stays hidden when every row is on screen", () => { - const { container } = render(() => ( - - undefined} /> - - )); - expect(container.textContent).toBe(""); -}); diff --git a/src/components/ui/truncation-notice.tsx b/src/components/ui/truncation-notice.tsx deleted file mode 100644 index 354522b..0000000 --- a/src/components/ui/truncation-notice.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { Show } from "solid-js"; -import { Alert } from "@/components/ui/alert"; -import { Button } from "@/components/ui/button"; -import { useT } from "@/stores/preferences-context"; - -/** Says a capped list left rows out, with a way to fetch the rest. */ -export function TruncationNotice(props: { shown: number; total: number; loading?: boolean; onLoadAll: () => void }) { - const t = useT(); - return ( - props.shown}> - - {t("common.truncatedNotice", { shown: props.shown, total: props.total })} - - - - ); -} diff --git a/src/lib/capped-list.test.ts b/src/lib/capped-list.test.ts index ed3051e..f4510ce 100644 --- a/src/lib/capped-list.test.ts +++ b/src/lib/capped-list.test.ts @@ -1,5 +1,5 @@ import type { Page, PageParams } from "@/api/client"; -import { isTruncated, loadCappedList } from "./capped-list"; +import { isTruncated, loadAllPages, loadCappedList } from "./capped-list"; const page = (items: number[], total: number): Page => ({ items, total, limit: null, offset: 0 }); @@ -23,3 +23,20 @@ it("never reports a total below the rows it holds", async () => { expect(list.total).toBe(3); expect(isTruncated(undefined)).toBe(false); }); + +it("reads every page of a paged list, in order", async () => { + const all = Array.from({ length: 7 }, (_, index) => index); + const fetch = vi.fn(async (params?: PageParams) => { + const offset = params?.offset ?? 0; + return page(all.slice(offset, offset + (params?.limit ?? all.length)), all.length); + }); + expect(await loadAllPages(fetch, 3)).toEqual(all); + expect(fetch).toHaveBeenCalledTimes(3); + expect(fetch).toHaveBeenCalledWith({ limit: 3, offset: 6 }); +}); + +it("stops after one request when the first page holds everything", async () => { + const fetch = vi.fn(async () => page([1, 2], 2)); + expect(await loadAllPages(fetch, 100)).toEqual([1, 2]); + expect(fetch).toHaveBeenCalledTimes(1); +}); diff --git a/src/lib/capped-list.ts b/src/lib/capped-list.ts index 7682976..1d48caf 100644 --- a/src/lib/capped-list.ts +++ b/src/lib/capped-list.ts @@ -28,3 +28,31 @@ export async function loadCappedList( export function isTruncated(list: CappedList | undefined): boolean { return !!list && list.total > list.items.length; } + +/** Requests in flight at once while a list's later pages are read. */ +const PAGE_CONCURRENCY = 3; + +/** + * Every row of a list the backend can only page (`limit` / `offset`, no + * search or filter): the first page names the total, the rest are read in + * parallel, in order. The page then searches, filters and pages in memory + * over the whole list, so there is no "first 100 of 161" cut to explain. + */ +export async function loadAllPages( + fetch: (params?: PageParams) => Promise>, + pageSize: number = LIST_CAP, +): Promise { + const first = await fetch({ limit: pageSize, offset: 0 }); + const items = Array.isArray(first.items) ? [...first.items] : []; + const total = typeof first.total === "number" ? first.total : items.length; + if (items.length < pageSize || items.length >= total) return items; + const offsets: number[] = []; + for (let offset = items.length; offset < total; offset += pageSize) offsets.push(offset); + const pages: T[][] = new Array(offsets.length); + for (let start = 0; start < offsets.length; start += PAGE_CONCURRENCY) { + const batch = offsets.slice(start, start + PAGE_CONCURRENCY); + const results = await Promise.all(batch.map((offset) => fetch({ limit: pageSize, offset }))); + results.forEach((page, index) => { pages[start + index] = Array.isArray(page.items) ? page.items : []; }); + } + return items.concat(...pages); +} diff --git a/src/pages/admin-users-page.tsx b/src/pages/admin-users-page.tsx index dfe5ee4..ecd3ad5 100644 --- a/src/pages/admin-users-page.tsx +++ b/src/pages/admin-users-page.tsx @@ -15,8 +15,7 @@ import { Button } from "@/components/ui/button"; import { DataTableSkeleton } from "@/components/ui/data-table"; import { IconPlus } from "@/components/ui/icons"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; -import { TruncationNotice } from "@/components/ui/truncation-notice"; -import { loadCappedList } from "@/lib/capped-list"; +import { loadAllPages } from "@/lib/capped-list"; import { createFlash } from "@/lib/flash"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; @@ -32,7 +31,7 @@ export default function AdminUsersPage() { } /** Staff rosters run longer than other lists, so the first load holds more. */ -const USER_LIST_CAP = 200; +const USER_PAGE_SIZE = 200; function AdminUsersContent() { const auth = useAuth(); @@ -44,10 +43,8 @@ function AdminUsersContent() { const [creating, setCreating] = createSignal(false); const [editTarget, setEditTarget] = createSignal(null); - const [loadAll, setLoadAll] = createSignal(false); const [list, { refetch }] = createResource( - () => (loadAll() ? "all" : "capped"), - (scope) => loadCappedList(getUsers, USER_LIST_CAP, scope === "all"), + async () => ({ items: await loadAllPages(getUsers, USER_PAGE_SIZE) }), ); const allUsers = () => list()?.items ?? []; const visibleUsers = createMemo(() => { @@ -89,12 +86,6 @@ function AdminUsersContent() {
}> - setLoadAll(true)} - /> { vi.clearAllMocks(); }); -test("a capped event list says rows were left out and loads them on request", async () => { - getEvents.mockImplementation(async (params?: { limit?: number }) => { - const all = Array.from({ length: 3 }, (_, i) => event(i + 1)); - const items = params?.limit ? all.slice(0, 2) : all; - return { items, total: all.length, limit: params?.limit ?? null, offset: 0 }; +test("the event list reads every page instead of stopping at the first 100", async () => { + const all = Array.from({ length: 150 }, (_, i) => event(i + 1)); + getEvents.mockImplementation(async (params?: { limit?: number; offset?: number }) => { + const offset = params?.offset ?? 0; + const items = all.slice(offset, offset + (params?.limit ?? all.length)); + return { items, total: all.length, limit: params?.limit ?? null, offset }; }); render(() => ( @@ -50,12 +51,7 @@ test("a capped event list says rows were left out and loads them on request", as )); - const notice = await screen.findByRole("status"); - expect(getEvents).toHaveBeenCalledWith({ limit: 100 }); - expect(notice.textContent).toContain("3"); - - fireEvent.click(await screen.findByRole("button", { name: /Tümünü yükle|Load all/ })); - - await waitFor(() => expect(getEvents).toHaveBeenLastCalledWith(undefined)); - await waitFor(() => expect(screen.queryByRole("status")).toBeNull()); + await waitFor(() => expect(getEvents).toHaveBeenCalledWith({ limit: 100, offset: 100 })); + expect(getEvents).toHaveBeenCalledWith({ limit: 100, offset: 0 }); + expect(screen.queryByRole("button", { name: /Tümünü yükle|Load all/ })).toBeNull(); }); diff --git a/src/pages/events-page.tsx b/src/pages/events-page.tsx index 22c94dc..946ebb7 100644 --- a/src/pages/events-page.tsx +++ b/src/pages/events-page.tsx @@ -18,11 +18,10 @@ import { IconEdit, IconEye, IconPlus, IconTrash } from "@/components/ui/icons"; import { DropdownSelect } from "@/components/ui/select"; import { SidePanel } from "@/components/ui/side-panel"; import { TableRowActions } from "@/components/ui/table-row-actions"; -import { TruncationNotice } from "@/components/ui/truncation-notice"; import { cn } from "@/lib/cn"; import { createUrlEnum } from "@/lib/url-state"; import { createNow } from "@/lib/create-now"; -import { LIST_CAP, loadCappedList } from "@/lib/capped-list"; +import { loadAllPages } from "@/lib/capped-list"; import { createFlash } from "@/lib/flash"; import { formatDateTime } from "@/lib/format"; import { hasMinRole } from "@/lib/roles"; @@ -85,10 +84,8 @@ function EventsContent() { .sort(byDefault); }; - const [loadAll, setLoadAll] = createSignal(false); const [list, { refetch }] = createResource( - () => (loadAll() ? "all" : "capped"), - (scope) => loadCappedList(getEvents, LIST_CAP, scope === "all"), + async () => ({ items: await loadAllPages(getEvents) }), ); const rows = createMemo(() => filterEvents(list()?.items ?? [])); @@ -279,12 +276,6 @@ function EventsContent() { {formatApiError(list.error)} - setLoadAll(true)} - /> { if (sections() === undefined) return null; - return `${loadAll() ? "all" : "capped"}|${visibleCourses().map((row) => row.id).join(",")}`; + return `${visibleCourses().map((row) => row.id).join(",")}`; }, async () => { - const page = await loadCappedList(getExams, LIST_CAP, loadAll()); - const items = page.items; + const items = await loadAllPages(getExams); + const page = { items }; const known = new Map(visibleCourses().map((row) => [row.id, row.label])); const missing = [...new Set(items.map((exam) => exam.class_course))].filter((instanceId) => !known.has(instanceId)); if (missing.length > 0) { @@ -384,12 +382,6 @@ function ExamsContent() { {formatApiError(list.error)} - setLoadAll(true)} - />