|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {TableChunk} from './TableChunk'; |
| 4 | +import type { |
| 5 | + Column, |
| 6 | + FetchData, |
| 7 | + GetRowClassName, |
| 8 | + PaginatedTableData, |
| 9 | + RenderEmptyDataMessage, |
| 10 | + RenderErrorMessage, |
| 11 | + SortParams, |
| 12 | +} from './types'; |
| 13 | + |
| 14 | +interface UseVirtualizedTbodiesProps<T, F> { |
| 15 | + activeChunks: boolean[]; |
| 16 | + chunkSize: number; |
| 17 | + lastChunkSize: number; |
| 18 | + rowHeight: number; |
| 19 | + columns: Column<T>[]; |
| 20 | + fetchData: FetchData<T, F>; |
| 21 | + filters?: F; |
| 22 | + tableName: string; |
| 23 | + sortParams?: SortParams; |
| 24 | + getRowClassName?: GetRowClassName<T>; |
| 25 | + renderErrorMessage?: RenderErrorMessage; |
| 26 | + renderEmptyDataMessage?: RenderEmptyDataMessage; |
| 27 | + onDataFetched: (data?: PaginatedTableData<T>) => void; |
| 28 | + keepCache?: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +export const useVirtualizedTbodies = <T, F>({ |
| 32 | + activeChunks, |
| 33 | + chunkSize, |
| 34 | + lastChunkSize, |
| 35 | + rowHeight, |
| 36 | + columns, |
| 37 | + fetchData, |
| 38 | + filters, |
| 39 | + tableName, |
| 40 | + sortParams, |
| 41 | + getRowClassName, |
| 42 | + renderErrorMessage, |
| 43 | + renderEmptyDataMessage, |
| 44 | + onDataFetched, |
| 45 | + keepCache = true, |
| 46 | +}: UseVirtualizedTbodiesProps<T, F>) => { |
| 47 | + // Reusable spacer tbody elements (max 2: before and after active chunks) |
| 48 | + const beforeSpacerRef = React.useRef<HTMLTableSectionElement>(null); |
| 49 | + const afterSpacerRef = React.useRef<HTMLTableSectionElement>(null); |
| 50 | + |
| 51 | + const renderChunks = React.useCallback(() => { |
| 52 | + const chunks: React.ReactElement[] = []; |
| 53 | + |
| 54 | + // Count empty start chunks |
| 55 | + let startEmptyCount = 0; |
| 56 | + while (startEmptyCount < activeChunks.length && !activeChunks[startEmptyCount]) { |
| 57 | + startEmptyCount++; |
| 58 | + } |
| 59 | + |
| 60 | + // Push start spacer if needed |
| 61 | + if (startEmptyCount > 0) { |
| 62 | + chunks.push( |
| 63 | + <tbody |
| 64 | + key="spacer-start" |
| 65 | + ref={beforeSpacerRef} |
| 66 | + style={{ |
| 67 | + height: `${startEmptyCount * chunkSize * rowHeight}px`, |
| 68 | + display: 'block', |
| 69 | + }} |
| 70 | + />, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + // Collect and push active chunks |
| 75 | + for (let i = startEmptyCount; i < activeChunks.length && activeChunks[i]; i++) { |
| 76 | + chunks.push( |
| 77 | + <TableChunk<T, F> |
| 78 | + key={i} |
| 79 | + id={i} |
| 80 | + calculatedCount={i === activeChunks.length - 1 ? lastChunkSize : chunkSize} |
| 81 | + chunkSize={chunkSize} |
| 82 | + rowHeight={rowHeight} |
| 83 | + columns={columns} |
| 84 | + fetchData={fetchData} |
| 85 | + filters={filters} |
| 86 | + tableName={tableName} |
| 87 | + sortParams={sortParams} |
| 88 | + getRowClassName={getRowClassName} |
| 89 | + renderErrorMessage={renderErrorMessage} |
| 90 | + renderEmptyDataMessage={renderEmptyDataMessage} |
| 91 | + onDataFetched={onDataFetched} |
| 92 | + isActive={true} |
| 93 | + keepCache={keepCache} |
| 94 | + />, |
| 95 | + ); |
| 96 | + startEmptyCount = i + 1; |
| 97 | + } |
| 98 | + |
| 99 | + // Count empty end chunks |
| 100 | + const endEmptyCount = activeChunks.length - startEmptyCount; |
| 101 | + |
| 102 | + // Push end spacer if needed |
| 103 | + if (endEmptyCount > 0) { |
| 104 | + chunks.push( |
| 105 | + <tbody |
| 106 | + key="spacer-end" |
| 107 | + ref={afterSpacerRef} |
| 108 | + style={{ |
| 109 | + height: `${endEmptyCount * chunkSize * rowHeight}px`, |
| 110 | + display: 'block', |
| 111 | + }} |
| 112 | + />, |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + return chunks; |
| 117 | + }, [ |
| 118 | + activeChunks, |
| 119 | + chunkSize, |
| 120 | + lastChunkSize, |
| 121 | + rowHeight, |
| 122 | + columns, |
| 123 | + fetchData, |
| 124 | + filters, |
| 125 | + tableName, |
| 126 | + sortParams, |
| 127 | + getRowClassName, |
| 128 | + renderErrorMessage, |
| 129 | + renderEmptyDataMessage, |
| 130 | + onDataFetched, |
| 131 | + keepCache, |
| 132 | + ]); |
| 133 | + |
| 134 | + return {renderChunks}; |
| 135 | +}; |
0 commit comments