|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {TableChunk} from './TableChunk'; |
| 4 | +import {b} from './shared'; |
| 5 | +import type { |
| 6 | + Column, |
| 7 | + FetchData, |
| 8 | + GetRowClassName, |
| 9 | + PaginatedTableData, |
| 10 | + RenderEmptyDataMessage, |
| 11 | + RenderErrorMessage, |
| 12 | + SortParams, |
| 13 | +} from './types'; |
| 14 | +import {useScrollBasedChunks} from './useScrollBasedChunks'; |
| 15 | + |
| 16 | +export interface TableChunksRendererProps<T, F> { |
| 17 | + scrollContainerRef: React.RefObject<HTMLElement>; |
| 18 | + tableRef: React.RefObject<HTMLElement>; |
| 19 | + foundEntities: number; |
| 20 | + tableOffset: number; |
| 21 | + chunkSize: number; |
| 22 | + rowHeight: number; |
| 23 | + columns: Column<T>[]; |
| 24 | + fetchData: FetchData<T, F>; |
| 25 | + filters?: F; |
| 26 | + tableName: string; |
| 27 | + sortParams?: SortParams; |
| 28 | + getRowClassName?: GetRowClassName<T>; |
| 29 | + renderErrorMessage?: RenderErrorMessage; |
| 30 | + renderEmptyDataMessage?: RenderEmptyDataMessage; |
| 31 | + onDataFetched: (data?: PaginatedTableData<T>) => void; |
| 32 | + keepCache: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +export const TableChunksRenderer = <T, F>({ |
| 36 | + scrollContainerRef, |
| 37 | + tableRef, |
| 38 | + foundEntities, |
| 39 | + tableOffset, |
| 40 | + chunkSize, |
| 41 | + rowHeight, |
| 42 | + columns, |
| 43 | + fetchData, |
| 44 | + filters, |
| 45 | + tableName, |
| 46 | + sortParams, |
| 47 | + getRowClassName, |
| 48 | + renderErrorMessage, |
| 49 | + renderEmptyDataMessage, |
| 50 | + onDataFetched, |
| 51 | + keepCache, |
| 52 | +}: TableChunksRendererProps<T, F>) => { |
| 53 | + const chunkStates = useScrollBasedChunks({ |
| 54 | + scrollContainerRef, |
| 55 | + tableRef, |
| 56 | + totalItems: foundEntities || 1, |
| 57 | + rowHeight, |
| 58 | + chunkSize, |
| 59 | + tableOffset, |
| 60 | + }); |
| 61 | + |
| 62 | + const lastChunkSize = React.useMemo(() => { |
| 63 | + // If foundEntities = 0, there will only first chunk |
| 64 | + // Display it with 1 row, to display empty data message |
| 65 | + if (!foundEntities) { |
| 66 | + return 1; |
| 67 | + } |
| 68 | + return foundEntities % chunkSize || chunkSize; |
| 69 | + }, [foundEntities, chunkSize]); |
| 70 | + |
| 71 | + const findRenderChunkRange = React.useCallback(() => { |
| 72 | + const firstRenderIndex = chunkStates.findIndex((state) => state.shouldRender); |
| 73 | + const lastRenderIndex = chunkStates.findLastIndex((state) => state.shouldRender); |
| 74 | + return {firstRenderIndex, lastRenderIndex}; |
| 75 | + }, [chunkStates]); |
| 76 | + |
| 77 | + const findFetchChunkRange = React.useCallback(() => { |
| 78 | + const firstFetchIndex = chunkStates.findIndex((state) => state.shouldFetch); |
| 79 | + const lastFetchIndex = chunkStates.findLastIndex((state) => state.shouldFetch); |
| 80 | + return {firstFetchIndex, lastFetchIndex}; |
| 81 | + }, [chunkStates]); |
| 82 | + |
| 83 | + const calculateSeparatorHeight = React.useCallback( |
| 84 | + (startIndex: number, endIndex: number) => { |
| 85 | + let totalHeight = 0; |
| 86 | + for (let i = startIndex; i < endIndex; i++) { |
| 87 | + const currentChunkSize = i === chunkStates.length - 1 ? lastChunkSize : chunkSize; |
| 88 | + totalHeight += currentChunkSize * rowHeight; |
| 89 | + } |
| 90 | + return totalHeight; |
| 91 | + }, |
| 92 | + [chunkSize, chunkStates.length, lastChunkSize, rowHeight], |
| 93 | + ); |
| 94 | + |
| 95 | + const createSeparator = React.useCallback( |
| 96 | + (startIndex: number, endIndex: number, key: string) => { |
| 97 | + const height = calculateSeparatorHeight(startIndex, endIndex); |
| 98 | + return ( |
| 99 | + <tr style={{height: `${height}px`}} className={b(key)} key={key}> |
| 100 | + <td colSpan={columns.length} style={{padding: 0, border: 'none'}} /> |
| 101 | + </tr> |
| 102 | + ); |
| 103 | + }, |
| 104 | + [calculateSeparatorHeight, columns.length], |
| 105 | + ); |
| 106 | + |
| 107 | + const createChunk = React.useCallback( |
| 108 | + (chunkIndex: number) => { |
| 109 | + const chunkState = chunkStates[chunkIndex]; |
| 110 | + return ( |
| 111 | + <TableChunk<T, F> |
| 112 | + key={chunkIndex} |
| 113 | + id={chunkIndex} |
| 114 | + calculatedCount={ |
| 115 | + chunkIndex === chunkStates.length - 1 ? lastChunkSize : chunkSize |
| 116 | + } |
| 117 | + chunkSize={chunkSize} |
| 118 | + rowHeight={rowHeight} |
| 119 | + columns={columns} |
| 120 | + fetchData={fetchData} |
| 121 | + filters={filters} |
| 122 | + tableName={tableName} |
| 123 | + sortParams={sortParams} |
| 124 | + getRowClassName={getRowClassName} |
| 125 | + renderErrorMessage={renderErrorMessage} |
| 126 | + renderEmptyDataMessage={renderEmptyDataMessage} |
| 127 | + onDataFetched={onDataFetched} |
| 128 | + shouldFetch={chunkState.shouldFetch} |
| 129 | + shouldRender={chunkState.shouldRender} |
| 130 | + keepCache={keepCache} |
| 131 | + /> |
| 132 | + ); |
| 133 | + }, |
| 134 | + [ |
| 135 | + chunkSize, |
| 136 | + chunkStates, |
| 137 | + columns, |
| 138 | + fetchData, |
| 139 | + filters, |
| 140 | + getRowClassName, |
| 141 | + keepCache, |
| 142 | + lastChunkSize, |
| 143 | + onDataFetched, |
| 144 | + renderEmptyDataMessage, |
| 145 | + renderErrorMessage, |
| 146 | + rowHeight, |
| 147 | + sortParams, |
| 148 | + tableName, |
| 149 | + ], |
| 150 | + ); |
| 151 | + |
| 152 | + const renderChunks = React.useCallback(() => { |
| 153 | + // Chunk states are distrubuted like [fetch, fetch, render/fetch, render/fetch, fetch, fetch] |
| 154 | + // i.e. fetched chunks include rendered chunks |
| 155 | + const {firstFetchIndex, lastFetchIndex} = findFetchChunkRange(); |
| 156 | + const {firstRenderIndex, lastRenderIndex} = findRenderChunkRange(); |
| 157 | + const elements: React.ReactElement[] = []; |
| 158 | + |
| 159 | + // No fetch chunks found |
| 160 | + if (firstFetchIndex === -1) { |
| 161 | + return elements; |
| 162 | + } |
| 163 | + |
| 164 | + // Beginning separator (for chunks before first render chunk) |
| 165 | + if (firstRenderIndex > 0) { |
| 166 | + elements.push(createSeparator(0, firstRenderIndex, 'separator-beginning')); |
| 167 | + } |
| 168 | + |
| 169 | + // All fetch chunks (shouldFetch = true) get rendered as TableChunk components |
| 170 | + for (let i = firstFetchIndex; i <= lastFetchIndex; i++) { |
| 171 | + elements.push(createChunk(i)); |
| 172 | + } |
| 173 | + |
| 174 | + // End separator (for chunks after last render chunk) |
| 175 | + if (lastRenderIndex < chunkStates.length - 1) { |
| 176 | + elements.push( |
| 177 | + createSeparator(lastRenderIndex + 1, chunkStates.length, 'separator-end'), |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + return elements; |
| 182 | + }, [ |
| 183 | + chunkStates.length, |
| 184 | + createChunk, |
| 185 | + createSeparator, |
| 186 | + findFetchChunkRange, |
| 187 | + findRenderChunkRange, |
| 188 | + ]); |
| 189 | + |
| 190 | + return <React.Fragment>{renderChunks()}</React.Fragment>; |
| 191 | +}; |
0 commit comments