"use client"; import { useCallback, useLayoutEffect, useRef, useState } from "react"; export const MIN_PAGE_SIZE = 10; export const MAX_PAGE_SIZE = 50; /** A two-line DataTable row in px (a member code above a name). Single-line rows are ~37px, so a page of * them leaves a little room rather than pushing the last rows under the pinned pager. */ export const ROW_PX = 49; /** Around the rows: the header row (40px), the table border (4px), the gap above the pager (12px), the * pinned pager (53px) and main's bottom padding (16px). */ export const TABLE_CHROME_PX = 125; /** Where a listing's table usually starts when it is not mounted yet (header, page title, filter bar). */ export const ESTIMATED_TABLE_TOP_PX = 280; /** Rows that fit between a table starting at `tableTop` and the bottom of a `viewportHeight` screen, 10–50. */ export function fitPageSize(viewportHeight: number, tableTop: number): number { const rows = Math.floor((viewportHeight - tableTop - TABLE_CHROME_PX) / ROW_PX); return Math.min(MAX_PAGE_SIZE, Math.max(MIN_PAGE_SIZE, rows)); } /** * Page size for a server-paged listing, fitted to the screen (fit-admin-listing-pages). Attach * `tableRef` to the listing's DataTable; `pageSize` is null until measured, so a listing must not * fetch before it is a number. Measured once per mount, before the first fetch. * ponytail: no re-measure on resize (a resize would change the rows behind the current page number); * add a debounced ResizeObserver that resets to page 1 if owners ask for it. */ export function useFitPageSize(): { tableRef: (el: HTMLElement | null) => void; pageSize: number | null } { const elRef = useRef(null); const [pageSize, setPageSize] = useState(null); const tableRef = useCallback((el: HTMLElement | null) => { elRef.current = el; }, []); useLayoutEffect(() => { const top = elRef.current ? elRef.current.getBoundingClientRect().top + window.scrollY : ESTIMATED_TABLE_TOP_PX; // eslint-disable-next-line react-hooks/set-state-in-effect -- measured once from layout, before the first fetch. setPageSize(fitPageSize(window.innerHeight, top)); }, []); return { tableRef, pageSize }; }