"use client" /** Row model (lazy slicing) and active filter/rule column keys. */ import * as React from "react" import { ruleHasActiveValues } from "../../lib/conditional-rule-match" import { useIncrementalRender } from "../../lib/lazy-loading" import type { ColumnDef, ConditionalRule } from "./types" import type { TableState, RowGroup } from "./table-state-types" // ───────────────────────────────────────────────────────────────────────────── // Row model (paging, lazy slice, selection state) // ───────────────────────────────────────────────────────────────────────────── export interface LazyLoadingOptions { enabled?: boolean threshold?: number chunkSize?: number intervalMs?: number loadingLabel?: string } /** Keep leading groups' rows until `budget` rows are used. */ function sliceGroupsToBudget>( groups: RowGroup[], budget: number, ): RowGroup[] { let remaining = budget const next: RowGroup[] = [] for (const group of groups) { if (remaining <= 0) break const take = Math.min(group.rows.length, remaining) if (take > 0) next.push({ ...group, rows: group.rows.slice(0, take) }) remaining -= take } return next } export function useDataTableRowModel>({ state, lazyLoading, getRowIdProp, }: { state: TableState lazyLoading: LazyLoadingOptions | undefined getRowIdProp: ((row: TData, index: number) => string | number) | undefined }) { const { rows, pagedRows, groupedRows, getRowId, selected } = state const tableGroups = React.useMemo[]>( () => pagedRows !== rows ? [{ groupKey: null as string | null, groupLabel: null as string | null, rows: pagedRows }] : groupedRows, [groupedRows, pagedRows, rows], ) const totalRenderableRows = React.useMemo( () => tableGroups.reduce((sum, group) => sum + group.rows.length, 0), [tableGroups], ) const { renderCount, isLazyLoading } = useIncrementalRender(totalRenderableRows, { enabled: lazyLoading?.enabled ?? true, threshold: lazyLoading?.threshold, chunkSize: lazyLoading?.chunkSize, intervalMs: lazyLoading?.intervalMs, }) const displayedGroups = React.useMemo( () => (isLazyLoading ? sliceGroupsToBudget(tableGroups, renderCount) : tableGroups), [isLazyLoading, renderCount, tableGroups], ) // Row IDs for rows currently rendered (respects embedded cap / pagination / lazy slice). const visibleRows = React.useMemo( () => displayedGroups.flatMap(group => group.rows), [displayedGroups], ) const visibleRowIds = React.useMemo( () => visibleRows.map((r, i) => getRowId(r, i, getRowIdProp)), [visibleRows, getRowId, getRowIdProp], ) const allSelected = visibleRowIds.length > 0 && visibleRowIds.every(id => selected.has(id)) const someSelected = visibleRowIds.some(id => selected.has(id)) && !allSelected return { displayedGroups, isLazyLoading, lazyLoadingLabel: lazyLoading?.loadingLabel ?? "Loading more rows...", visibleRowIds, allSelected, someSelected, anySelected: selected.size > 0, } } // ───────────────────────────────────────────────────────────────────────────── // Header-indicator key sets // ───────────────────────────────────────────────────────────────────────────── /** * Column keys carrying a filter that actually narrows the rows. Mirrors the * `activeWithValues` gate in `use-table-state` — a filter row whose value is * still blank changes nothing, so it must not claim a header indicator. */ export function useFilteredColumnKeys(activeFilters: TableState>["activeFilters"]) { return React.useMemo(() => { const keys = new Set() for (const filter of activeFilters) { if (filter.values.some(value => String(value ?? "").trim().length > 0)) keys.add(filter.fieldKey) } return keys }, [activeFilters]) } /** Column keys with a conditional-formatting rule that can paint a cell. */ export function useConditionalRuleColumnKeys>( conditionalRules: ConditionalRule[] | undefined, columns: ColumnDef[], ) { return React.useMemo(() => { const keys = new Set() for (const rule of conditionalRules ?? []) { if (ruleHasActiveValues(rule, columns)) keys.add(rule.fieldKey) } return keys }, [conditionalRules, columns]) }