Skip to content
Open
58 changes: 24 additions & 34 deletions plugins/ui/src/js/src/elements/UITable/UITable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,43 +379,31 @@ export function UITable({
[memoizedStateFn, model, setDehydratedState]
);

// Initial sorts are captured once at mount so later re-renders never push
// a new `sorts` reference into IrisGrid (which would call updateSorts and
// clobber the user's interactive sort changes).
const initialSortsRef = useRef(sorts);

// Lock the initial hydrated state to a stable value the first time model+utils
// are available. Recomputing it would change the `sorts` (and other) prop
// identities and cause IrisGrid to overwrite user changes on every re-render.
const lockedInitialHydratedStateRef = useRef<
Partial<IrisGridProps> | undefined
>(undefined);
const initialHydratedStateComputedRef = useRef(false);
const initialHydratedState = useMemo(() => {
if (model && utils && initialState.current != null) {
return {
...utils.hydrateIrisGridState(model, initialState.current),
...IrisGridUtils.hydrateGridState(model, initialState.current),
};
}
}, [model, utils]);

// Hydrate `sorts` once into a stable reference so IrisGrid applies it as
// initial state without re-applying it (and clobbering user changes) on
// later re-renders.
const hydratedSortsRef = useRef<IrisGridProps['sorts'] | undefined>(
undefined
);
if (
!initialHydratedStateComputedRef.current &&
model != null &&
utils != null
hydratedSortsRef.current === undefined &&
utils != null &&
sorts !== undefined &&
columns.length > 0
) {
initialHydratedStateComputedRef.current = true;
const persisted =
initialState.current != null
? {
...utils.hydrateIrisGridState(model, initialState.current),
...IrisGridUtils.hydrateGridState(model, initialState.current),
}
: undefined;
const initialSorts = initialSortsRef.current;
const seededSorts =
persisted == null && initialSorts !== undefined && columns !== undefined
? utils.hydrateSort(columns, initialSorts)
: undefined;
if (persisted != null) {
lockedInitialHydratedStateRef.current = persisted;
} else if (seededSorts !== undefined) {
lockedInitialHydratedStateRef.current = { sorts: seededSorts };
}
log.debug('Hydrating sorts', sorts);
hydratedSortsRef.current = utils.hydrateSort(columns, sorts);
}
const initialHydratedState = lockedInitialHydratedStateRef.current;
const hydratedSorts = hydratedSortsRef.current;

const hydratedQuickFilters = useMemo(() => {
if (
Expand Down Expand Up @@ -561,6 +549,7 @@ export function UITable({
mouseHandlers,
alwaysFetchColumns,
showSearchBar,
sorts: hydratedSorts,
quickFilters: hydratedQuickFilters,
isFilterBarShown: showQuickFilters,
reverse,
Expand Down Expand Up @@ -610,6 +599,7 @@ export function UITable({
alwaysFetchColumns,
showSearchBar,
showQuickFilters,
hydratedSorts,
hydratedQuickFilters,
reverse,
density,
Expand Down
Loading