@@ -22,25 +22,33 @@ import {
2222 useMemo ,
2323 useRef ,
2424 memo ,
25+ FunctionComponent ,
2526 useState ,
2627 ChangeEvent ,
2728 useEffect ,
29+ type RefObject ,
2830} from 'react' ;
2931
30- import { ThemedAgGridReact } from '@superset-ui/core/components' ;
32+ import { Constants , ThemedAgGridReact } from '@superset-ui/core/components' ;
3133import {
3234 AgGridReact ,
3335 AllCommunityModule ,
3436 ClientSideRowModelModule ,
3537 type ColDef ,
38+ type ColumnState ,
3639 ModuleRegistry ,
3740 GridReadyEvent ,
3841 GridState ,
3942 CellClickedEvent ,
4043 IMenuActionParams ,
4144} from '@superset-ui/core/components/ThemedAgGridReact' ;
42- import { type FunctionComponent } from 'react' ;
43- import { JsonObject , DataRecordValue , DataRecord , t } from '@superset-ui/core' ;
45+ import {
46+ AgGridChartState ,
47+ DataRecordValue ,
48+ DataRecord ,
49+ JsonObject ,
50+ t ,
51+ } from '@superset-ui/core' ;
4452import { SearchOutlined } from '@ant-design/icons' ;
4553import { debounce , isEqual } from 'lodash' ;
4654import Pagination from './components/Pagination' ;
@@ -49,6 +57,17 @@ import { SearchOption, SortByItem } from '../types';
4957import getInitialSortState , { shouldSort } from '../utils/getInitialSortState' ;
5058import { PAGE_SIZE_OPTIONS } from '../consts' ;
5159
60+ export interface AgGridState extends Partial < GridState > {
61+ timestamp ?: number ;
62+ hasChanges ?: boolean ;
63+ }
64+
65+ // AgGridChartState with optional metadata fields for state change events
66+ export type AgGridChartStateWithMetadata = Partial < AgGridChartState > & {
67+ timestamp ?: number ;
68+ hasChanges ?: boolean ;
69+ } ;
70+
5271export interface AgGridTableProps {
5372 gridTheme ?: string ;
5473 isDarkMode ?: boolean ;
@@ -80,6 +99,9 @@ export interface AgGridTableProps {
8099 cleanedTotals : DataRecord ;
81100 showTotals : boolean ;
82101 width : number ;
102+ onColumnStateChange ?: ( state : AgGridChartStateWithMetadata ) => void ;
103+ gridRef ?: RefObject < AgGridReact > ;
104+ chartState ?: AgGridChartState ;
83105}
84106
85107ModuleRegistry . registerModules ( [ AllCommunityModule , ClientSideRowModelModule ] ) ;
@@ -114,11 +136,14 @@ const AgGridDataTable: FunctionComponent<AgGridTableProps> = memo(
114136 cleanedTotals,
115137 showTotals,
116138 width,
139+ onColumnStateChange,
140+ chartState,
117141 } ) => {
118142 const gridRef = useRef < AgGridReact > ( null ) ;
119143 const inputRef = useRef < HTMLInputElement > ( null ) ;
120144 const rowData = useMemo ( ( ) => data , [ data ] ) ;
121145 const containerRef = useRef < HTMLDivElement > ( null ) ;
146+ const lastCapturedStateRef = useRef < string | null > ( null ) ;
122147
123148 const searchId = `search-${ id } ` ;
124149 const gridInitialState : GridState = {
@@ -211,6 +236,34 @@ const AgGridDataTable: FunctionComponent<AgGridTableProps> = memo(
211236
212237 if ( ! isSortable ) return ;
213238
239+ if ( serverPagination && gridRef . current ?. api && onColumnStateChange ) {
240+ const { api } = gridRef . current ;
241+
242+ if ( sortDir == null ) {
243+ api . applyColumnState ( {
244+ defaultState : { sort : null } ,
245+ } ) ;
246+ } else {
247+ api . applyColumnState ( {
248+ defaultState : { sort : null } ,
249+ state : [ { colId, sort : sortDir as 'asc' | 'desc' , sortIndex : 0 } ] ,
250+ } ) ;
251+ }
252+
253+ const columnState = api . getColumnState ?.( ) || [ ] ;
254+ const filterModel = api . getFilterModel ?.( ) || { } ;
255+ const sortModel = sortDir
256+ ? [ { colId, sort : sortDir as 'asc' | 'desc' , sortIndex : 0 } ]
257+ : [ ] ;
258+
259+ onColumnStateChange ( {
260+ columnState,
261+ sortModel,
262+ filterModel,
263+ timestamp : Date . now ( ) ,
264+ } ) ;
265+ }
266+
214267 if ( sortDir == null ) {
215268 onSortChange ( [ ] ) ;
216269 return ;
@@ -234,6 +287,51 @@ const AgGridDataTable: FunctionComponent<AgGridTableProps> = memo(
234287 [ serverPagination , gridInitialState , percentMetrics , onSortChange ] ,
235288 ) ;
236289
290+ const handleGridStateChange = useCallback (
291+ debounce ( ( ) => {
292+ if ( onColumnStateChange && gridRef . current ?. api ) {
293+ try {
294+ const { api } = gridRef . current ;
295+
296+ const columnState = api . getColumnState ? api . getColumnState ( ) : [ ] ;
297+
298+ const filterModel = api . getFilterModel ? api . getFilterModel ( ) : { } ;
299+
300+ const sortModel = columnState
301+ . filter ( col => col . sort )
302+ . map ( col => ( {
303+ colId : col . colId ,
304+ sort : col . sort as 'asc' | 'desc' ,
305+ sortIndex : col . sortIndex || 0 ,
306+ } ) )
307+ . sort ( ( a , b ) => ( a . sortIndex || 0 ) - ( b . sortIndex || 0 ) ) ;
308+
309+ const stateToSave = {
310+ columnState,
311+ sortModel,
312+ filterModel,
313+ timestamp : Date . now ( ) ,
314+ } ;
315+
316+ const stateHash = JSON . stringify ( {
317+ columnOrder : columnState . map ( c => c . colId ) ,
318+ sorts : sortModel ,
319+ filters : filterModel ,
320+ } ) ;
321+
322+ if ( stateHash !== lastCapturedStateRef . current ) {
323+ lastCapturedStateRef . current = stateHash ;
324+
325+ onColumnStateChange ( stateToSave ) ;
326+ }
327+ } catch ( error ) {
328+ console . warn ( 'Error capturing AG Grid state:' , error ) ;
329+ }
330+ }
331+ } , Constants . SLOW_DEBOUNCE ) ,
332+ [ onColumnStateChange ] ,
333+ ) ;
334+
237335 useEffect ( ( ) => {
238336 if (
239337 hasServerPageLengthChanged &&
@@ -257,6 +355,24 @@ const AgGridDataTable: FunctionComponent<AgGridTableProps> = memo(
257355 const onGridReady = ( params : GridReadyEvent ) => {
258356 // This will make columns fill the grid width
259357 params . api . sizeColumnsToFit ( ) ;
358+
359+ // Restore saved AG Grid state from permalink if available
360+ if ( chartState && params . api ) {
361+ try {
362+ if ( chartState . columnState ) {
363+ params . api . applyColumnState ?.( {
364+ state : chartState . columnState as ColumnState [ ] ,
365+ applyOrder : true ,
366+ } ) ;
367+ }
368+
369+ if ( chartState . filterModel ) {
370+ params . api . setFilterModel ?.( chartState . filterModel ) ;
371+ }
372+ } catch {
373+ // Silently fail if state restoration fails
374+ }
375+ }
260376 } ;
261377
262378 return (
@@ -313,7 +429,9 @@ const AgGridDataTable: FunctionComponent<AgGridTableProps> = memo(
313429 rowSelection = "multiple"
314430 animateRows
315431 onCellClicked = { handleCrossFilter }
432+ onStateUpdated = { handleGridStateChange }
316433 initialState = { gridInitialState }
434+ maintainColumnOrder
317435 suppressAggFuncInHeader
318436 enableCellTextSelection
319437 quickFilterText = { serverPagination ? '' : quickFilterText }
0 commit comments