From 15be7687f0fe65523fd0b7ed2202be345c6b3dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Fri, 18 Sep 2026 11:41:41 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=E2=9C=82=EF=B8=8F=20say=20when=20a=20ca?= =?UTF-8?q?pped=20list=20left=20rows=20out=20-=20new=20loadCappedList:=20f?= =?UTF-8?q?irst=20load=20keeps=20the=20100-row=20cap=20from=2061d2048=20bu?= =?UTF-8?q?t=20=20=20returns=20the=20server=20total,=20and=20fetches=20eve?= =?UTF-8?q?ry=20row=20on=20request=20-=20TruncationNotice=20shows=20"first?= =?UTF-8?q?=20N=20of=20total"=20with=20a=20Load=20all=20button,=20above=20?= =?UTF-8?q?=20=20the=20events,=20exams=20and=20admin=20users=20tables=20(u?= =?UTF-8?q?sers=20keep=20their=20200=20cap)=20-=20tests=20for=20the=20help?= =?UTF-8?q?er,=20the=20notice=20and=20the=20events=20page=20round=20trip?= =?UTF-8?q?=20-=20other=20capped=20reads=20(dashboard,=20calendar,=20payme?= =?UTF-8?q?nts,=20meal=20detail,=20terms=20and=20=20=20more)=20still=20cap?= =?UTF-8?q?=20silently;=20they=20follow=20in=20later=20layer=20A=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ui/truncation-notice.test.tsx | 27 +++++++++ src/components/ui/truncation-notice.tsx | 26 +++++++++ src/i18n/messages.ts | 6 ++ src/lib/capped-list.test.ts | 25 ++++++++ src/lib/capped-list.ts | 30 ++++++++++ src/pages/admin-users-page.tsx | 19 +++++- src/pages/events-page.test.tsx | 61 ++++++++++++++++++++ src/pages/events-page.tsx | 16 ++++- src/pages/exams-page.tsx | 18 ++++-- 9 files changed, 220 insertions(+), 8 deletions(-) create mode 100644 src/components/ui/truncation-notice.test.tsx create mode 100644 src/components/ui/truncation-notice.tsx create mode 100644 src/lib/capped-list.test.ts create mode 100644 src/lib/capped-list.ts create mode 100644 src/pages/events-page.test.tsx diff --git a/src/components/ui/truncation-notice.test.tsx b/src/components/ui/truncation-notice.test.tsx new file mode 100644 index 0000000..d829b46 --- /dev/null +++ b/src/components/ui/truncation-notice.test.tsx @@ -0,0 +1,27 @@ +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 new file mode 100644 index 0000000..354522b --- /dev/null +++ b/src/components/ui/truncation-notice.tsx @@ -0,0 +1,26 @@ +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/i18n/messages.ts b/src/i18n/messages.ts index 9bfcb7a..fea7f67 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -614,6 +614,8 @@ export type MessageKey = | "common.created" | "common.deleted" | "common.saved" + | "common.loadAll" + | "common.truncatedNotice" | "common.createItem" | "common.countItem" | "pool.title" @@ -2740,6 +2742,8 @@ const en: Dict = { "common.created": "Created.", "common.deleted": "Deleted.", "common.saved": "Saved successfully.", + "common.loadAll": "Load all", + "common.truncatedNotice": "Showing the first {shown} of {total} records.", "common.createItem": "Create new {item}", "common.countItem": "{count} {item}", "pool.title": "Question pool", @@ -4868,6 +4872,8 @@ const tr: Dict = { "common.created": "Oluşturuldu.", "common.deleted": "Silindi.", "common.saved": "Başarıyla kaydedildi.", + "common.loadAll": "Tümünü yükle", + "common.truncatedNotice": "{total} kayıttan ilk {shown} tanesi gösteriliyor.", "common.createItem": "Yeni {item} oluştur", "common.countItem": "{count} {item}", "pool.title": "Soru havuzu", diff --git a/src/lib/capped-list.test.ts b/src/lib/capped-list.test.ts new file mode 100644 index 0000000..ed3051e --- /dev/null +++ b/src/lib/capped-list.test.ts @@ -0,0 +1,25 @@ +import type { Page, PageParams } from "@/api/client"; +import { isTruncated, loadCappedList } from "./capped-list"; + +const page = (items: number[], total: number): Page => ({ items, total, limit: null, offset: 0 }); + +it("asks for the cap and reports the server total", async () => { + const fetch = vi.fn(async (_params?: PageParams) => page([1, 2], 5)); + const list = await loadCappedList(fetch, 2, false); + expect(fetch).toHaveBeenCalledWith({ limit: 2 }); + expect(list).toEqual({ items: [1, 2], total: 5 }); + expect(isTruncated(list)).toBe(true); +}); + +it("fetches every row without a limit when asked for all", async () => { + const fetch = vi.fn(async (_params?: PageParams) => page([1, 2, 3, 4, 5], 5)); + const list = await loadCappedList(fetch, 2, true); + expect(fetch).toHaveBeenCalledWith(undefined); + expect(isTruncated(list)).toBe(false); +}); + +it("never reports a total below the rows it holds", async () => { + const list = await loadCappedList(async () => page([1, 2, 3], 1), 10, false); + expect(list.total).toBe(3); + expect(isTruncated(undefined)).toBe(false); +}); diff --git a/src/lib/capped-list.ts b/src/lib/capped-list.ts new file mode 100644 index 0000000..7682976 --- /dev/null +++ b/src/lib/capped-list.ts @@ -0,0 +1,30 @@ +import type { Page, PageParams } from "@/api/client"; + +/** Default first-load size for list pages that filter and page in memory. */ +export const LIST_CAP = 100; + +export type CappedList = { + items: T[]; + /** Row count on the server, before the cap. */ + total: number; +}; + +/** + * Fetch the first `cap` rows of a list, or every row once the user asks for + * it. The cap keeps big lists cheap; `total` lets the page say when rows were + * left out instead of hiding them. + */ +export async function loadCappedList( + fetch: (params?: PageParams) => Promise>, + cap: number, + all: boolean, +): Promise> { + const page = await fetch(all ? undefined : { limit: cap }); + const items = Array.isArray(page.items) ? page.items : []; + const total = typeof page.total === "number" ? Math.max(page.total, items.length) : items.length; + return { items, total }; +} + +export function isTruncated(list: CappedList | undefined): boolean { + return !!list && list.total > list.items.length; +} diff --git a/src/pages/admin-users-page.tsx b/src/pages/admin-users-page.tsx index a38227b..6d9fc3e 100644 --- a/src/pages/admin-users-page.tsx +++ b/src/pages/admin-users-page.tsx @@ -15,6 +15,8 @@ import { ComingSoonBadge } from "@/components/ui/coming-soon"; 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 { createFlash } from "@/lib/flash"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; @@ -29,6 +31,9 @@ export default function AdminUsersPage() { ); } +/** Staff rosters run longer than other lists, so the first load holds more. */ +const USER_LIST_CAP = 200; + function AdminUsersContent() { const auth = useAuth(); const navigate = useNavigate(); @@ -38,8 +43,12 @@ function AdminUsersContent() { const [roleTab, setRoleTab] = createSignal("all"); const [creating, setCreating] = createSignal(false); - const [list, { refetch }] = createResource(async () => (await getUsers({ limit: 200 })).items); - const allUsers = () => list() ?? []; + const [loadAll, setLoadAll] = createSignal(false); + const [list, { refetch }] = createResource( + () => (loadAll() ? "all" : "capped"), + (scope) => loadCappedList(getUsers, USER_LIST_CAP, scope === "all"), + ); + const allUsers = () => list()?.items ?? []; const visibleUsers = createMemo(() => { const tab = roleTab(); if (tab === "all") return allUsers(); @@ -83,6 +92,12 @@ function AdminUsersContent() {
}> + setLoadAll(true)} + /> ({ getEvents: vi.fn() })); + +vi.mock("@tanstack/solid-router", () => ({ + Link: (props: { to: string; children: JSX.Element; class?: string }) => {props.children}, + Navigate: () => null, + useNavigate: () => vi.fn(), + useLocation: () => () => ({ pathname: "/events", searchStr: "" }), +})); +vi.mock("@/stores/auth-context", () => ({ + useAuth: () => ({ + user: () => ({ id: "student-1", username: "student", role: "student" }), + loading: () => false, + error: () => undefined, + }), +})); +vi.mock("@/api/events", () => ({ getEvents, postEvent: vi.fn() })); +vi.mock("@/api/courses", () => ({ getCourses: vi.fn() })); +vi.mock("@/api/time", () => ({ getTime: vi.fn(async () => ({ now: Date.now() })) })); + +const event = (n: number) => ({ + id: `event-${n}`, + creator: "teacher-1", + title: `Event ${n}`, + description: "", + audience: { kind: "school" }, + starts_at: null, + ends_at: null, +}); + +afterEach(() => { + cleanup(); + 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 }; + }); + + render(() => ( + + + + )); + + 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()); +}); diff --git a/src/pages/events-page.tsx b/src/pages/events-page.tsx index 8d213da..0c6ab66 100644 --- a/src/pages/events-page.tsx +++ b/src/pages/events-page.tsx @@ -16,8 +16,10 @@ import { IconEye, IconPlus } 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 { createNow } from "@/lib/create-now"; +import { LIST_CAP, loadCappedList } from "@/lib/capped-list"; import { createFlash } from "@/lib/flash"; import { formatDateTime } from "@/lib/format"; import { hasMinRole } from "@/lib/roles"; @@ -64,8 +66,12 @@ function EventsContent() { }); }; - const [list, { refetch }] = createResource(async () => (await getEvents({ limit: 100 })).items); - const rows = () => filterEvents(list() ?? []); + const [loadAll, setLoadAll] = createSignal(false); + const [list, { refetch }] = createResource( + () => (loadAll() ? "all" : "capped"), + (scope) => loadCappedList(getEvents, LIST_CAP, scope === "all"), + ); + const rows = () => filterEvents(list()?.items ?? []); const eventStatus = (event: Event) => { const nowMs = now(); if (event.ends_at != null && event.ends_at < nowMs) return "past"; @@ -196,6 +202,12 @@ function EventsContent() { {formatApiError(list.error)} + setLoadAll(true)} + /> { if (sections() === undefined) return null; - return visibleCourses().map((row) => row.id).join(","); + return `${loadAll() ? "all" : "capped"}|${visibleCourses().map((row) => row.id).join(",")}`; }, async () => { - const items = (await getExams({ limit: 100 })).items; + const page = await loadCappedList(getExams, LIST_CAP, loadAll()); + const items = 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) { @@ -150,7 +154,7 @@ function ExamsContent() { ); } setCourseMap(Object.fromEntries(known)); - return items; + return page; }, ); @@ -168,7 +172,7 @@ function ExamsContent() { const statusLabel = (status: ExamDisplayStatus) => { return t(examStatusMessageKey(status)); }; - const rows = (): ExamRow[] => filterExams(list() ?? []).map((exam) => ({ ...exam, displayStatus: examStatus(exam) })); + const rows = (): ExamRow[] => filterExams(list()?.items ?? []).map((exam) => ({ ...exam, displayStatus: examStatus(exam) })); const columns = createMemo[]>(() => [ { accessorKey: "title", @@ -309,6 +313,12 @@ function ExamsContent() { {formatApiError(list.error)} + setLoadAll(true)} + />