forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

capped-list.ts30 lines, 1014 bytesHistoryRawDownload
1import type { Page, PageParams } from "@/api/client";
2
3/** Default first-load size for list pages that filter and page in memory. */
4export const LIST_CAP = 100;
5
6export type CappedList<T> = {
7 items: T[];
8 /** Row count on the server, before the cap. */
9 total: number;
10};
11
12/**
13 * Fetch the first `cap` rows of a list, or every row once the user asks for
14 * it. The cap keeps big lists cheap; `total` lets the page say when rows were
15 * left out instead of hiding them.
16 */
17export async function loadCappedList<T>(
18 fetch: (params?: PageParams) => Promise<Page<T>>,
19 cap: number,
20 all: boolean,
21): Promise<CappedList<T>> {
22 const page = await fetch(all ? undefined : { limit: cap });
23 const items = Array.isArray(page.items) ? page.items : [];
24 const total = typeof page.total === "number" ? Math.max(page.total, items.length) : items.length;
25 return { items, total };
26}
27
28export function isTruncated(list: CappedList<unknown> | undefined): boolean {
29 return !!list && list.total > list.items.length;
30}