From ac188c1fe1d53f7d8716742d43eee44ab1000e22 Mon Sep 17 00:00:00 2001 From: Joey Date: Mon, 1 Dec 2025 11:04:42 +0000 Subject: [PATCH] Fix for persistence on page reload --- .../explore-map/hooks/useCanvasPersistence.ts | 6 ++++++ .../features/explore-map/state/exploreMapSlice.ts | 13 ++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/public/app/features/explore-map/hooks/useCanvasPersistence.ts b/public/app/features/explore-map/hooks/useCanvasPersistence.ts index 1a3ccded4da..42e855527c2 100644 --- a/public/app/features/explore-map/hooks/useCanvasPersistence.ts +++ b/public/app/features/explore-map/hooks/useCanvasPersistence.ts @@ -30,6 +30,12 @@ export function useCanvasPersistence() { // Save state to storage whenever it changes, including Explore state useEffect(() => { + // Don't persist an empty canvas; this avoids removing a previously saved + // non-empty canvas when the in-memory state is still at its initial value. + if (!exploreMapState || Object.keys(exploreMapState.panels || {}).length === 0) { + return; + } + try { // Enrich exploreMapState with current Explore state for each panel const enrichedState: ExploreMapState = { diff --git a/public/app/features/explore-map/state/exploreMapSlice.ts b/public/app/features/explore-map/state/exploreMapSlice.ts index a2e5b2c9c21..b1a97313170 100644 --- a/public/app/features/explore-map/state/exploreMapSlice.ts +++ b/public/app/features/explore-map/state/exploreMapSlice.ts @@ -201,9 +201,16 @@ const exploreMapSlice = createSlice({ loadCanvas: (state, action: PayloadAction) => { const loadedState = action.payload; - // Clear any selection state from loaded data - loadedState.selectedPanelIds = []; - return loadedState; + + // Merge with initial state to ensure we always have required defaults + // and to avoid persisting ephemeral state like selection and cursors. + return { + ...initialExploreMapState, + ...loadedState, + panels: loadedState.panels || {}, + selectedPanelIds: [], + cursors: {}, + }; }, updateCursor: (state, action: PayloadAction) => {