Skip to content

Commit 9bf6947

Browse files
refactor: some renaming and fixes
Signed-off-by: Joris Mancini <[email protected]>
1 parent b555cad commit 9bf6947

File tree

5 files changed

+28
-67
lines changed

5 files changed

+28
-67
lines changed

src/components/spreadsheet-view/hooks/use-spreadsheet-nodes.ts renamed to src/components/spreadsheet-view/hooks/use-built-nodes-ids.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ import { NodeType } from '../../graph/tree-node.type';
1414
import { isStatusBuilt } from '../../graph/util/model-functions';
1515
import type { NodeAlias } from '../types/node-alias.type';
1616

17-
export function useSpreadsheetNodes(nodeAliases: NodeAlias[] | undefined) {
17+
export function useBuiltNodesIds(nodeAliases: NodeAlias[] | undefined) {
1818
const currentNode = useSelector((state: AppState) => state.currentTreeNode);
1919
const treeNodes = useSelector((state: AppState) => state.networkModificationTreeModel?.treeNodes);
2020

21-
const builtNodesIds = useStableComputedSet(() => {
22-
const ids: Set<UUID> = new Set<UUID>();
23-
if (currentNode?.id) {
24-
ids.add(currentNode.id);
25-
}
21+
return useStableComputedSet(() => {
2622
const aliasedNodesIds = nodeAliases
2723
?.filter((nodeAlias) => validAlias(nodeAlias))
2824
.map((nodeAlias) => nodeAlias.id);
25+
if (currentNode?.id) {
26+
aliasedNodesIds?.push(currentNode.id);
27+
}
28+
29+
const ids = new Set<UUID>();
2930
if (aliasedNodesIds && aliasedNodesIds.length > 0) {
3031
treeNodes?.forEach((treeNode) => {
3132
if (
@@ -38,6 +39,4 @@ export function useSpreadsheetNodes(nodeAliases: NodeAlias[] | undefined) {
3839
}
3940
return ids;
4041
}, [nodeAliases, treeNodes]);
41-
42-
return { builtNodesIds };
4342
}

src/components/spreadsheet-view/hooks/use-update-equipments-on-notification.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { useDispatch, useSelector } from 'react-redux';
2121
import { AppState } from 'redux/reducer';
2222
import { NotificationsUrlKeys, useNotificationsListener } from '@gridsuite/commons-ui';
2323
import { NodeAlias } from '../types/node-alias.type';
24-
import { useSpreadsheetNodes } from './use-spreadsheet-nodes';
24+
import { useBuiltNodesIds } from './use-built-nodes-ids';
2525

2626
const SPREADSHEET_EQUIPMENTS_LISTENER_ID = 'spreadsheet-equipments-listener';
2727

@@ -31,7 +31,7 @@ export function useUpdateEquipmentsOnNotification(nodeAliases: NodeAlias[] | und
3131
const studyUuid = useSelector((state: AppState) => state.studyUuid);
3232
const currentRootNetworkUuid = useSelector((state: AppState) => state.currentRootNetworkUuid);
3333

34-
const { builtNodesIds } = useSpreadsheetNodes(nodeAliases);
34+
const builtNodesIds = useBuiltNodesIds(nodeAliases);
3535

3636
const updateEquipmentsLocal = useCallback(
3737
(
@@ -54,6 +54,7 @@ export function useUpdateEquipmentsOnNotification(nodeAliases: NodeAlias[] | und
5454
resetEquipmentsByTypes(impactedSpreadsheetEquipmentsTypes.filter(isSpreadsheetEquipmentType))
5555
);
5656
}
57+
return;
5758
}
5859

5960
if (impactedSubstationsIds.length > 0 && studyUuid && currentRootNetworkUuid) {
@@ -104,15 +105,12 @@ export function useUpdateEquipmentsOnNotification(nodeAliases: NodeAlias[] | und
104105
currentRootNetworkUuid === eventRootNetworkUuid &&
105106
builtNodesIds.has(eventNodeUuid)
106107
) {
107-
const payload = JSON.parse(eventData.payload) as NetworkImpactsInfos;
108-
const impactedSubstationsIds = payload.impactedSubstationsIds;
109-
const deletedEquipments = payload.deletedEquipments;
110-
const impactedElementTypes = payload.impactedElementTypes ?? [];
108+
const networkImpacts = JSON.parse(eventData.payload) as NetworkImpactsInfos;
111109
updateEquipmentsLocal(
112110
eventNodeUuid,
113-
impactedSubstationsIds,
114-
deletedEquipments,
115-
impactedElementTypes
111+
networkImpacts.impactedSubstationsIds,
112+
networkImpacts.deletedEquipments,
113+
networkImpacts.impactedElementTypes ?? []
116114
);
117115
}
118116
}

src/components/spreadsheet-view/spreadsheet/spreadsheet-content/hooks/use-spreadsheet-equipments.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { NodeAlias } from '../../../types/node-alias.type';
1313
import { useOptionalLoadingParametersForEquipments } from './use-optional-loading-parameters-for-equipments';
1414
import { SpreadsheetEquipmentType } from '../../../types/spreadsheet.type';
1515
import { useFetchEquipment } from 'components/spreadsheet-view/hooks/use-fetch-equipment';
16-
import { useSpreadsheetNodes } from '../../../hooks/use-spreadsheet-nodes';
16+
import { useBuiltNodesIds } from '../../../hooks/use-built-nodes-ids';
1717
import { useStableComputedSet } from '../../../../../hooks/use-stable-computed-set';
1818
import type { UUID } from 'crypto';
1919

@@ -27,7 +27,7 @@ export const useSpreadsheetEquipments = (
2727
const currentNode = useSelector((state: AppState) => state.currentTreeNode);
2828
const treeNodes = useSelector((state: AppState) => state.networkModificationTreeModel?.treeNodes);
2929
const equipments = useSelector((state: AppState) => state.spreadsheetNetwork[type]);
30-
const nodesIds = useSelector((state: AppState) => state.spreadsheetNetwork[type].nodesId);
30+
const loadedNodesIdsForType = useSelector((state: AppState) => state.spreadsheetNetwork[type].nodesId);
3131
const [isFetching, setIsFetching] = useState<boolean>(false);
3232
const { fetchNodesEquipmentData } = useFetchEquipment(type);
3333

@@ -46,36 +46,26 @@ export const useSpreadsheetEquipments = (
4646
// eslint-disable-next-line react-hooks/exhaustive-deps
4747
}, [shouldCleanOptionalLoadingParameters, type]);
4848

49-
const { builtNodesIds } = useSpreadsheetNodes(nodeAliases);
49+
const builtNodesIds = useBuiltNodesIds(nodeAliases);
5050

5151
const nodesIdsToRemove = useStableComputedSet(() => {
52-
if (!currentNode?.id) {
53-
return new Set<UUID>();
54-
}
55-
const currentNodeId = currentNode.id;
56-
const unwantedFetchedNodes = new Set(nodesIds);
57-
const usedNodesId = new Set(builtNodesIds);
58-
usedNodesId.add(currentNodeId);
59-
usedNodesId.forEach((nodeId) => unwantedFetchedNodes.delete(nodeId));
52+
const unwantedFetchedNodes = new Set(loadedNodesIdsForType);
53+
builtNodesIds.forEach((nodeId) => unwantedFetchedNodes.delete(nodeId));
6054
return unwantedFetchedNodes;
61-
}, [currentNode?.id, builtNodesIds]);
55+
}, [builtNodesIds]);
6256

6357
const nodesIdsToFetch = useStableComputedSet(() => {
6458
if (shouldLoadOptionalLoadingParameters) {
65-
return new Set<UUID>(builtNodesIds);
66-
}
67-
const nodesIdToFetch = new Set<UUID>();
68-
for (const builtAliasNodeId of builtNodesIds) {
69-
if (nodesIds.find((nodeId) => nodeId === builtAliasNodeId) === undefined) {
70-
nodesIdToFetch.add(builtAliasNodeId);
71-
}
59+
return builtNodesIds;
7260
}
61+
const nodesIdToFetch = new Set<UUID>(builtNodesIds);
62+
loadedNodesIdsForType.forEach((nodeId) => nodesIdToFetch.delete(nodeId));
7363
return nodesIdToFetch;
74-
}, [currentNode?.id, nodesIds, nodeAliases, treeNodes]);
64+
}, [currentNode?.id, loadedNodesIdsForType, nodeAliases, treeNodes]);
7565

7666
// effect to unload equipment data when we remove an alias or unbuild an aliased node
7767
useEffect(() => {
78-
if (active && nodesIdsToRemove && nodesIdsToRemove.size !== 0) {
68+
if (active && nodesIdsToRemove.size > 0) {
7969
dispatch(removeNodeData(Array.from(nodesIdsToRemove)));
8070
}
8171
}, [active, dispatch, nodesIdsToRemove]);

src/components/study-container.jsx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -486,33 +486,6 @@ export function StudyContainer({ view, onChangeTab }) {
486486
}
487487
}, [studyUpdatedForce, dispatch]);
488488

489-
//handles map automatic mode network reload
490-
useEffect(() => {
491-
let previousCurrentNode = currentNodeRef.current;
492-
currentNodeRef.current = currentNode;
493-
let previousCurrentRootNetworkUuid = currentRootNetworkUuidRef.current;
494-
// this is the last effect to compare currentRootNetworkUuid and currentRootNetworkUuidRef.current
495-
// then we can update the currentRootNetworkUuidRef.current
496-
currentRootNetworkUuidRef.current = currentRootNetworkUuid;
497-
// if only node renaming, do not reload network
498-
if (isNodeEdited(previousCurrentNode, currentNode)) {
499-
return;
500-
}
501-
if (!isNodeBuilt(currentNode)) {
502-
return;
503-
}
504-
// A modification has been added to the currentNode and this one has been built incrementally.
505-
// No need to load the network because reloadImpactedSubstationsEquipments will be executed in the notification useEffect.
506-
if (
507-
previousCurrentRootNetworkUuid === currentRootNetworkUuid &&
508-
isSameNode(previousCurrentNode, currentNode) &&
509-
isNodeBuilt(previousCurrentNode)
510-
) {
511-
return;
512-
}
513-
dispatch(resetEquipments());
514-
}, [currentNode, currentRootNetworkUuid, dispatch]);
515-
516489
useEffect(() => {
517490
if (studyUuid) {
518491
websocketExpectedCloseRef.current = false;

src/hooks/use-stable-computed-set.ts

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

8-
import { useMemo, useRef } from 'react';
8+
import { DependencyList, useMemo, useRef } from 'react';
99

10-
export function useStableComputedSet<T>(compute: () => Iterable<T>, deps: React.DependencyList): Set<T> {
10+
export function useStableComputedSet<T>(compute: () => Iterable<T>, deps: DependencyList): Set<T> {
1111
const prevRef = useRef<Set<T>>(new Set());
1212

1313
return useMemo(() => {
@@ -22,5 +22,6 @@ export function useStableComputedSet<T>(compute: () => Iterable<T>, deps: React.
2222
}
2323

2424
return prevRef.current;
25+
// eslint-disable-next-line react-hooks/exhaustive-deps
2526
}, [deps]);
2627
}

0 commit comments

Comments
 (0)