@@ -15,7 +15,6 @@ import React, { useCallback, useContext, useEffect, useRef, useState } from 'rea
1515import { useSelectionService } from '../../hooks'
1616import { ContentContextMenu } from '../context-menu/content-context-menu'
1717import { DropFileArea } from '../DropFileArea'
18- import { contentColumnDefs } from './Cols/ColumnDefs.'
1918import { GridProps } from './Props/GridProps'
2019import { useGridLoading } from './Providers/GridLoadingProvider'
2120
@@ -44,7 +43,7 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
4443 const columnApi = useRef < ColumnApi | null > ( null )
4544 const gridRef = useRef < HTMLDivElement > ( null )
4645 const fixedColumns : string [ ] = [ '0' , 'Icon' , 'Actions' ]
47- const [ columnDefs , setColumnDefs ] = useState < ColDef [ ] > ( props . colDef ?? contentColumnDefs )
46+ const [ columnDefs , setColumnDefs ] = useState < ColDef [ ] > ( props . colDef )
4847 const loadingTimeoutRef = useRef < NodeJS . Timeout | null > ( null )
4948
5049 const setLoadingWithMinDuration = useCallback (
@@ -136,11 +135,32 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
136135 }
137136 }
138137
138+ const restoreSortModel = ( ) => {
139+ if ( columnApi . current ) {
140+ const sortModelString = localStorage . getItem ( `sortModel-${ props . gridKey } ` )
141+ if ( sortModelString ) {
142+ const sortModel = JSON . parse ( sortModelString )
143+ console . log ( 'props.gridKey:' , props . gridKey )
144+ console . log ( 'sortModel:' , sortModel )
145+ sortModel . forEach ( ( s : { colId : string ; sort : 'asc' | 'desc' } ) => {
146+ const col = columnApi . current ! . getColumnState ( ) . find ( ( c ) => c . colId === s . colId )
147+ if ( col ) {
148+ columnApi . current ! . applyColumnState ( {
149+ state : [ { colId : s . colId , sort : s . sort } ] ,
150+ applyOrder : false ,
151+ } )
152+ }
153+ } )
154+ }
155+ }
156+ }
157+
139158 const onGridReady = ( params : GridReadyEvent ) => {
140159 gridApi . current = params . api
141160 columnApi . current = params . columnApi
142161 restoreColumnFlexRatios ( )
143- setLoadingWithMinDuration ( false ) // Reset loading when grid is ready
162+ restoreSortModel ( )
163+ setLoadingWithMinDuration ( false )
144164 }
145165
146166 useEffect ( ( ) => {
@@ -149,6 +169,19 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
149169 }
150170 } , [ props . colDef ] )
151171
172+ const onSortChanged = useCallback ( ( ) => {
173+ if ( columnApi . current ) {
174+ const sortModel = columnApi . current
175+ . getColumnState ( )
176+ . filter ( ( c ) => c . sort )
177+ . map ( ( c ) => ( {
178+ colId : c . colId ,
179+ sort : c . sort ,
180+ } ) )
181+ localStorage . setItem ( `sortModel-${ props . gridKey } ` , JSON . stringify ( sortModel ) )
182+ }
183+ } , [ props . gridKey ] )
184+
152185 return (
153186 < DropFileArea parentContent = { parentContent } style = { { height : '100%' , overflow : 'hidden' } } >
154187 { isGridLoading && (
@@ -175,6 +208,7 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
175208 onSelectionChanged = { onSelectionChanged }
176209 onCellContextMenu = { ( event ) => onContextMenu ( event ) }
177210 onColumnResized = { onColumnResized }
211+ onSortChanged = { onSortChanged }
178212 suppressNoRowsOverlay = { true }
179213 />
180214 </ div >
0 commit comments