@@ -14,8 +14,10 @@ import {
1414 type IrisGridContextMenuData ,
1515 IrisGridProps ,
1616 IrisGridUtils ,
17+ IrisGridCacheUtils ,
1718 IrisGridState ,
18- DehydratedIrisGridState ,
19+ type DehydratedIrisGridState ,
20+ type DehydratedGridState ,
1921} from '@deephaven/iris-grid' ;
2022import {
2123 ColorValues ,
@@ -30,8 +32,9 @@ import { TableUtils } from '@deephaven/jsapi-utils';
3032import type { dh as DhType } from '@deephaven/jsapi-types' ;
3133import Log from '@deephaven/log' ;
3234import { getSettings , RootState } from '@deephaven/redux' ;
33- import { GridMouseHandler } from '@deephaven/grid' ;
35+ import { GridMouseHandler , GridState } from '@deephaven/grid' ;
3436import { EMPTY_ARRAY , ensureArray } from '@deephaven/utils' ;
37+ import { usePersistentState } from '@deephaven/plugin' ;
3538import {
3639 DatabarConfig ,
3740 FormattingRule ,
@@ -45,7 +48,6 @@ import UITableContextMenuHandler, {
4548} from './UITableContextMenuHandler' ;
4649import UITableModel , { makeUiTableModel } from './UITableModel' ;
4750import { UITableLayoutHints } from './JsTableProxy' ;
48- import usePersistentState from '../hooks/usePersistentState' ;
4951
5052const log = Log . module ( '@deephaven/js-plugin-ui/UITable' ) ;
5153
@@ -176,8 +178,6 @@ export function UITable({
176178 databars = EMPTY_ARRAY as unknown as DatabarConfig [ ] ,
177179 ...userStyleProps
178180} : UITableProps ) : JSX . Element | null {
179- const id = useMemo ( ( ) => Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) , [ ] ) ;
180- console . log ( 'render table' , id ) ;
181181 const [ throwError ] = useThrowError ( ) ;
182182
183183 // Margin looks wrong with ui.table, so we want to map margin to padding instead
@@ -286,40 +286,32 @@ export function UITable({
286286 model . setColorMap ( colorMap ) ;
287287 }
288288
289- const prevState = useRef < IrisGridState | undefined > ( ) ;
290289 const [ dehydratedState , setDehydratedState ] = usePersistentState <
291- DehydratedIrisGridState | undefined
292- > ( undefined ) ;
290+ ( DehydratedIrisGridState & DehydratedGridState ) | undefined
291+ > ( undefined , { type : 'UITable' , version : 1 } ) ;
293292 const initialState = useRef ( dehydratedState ) ;
294293
294+ const memoizedStateFn = useMemo (
295+ ( ) => IrisGridCacheUtils . makeMemoizedCombinedGridStateDehydrator ( ) ,
296+ [ ]
297+ ) ;
298+
295299 const onStateChange = useCallback (
296- ( newState : IrisGridState ) => {
297- if (
298- model == null ||
299- newState . metrics == null ||
300- newState === prevState . current ||
301- newState . sorts === prevState . current ?. sorts
302- ) {
300+ ( irisGridState : IrisGridState , gridState : GridState ) => {
301+ if ( model == null ) {
303302 return ;
304303 }
305- // console.log('onStateChange', newState);
306- prevState . current = newState ;
307- setDehydratedState (
308- utils . dehydrateIrisGridState ( model , {
309- ...newState ,
310- metrics : {
311- userColumnWidths : newState . metrics . userColumnWidths ,
312- userRowHeights : newState . metrics . userRowHeights ,
313- } ,
314- } )
315- ) ;
304+ setDehydratedState ( memoizedStateFn ( model , irisGridState , gridState ) ) ;
316305 } ,
317- [ model , setDehydratedState , utils ]
306+ [ memoizedStateFn , model , setDehydratedState ]
318307 ) ;
319308
320- const hydratedState = useMemo ( ( ) => {
321- if ( model && initialState . current ) {
322- return utils . hydrateIrisGridState ( model , initialState . current ) ;
309+ const initialHydratedState = useMemo ( ( ) => {
310+ if ( model && initialState . current != null ) {
311+ return {
312+ ...utils . hydrateIrisGridState ( model , initialState . current ) ,
313+ ...IrisGridUtils . hydrateGridState ( model , initialState . current ) ,
314+ } ;
323315 }
324316 } , [ model , utils ] ) ;
325317
@@ -521,7 +513,7 @@ export function UITable({
521513 model = { model }
522514 onStateChange = { onStateChange }
523515 // eslint-disable-next-line react/jsx-props-no-spreading
524- { ...hydratedState }
516+ { ...initialHydratedState }
525517 // eslint-disable-next-line react/jsx-props-no-spreading
526518 { ...irisGridProps }
527519 />
0 commit comments