Files
co-lite/solvway-admin-portal/app/lib/hooks/useFitPageSize.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

43 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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<HTMLElement | null>(null);
const [pageSize, setPageSize] = useState<number | null>(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 };
}