Raw
1 import {
2 ColumnDef,
3 OnChangeFn,
4 PaginationState,
5 Row,
6 RowSelectionState,
7 getCoreRowModel,
8 getExpandedRowModel,
9 getPaginationRowModel,
10 useReactTable,
11 } from "@tanstack/react-table"
12 import { useEffect, useMemo, useState } from "react"
13 import { useSearchParams } from "react-router-dom"
14
15 type UseDataTableProps<TData> = {
16 data?: TData[]
17 columns: ColumnDef<TData, any>[]
18 count?: number
19 pageSize?: number
20 enableRowSelection?: boolean | ((row: Row<TData>) => boolean)
21 rowSelection?: {
22 state: RowSelectionState
23 updater: OnChangeFn<RowSelectionState>
24 }
25 enablePagination?: boolean
26 enableExpandableRows?: boolean
27 getRowId?: (original: TData, index: number) => string
28 getSubRows?: (original: TData) => TData[]
29 meta?: Record<string, unknown>
30 prefix?: string
31 }
32
33 export const useDataTable = <TData,>({
34 data = [],
35 columns,
36 count = 0,
37 pageSize: _pageSize = 20,
38 enablePagination = true,
39 enableRowSelection = false,
40 enableExpandableRows = false,
41 rowSelection: _rowSelection,
42 getSubRows,
43 getRowId,
44 meta,
45 prefix,
46 }: UseDataTableProps<TData>) => {
47 const [searchParams, setSearchParams] = useSearchParams()
48 const offsetKey = `${prefix ? `${prefix}_` : ""}offset`
49 const offset = searchParams.get(offsetKey)
50
51 const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
52 pageIndex: offset ? Math.ceil(Number(offset) / _pageSize) : 0,
53 pageSize: _pageSize,
54 })
55 const pagination = useMemo(
56 () => ({
57 pageIndex,
58 pageSize,
59 }),
60 [pageIndex, pageSize]
61 )
62 const [localRowSelection, setLocalRowSelection] = useState({})
63 const rowSelection = _rowSelection?.state ?? localRowSelection
64 const setRowSelection = _rowSelection?.updater ?? setLocalRowSelection
65
66 useEffect(() => {
67 if (!enablePagination) {
68 return
69 }
70
71 const index = offset ? Math.ceil(Number(offset) / _pageSize) : 0
72
73 if (index === pageIndex) {
74 return
75 }
76
77 setPagination((prev) => ({
78 ...prev,
79 pageIndex: index,
80 }))
81 }, [offset, enablePagination, _pageSize, pageIndex])
82
83 const onPaginationChange = (
84 updater: (old: PaginationState) => PaginationState
85 ) => {
86 const state = updater(pagination)
87 const { pageIndex, pageSize } = state
88
89 setSearchParams((prev) => {
90 if (!pageIndex) {
91 prev.delete(offsetKey)
92 return prev
93 }
94
95 const newSearch = new URLSearchParams(prev)
96 newSearch.set(offsetKey, String(pageIndex * pageSize))
97
98 return newSearch
99 })
100
101 setPagination(state)
102 return state
103 }
104
105 const table = useReactTable({
106 data,
107 columns,
108 state: {
109 rowSelection: rowSelection, // We always pass a selection state to the table even if it's not enabled
110 pagination: enablePagination ? pagination : undefined,
111 },
112 pageCount: Math.ceil((count ?? 0) / pageSize),
113 enableRowSelection,
114 getRowId,
115 getSubRows,
116 onRowSelectionChange: enableRowSelection ? setRowSelection : undefined,
117 onPaginationChange: enablePagination
118 ? (onPaginationChange as OnChangeFn<PaginationState>)
119 : undefined,
120 getCoreRowModel: getCoreRowModel(),
121 getPaginationRowModel: enablePagination
122 ? getPaginationRowModel()
123 : undefined,
124 getExpandedRowModel: enableExpandableRows
125 ? getExpandedRowModel()
126 : undefined,
127 manualPagination: enablePagination ? true : undefined,
128 meta,
129 })
130
131 return { table }
132 }