import { useMemo } from 'react' type UsePlaceholderRowsOptions = { /** Actual data to display when loaded */ data: TData[] /** Loading state */ isLoading: boolean /** Page size for dynamic placeholder count (from pagination) */ pageSize?: number /** Custom placeholder count - if provided, overrides all other placeholder count logic */ placeholderCount?: number } type UsePlaceholderRowsResult = { /** Data array - either actual data or placeholder array of nulls */ data: TData[] | null[] /** Loading state */ isLoading: boolean } /** * Hook that generates placeholder rows for DataTable loading states. * * Placeholder count priority when loading: * 1. `placeholderCount` - if provided, always uses this value (overrides everything) * 2. If `data` has items - uses `data.length` * 3. If `pageSize` is provided (from pagination) - uses `pageSize` * 4. Default: 5 * * @example * ```tsx * const tableData = usePlaceholderRows({ * data: myData, * isLoading: isLoading, * pageSize: 10, * }) * * * ``` */ export function usePlaceholderRows( options: UsePlaceholderRowsOptions, ): UsePlaceholderRowsResult { const { data, isLoading, pageSize, placeholderCount } = options const result = useMemo(() => { // If loading, return placeholder array if (isLoading) { // Priority: placeholderCount > data.length > pageSize > default (5) let count: number if (placeholderCount !== undefined) { // 1. placeholderCount overrides everything count = placeholderCount } else if (data.length > 0) { // 2. If data has items, use its length count = data.length } else if (pageSize !== undefined) { // 3. If pagination is provided, use pageSize count = pageSize } else { // 4. Default to 5 count = 5 } return { data: new Array(count).fill(null) as null[], isLoading: true, } } // If not loading, return actual data return { data, isLoading: false, } }, [data, isLoading, pageSize, placeholderCount]) return result }