( cellId: Id | undefined, descending = false, sortOnClick: boolean | undefined, offset = 0, limit: number | undefined, total: number, paginator: boolean | ComponentType<SortedTablePaginatorProps>, onChange?: (sortAndOffset: SortAndOffset) => void, )
| 14 | const RIGHT_ARROW = '\u2192'; |
| 15 | |
| 16 | export const useSortingAndPagination = ( |
| 17 | cellId: Id | undefined, |
| 18 | descending = false, |
| 19 | sortOnClick: boolean | undefined, |
| 20 | offset = 0, |
| 21 | limit: number | undefined, |
| 22 | total: number, |
| 23 | paginator: boolean | ComponentType<SortedTablePaginatorProps>, |
| 24 | onChange?: (sortAndOffset: SortAndOffset) => void, |
| 25 | ): [ |
| 26 | sortAndOffset: SortAndOffset, |
| 27 | handleSort: HandleSort, |
| 28 | paginatorComponent: ReactNode | undefined, |
| 29 | ] => { |
| 30 | const [[currentCellId, currentDescending, currentOffset], setState] = |
| 31 | useState<SortAndOffset>([cellId, descending, offset]); |
| 32 | const setStateAndChange = useCallback( |
| 33 | (sortAndOffset: SortAndOffset) => { |
| 34 | setState(sortAndOffset); |
| 35 | onChange?.(sortAndOffset); |
| 36 | }, |
| 37 | [onChange], |
| 38 | ); |
| 39 | const handleSort = useCallbackOrUndefined( |
| 40 | (cellId: Id | undefined) => |
| 41 | setStateAndChange([ |
| 42 | cellId, |
| 43 | cellId == currentCellId ? !currentDescending : false, |
| 44 | currentOffset, |
| 45 | ]), |
| 46 | [setStateAndChange, currentCellId, currentDescending, currentOffset], |
| 47 | sortOnClick, |
| 48 | ); |
| 49 | const handleChangeOffset = useCallback( |
| 50 | (offset: number) => |
| 51 | setStateAndChange([currentCellId, currentDescending, offset]), |
| 52 | [setStateAndChange, currentCellId, currentDescending], |
| 53 | ); |
| 54 | const PaginatorComponent = isTrue(paginator) |
| 55 | ? SortedTablePaginator |
| 56 | : (paginator as ComponentType<SortedTablePaginatorProps>); |
| 57 | return [ |
| 58 | [currentCellId, currentDescending, currentOffset], |
| 59 | handleSort, |
| 60 | useMemo( |
| 61 | () => |
| 62 | isFalse(paginator) ? null : ( |
| 63 | <PaginatorComponent |
| 64 | offset={currentOffset} |
| 65 | limit={limit} |
| 66 | total={total} |
| 67 | onChange={handleChangeOffset} |
| 68 | /> |
| 69 | ), |
| 70 | [ |
| 71 | paginator, |
| 72 | PaginatorComponent, |
| 73 | currentOffset, |
no test coverage detected
searching dependent graphs…