Skip to content

Commit d2e1da6

Browse files
authored
Fix nodes sync (#3210)
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent 7620dbe commit d2e1da6

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

src/components/study-navigation-sync-toggle.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const StudyNavigationSyncToggle = () => {
2929
localStorage.setItem(STORAGE_KEYS.SYNC_ENABLED, JSON.stringify(newValue));
3030
if (newValue) {
3131
// Save current values when enabling sync for other tabs
32-
localStorage.setItem(STORAGE_KEYS.TREE_NODE, JSON.stringify(currentTreeNode));
32+
localStorage.setItem(STORAGE_KEYS.TREE_NODE_UUID, JSON.stringify(currentTreeNode?.id));
3333
localStorage.setItem(STORAGE_KEYS.ROOT_NETWORK_UUID, JSON.stringify(currentRootNetworkUuid));
3434
}
3535
} catch (err) {

src/constants/study-navigation-sync-constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
export const BASE_KEYS = {
8+
export const BASE_NAVIGATION_KEYS = {
99
SYNC_ENABLED: 'SYNC_ENABLED',
1010
ROOT_NETWORK_UUID: 'ROOT_NETWORK_UUID',
11-
TREE_NODE: 'TREE_NODE',
11+
TREE_NODE_UUID: 'TREE_NODE_UUID',
1212
};

src/hooks/use-study-navigation-sync.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { CurrentTreeNode } from 'components/graph/tree-node.type';
98
import { UUID } from 'crypto';
109
import { useCallback, useEffect, useMemo } from 'react';
1110
import { useDispatch, useSelector } from 'react-redux';
@@ -22,6 +21,7 @@ const useStudyNavigationSync = () => {
2221
const syncEnabled = useSelector((state: AppState) => state.syncEnabled);
2322
const currentRootNetworkUuid = useSelector((state: AppState) => state.currentRootNetworkUuid);
2423
const currentTreeNode = useSelector((state: AppState) => state.currentTreeNode);
24+
const treeModel = useSelector((state: AppState) => state.networkModificationTreeModel);
2525
const dispatch = useDispatch();
2626

2727
const STORAGE_KEYS = useStudyScopedNavigationKeys();
@@ -36,20 +36,23 @@ const useStudyNavigationSync = () => {
3636
);
3737

3838
const updateTreeNode = useCallback(
39-
(treeNode: CurrentTreeNode | null) => {
40-
if (treeNode !== null && JSON.stringify(treeNode) !== JSON.stringify(currentTreeNode)) {
41-
dispatch(setCurrentTreeNode(treeNode));
39+
(uuid: UUID | null) => {
40+
if (uuid !== null && uuid !== currentTreeNode?.id) {
41+
const currentNode = treeModel?.treeNodes.find((node) => node.id === uuid);
42+
if (currentNode) {
43+
dispatch(setCurrentTreeNode({ ...currentNode }));
44+
}
4245
}
4346
},
44-
[dispatch, currentTreeNode]
47+
[dispatch, currentTreeNode, treeModel]
4548
);
4649

4750
const keyActions = useMemo(
4851
() => ({
4952
[STORAGE_KEYS.ROOT_NETWORK_UUID]: updateRootNetworkUuid,
50-
[STORAGE_KEYS.TREE_NODE]: updateTreeNode,
53+
[STORAGE_KEYS.TREE_NODE_UUID]: updateTreeNode,
5154
}),
52-
[STORAGE_KEYS.ROOT_NETWORK_UUID, STORAGE_KEYS.TREE_NODE, updateRootNetworkUuid, updateTreeNode]
55+
[STORAGE_KEYS.ROOT_NETWORK_UUID, STORAGE_KEYS.TREE_NODE_UUID, updateRootNetworkUuid, updateTreeNode]
5356
);
5457

5558
const syncFromLocalStorage = useCallback(() => {

src/hooks/use-study-scoped-navigation-keys.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { BASE_KEYS } from 'constants/study-navigation-sync-constants';
8+
import { BASE_NAVIGATION_KEYS } from 'constants/study-navigation-sync-constants';
99
import { useMemo } from 'react';
1010
import { useSelector } from 'react-redux';
1111
import { AppState } from 'redux/reducer';
@@ -16,9 +16,9 @@ export const useStudyScopedNavigationKeys = () => {
1616

1717
return useMemo(
1818
() => ({
19-
SYNC_ENABLED: `${BASE_KEYS.SYNC_ENABLED}-${studyUuid}`,
20-
ROOT_NETWORK_UUID: `${BASE_KEYS.ROOT_NETWORK_UUID}-${studyUuid}`,
21-
TREE_NODE: `${BASE_KEYS.TREE_NODE}-${studyUuid}`,
19+
SYNC_ENABLED: `${BASE_NAVIGATION_KEYS.SYNC_ENABLED}-${studyUuid}`,
20+
ROOT_NETWORK_UUID: `${BASE_NAVIGATION_KEYS.ROOT_NETWORK_UUID}-${studyUuid}`,
21+
TREE_NODE_UUID: `${BASE_NAVIGATION_KEYS.TREE_NODE_UUID}-${studyUuid}`,
2222
}),
2323
[studyUuid]
2424
);

src/hooks/use-sync-navigation-actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ export const useSyncNavigationActions = () => {
4545
dispatch(setCurrentTreeNode(treeNode));
4646
if (syncEnabled) {
4747
try {
48-
localStorage.setItem(keys.TREE_NODE, JSON.stringify(treeNode));
48+
localStorage.setItem(keys.TREE_NODE_UUID, JSON.stringify(treeNode.id));
4949
} catch (err) {
5050
console.warn('Failed to set current tree node in localStorage:', err);
5151
}
5252
}
5353
},
54-
[dispatch, syncEnabled, keys.TREE_NODE]
54+
[dispatch, syncEnabled, keys.TREE_NODE_UUID]
5555
);
5656

5757
return {

src/redux/reducer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ import { NodeInsertModes, RootNetworkIndexationStatus, type StudyUpdateNotificat
317317
import { mapSpreadsheetEquipments } from '../utils/spreadsheet-equipments-mapper';
318318
import { Layouts } from 'react-grid-layout';
319319
import { type DiagramConfigPosition } from '../services/explore';
320+
import { BASE_NAVIGATION_KEYS } from 'constants/study-navigation-sync-constants';
320321

321322
// Redux state
322323
export type StudyUpdated = {
@@ -1835,6 +1836,16 @@ function synchCurrentTreeNode(state: Draft<AppState>, nextCurrentNodeUuid?: UUID
18351836
// we need to overwrite state.currentTreeNode to consider label change for example.
18361837
if (nextCurrentNode) {
18371838
state.currentTreeNode = { ...nextCurrentNode };
1839+
/**
1840+
* we need to sync the current tree node uuid to localStorage
1841+
* to avoid having deleted node selected in other tabs for example.
1842+
*/
1843+
if (state.syncEnabled) {
1844+
localStorage.setItem(
1845+
`${BASE_NAVIGATION_KEYS.TREE_NODE_UUID}-${state.studyUuid}`,
1846+
JSON.stringify(nextCurrentNode.id)
1847+
);
1848+
}
18381849
}
18391850
}
18401851

src/redux/session-storage/local-storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DARK_THEME, getComputedLanguage, GsLang, GsTheme, LANG_SYSTEM } from '@
99
import { APP_NAME } from '../../utils/config-params';
1010
import { StudyDisplayMode } from 'components/network-modification.type';
1111
import { UUID } from 'crypto';
12-
import { BASE_KEYS } from 'constants/study-navigation-sync-constants';
12+
import { BASE_NAVIGATION_KEYS } from 'constants/study-navigation-sync-constants';
1313

1414
const LOCAL_STORAGE_THEME_KEY = (APP_NAME + '_THEME').toUpperCase();
1515
const LOCAL_STORAGE_LANGUAGE_KEY = (APP_NAME + '_LANGUAGE').toUpperCase();
@@ -55,7 +55,7 @@ export function saveLocalStorageToggleOptions(studyUuid: UUID, toggleOptions: St
5555
}
5656

5757
export function getLocalStorageSyncEnabled(studyUuid: UUID): boolean {
58-
const saved = localStorage.getItem(`${BASE_KEYS.SYNC_ENABLED}-${studyUuid}`);
58+
const saved = localStorage.getItem(`${BASE_NAVIGATION_KEYS.SYNC_ENABLED}-${studyUuid}`);
5959
if (saved) {
6060
try {
6161
return JSON.parse(saved);

0 commit comments

Comments
 (0)