11import { SetStateAction , useEffect , useRef , useState } from 'react' ;
22import { Table , TableBody , TableCell , TableHead , TableHeader , TableRow } from "@/components/ui/table" ;
33import { getCoreRowModel , useReactTable , flexRender , getFilteredRowModel , getPaginationRowModel , getExpandedRowModel , getSortedRowModel } from '@tanstack/react-table' ;
4- import type { ColumnDef , Row , PaginationState , RowSelectionState , ColumnFiltersState , ExpandedState , ColumnDefTemplate , HeaderContext } from '@tanstack/react-table' ;
4+ import type { ColumnDef , Row , PaginationState , RowSelectionState , ColumnFiltersState , ExpandedState , ColumnDefTemplate , HeaderContext , SortingState } from '@tanstack/react-table' ;
55import { cn } from '@/lib/utils' ;
66import { Select , SelectContent , SelectGroup , SelectItem , SelectLabel } from './select' ;
77import { Button } from './button' ;
@@ -41,6 +41,15 @@ export function getHeader<T>(name: string, translate: boolean = true, addSorting
4141 }
4242}
4343
44+ function getStoredSortingState ( key ?: string ) {
45+ if ( key ) {
46+ let sortingString = localStorage . getItem ( key ) ;
47+ if ( sortingString ) {
48+ return JSON . parse ( sortingString ) ;
49+ }
50+ }
51+ }
52+
4453interface ReactTableProps < T extends object > {
4554 data : T [ ] ;
4655 columns : ColumnDef < T > [ ] ;
@@ -58,6 +67,7 @@ interface ReactTableProps<T extends object> {
5867 filters ?: { id : string , value : string } [ ] ;
5968 maxHeight ?: string | number ;
6069 expandable ?: boolean ;
70+ storeSortingState ?: string ;
6171}
6272
6373function SimpleTable < T extends object > ( {
@@ -75,7 +85,8 @@ function SimpleTable<T extends object>({
7585 allowMultiSelect,
7686 filters,
7787 maxHeight,
78- expandable
88+ expandable,
89+ storeSortingState
7990} : ReactTableProps < T > ) {
8091 const [ pagination , setPagination ] = useState < PaginationState > ( {
8192 pageIndex : pageIndex ?? 0 ,
@@ -84,6 +95,7 @@ function SimpleTable<T extends object>({
8495 const [ rowSelection , setRowSelection ] = useState < RowSelectionState > ( initialRowSelection || { } ) ;
8596 const [ columnFilters , setColumnFilters ] = useState < ColumnFiltersState > ( filters || [ ] )
8697 const [ expanded , setExpanded ] = useState < ExpandedState > ( { } ) ;
98+ const [ sorting , setSorting ] = useState < SortingState > ( getStoredSortingState ( storeSortingState ) || [ ] ) ;
8799
88100 const table = useReactTable ( {
89101 data,
@@ -98,7 +110,8 @@ function SimpleTable<T extends object>({
98110 pagination,
99111 rowSelection,
100112 columnFilters,
101- expanded
113+ expanded,
114+ sorting
102115 } ,
103116 getFilteredRowModel : getFilteredRowModel ( ) ,
104117 onColumnFiltersChange : setColumnFilters ,
@@ -107,6 +120,7 @@ function SimpleTable<T extends object>({
107120 if ( allowSelect || allowSelectCheckbox || allowMultiSelect ) setRowSelection ( arg ) ;
108121 } ,
109122 onExpandedChange : setExpanded ,
123+ onSortingChange : setSorting ,
110124 getSubRows : ( row : any ) => row ?. subRows ,
111125 } ) ;
112126
@@ -133,6 +147,12 @@ function SimpleTable<T extends object>({
133147 }
134148 } , [ filters ] )
135149
150+ useEffect ( ( ) => {
151+ if ( storeSortingState ) {
152+ localStorage . setItem ( storeSortingState , JSON . stringify ( sorting ) ) ;
153+ }
154+ } , [ sorting ] ) ;
155+
136156 // For some reason the ScrollArea scrollbar is only shown when it's set to a specific height.
137157 // So, we wrap it in a parent div, monitor its size, and set the height of the table accordingly.
138158 const parentRef = useRef < HTMLTableElement > ( null ) ;
0 commit comments