@@ -23,7 +23,7 @@ import { fetchAllEquipments } from 'services/study/network-map';
23
23
import type { NodeAlias } from '../../../types/node-alias.type' ;
24
24
import { isStatusBuilt } from '../../../../graph/util/model-functions' ;
25
25
import { useFetchEquipment } from '../../../hooks/use-fetch-equipment' ;
26
- import { isStudyNotification } from 'types/notification-types' ;
26
+ import { type DeletedEquipment , isStudyNotification , type NetworkImpactsInfos } from 'types/notification-types' ;
27
27
import { NodeType } from '../../../../graph/tree-node.type' ;
28
28
import { validAlias } from '../../../hooks/use-node-aliases' ;
29
29
import { fetchNetworkElementInfos } from 'services/study/network' ;
@@ -83,13 +83,13 @@ export const useSpreadsheetEquipments = (
83
83
} , [ nodeAliases , treeNodes ] ) ;
84
84
85
85
const nodesIdToFetch = useMemo ( ( ) => {
86
- let nodesIdToFetch = new Set < string > ( ) ;
87
- if ( ! equipments || ! builtAliasedNodesIds ) {
86
+ const nodesIdToFetch = new Set < UUID > ( ) ;
87
+ if ( ! equipments . nodesId || ! builtAliasedNodesIds || ! currentNode ?. id ) {
88
88
return nodesIdToFetch ;
89
89
}
90
- // We check if we have the data for the currentNode and if we don't we save the fact that we need to fetch it
91
- if ( equipments . nodesId . find ( ( nodeId ) => nodeId === currentNode ? .id ) === undefined ) {
92
- nodesIdToFetch . add ( currentNode ? .id as string ) ;
90
+ // We check if we have the data for the currentNode and if we don't, we save the fact that we need to fetch it
91
+ if ( equipments . nodesId . find ( ( nodeId ) => nodeId === currentNode . id ) === undefined ) {
92
+ nodesIdToFetch . add ( currentNode . id ) ;
93
93
}
94
94
// Then we do the same for the other nodes we need the data of (the ones defined in aliases)
95
95
builtAliasedNodesIds . forEach ( ( builtAliasNodeId ) => {
@@ -98,30 +98,25 @@ export const useSpreadsheetEquipments = (
98
98
}
99
99
} ) ;
100
100
return nodesIdToFetch ;
101
- } , [ currentNode ?. id , equipments , builtAliasedNodesIds ] ) ;
101
+ } , [ currentNode ?. id , equipments . nodesId , builtAliasedNodesIds ] ) ;
102
102
103
103
// effect to unload equipment data when we remove an alias or unbuild an aliased node
104
104
useEffect ( ( ) => {
105
- if ( ! equipments || ! builtAliasedNodesIds ) {
105
+ if ( ! equipments || ! builtAliasedNodesIds || ! currentNode ?. id ) {
106
106
return ;
107
107
}
108
- const currentNodeId = currentNode ?. id as UUID ;
109
- let unwantedFetchedNodes = new Set < string > ( ) ;
110
- unwantedFetchedNodes = new Set ( [ ...unwantedFetchedNodes , ...equipments . nodesId ] ) ;
108
+ const currentNodeId = currentNode . id ;
109
+ const unwantedFetchedNodes = new Set ( equipments . nodesId ) ;
111
110
const usedNodesId = new Set ( builtAliasedNodesIds ) ;
112
111
usedNodesId . add ( currentNodeId ) ;
113
112
usedNodesId . forEach ( ( nodeId ) => unwantedFetchedNodes . delete ( nodeId ) ) ;
114
113
if ( unwantedFetchedNodes . size !== 0 ) {
115
114
dispatch ( removeNodeData ( Array . from ( unwantedFetchedNodes ) ) ) ;
116
115
}
117
- } , [ builtAliasedNodesIds , currentNode , dispatch , equipments ] ) ;
116
+ } , [ builtAliasedNodesIds , currentNode ?. id , dispatch , equipments ] ) ;
118
117
119
- const updateEquipmentsLocal = useCallback (
120
- (
121
- impactedSubstationsIds : string [ ] ,
122
- deletedEquipments : { equipmentType : string ; equipmentId : string } [ ] ,
123
- impactedElementTypes : string [ ]
124
- ) => {
118
+ const deleteEquipmentsLocal = useCallback (
119
+ ( impactedSubstationsIds : UUID [ ] , deletedEquipments : DeletedEquipment [ ] , impactedElementTypes : string [ ] ) => {
125
120
if ( ! type ) {
126
121
return ;
127
122
}
@@ -221,25 +216,18 @@ export const useSpreadsheetEquipments = (
221
216
currentNode ?. id === eventNodeUuid &&
222
217
currentRootNetworkUuid === eventRootNetworkUuid
223
218
) {
224
- // @ts -ignore
225
219
const payload = JSON . parse ( eventData . payload ) as NetworkImpactsInfos ;
226
220
const impactedSubstationsIds = payload . impactedSubstationsIds ;
227
221
const deletedEquipments = payload . deletedEquipments ;
228
222
const impactedElementTypes = payload . impactedElementTypes ?? [ ] ;
229
- updateEquipmentsLocal ( impactedSubstationsIds , deletedEquipments , impactedElementTypes ) ;
223
+ deleteEquipmentsLocal ( impactedSubstationsIds , deletedEquipments , impactedElementTypes ) ;
230
224
}
231
225
}
232
226
} ,
233
- [ currentNode ?. id , currentRootNetworkUuid , studyUuid , updateEquipmentsLocal ]
227
+ [ currentNode ?. id , currentRootNetworkUuid , studyUuid , deleteEquipmentsLocal ]
234
228
) ;
235
229
236
- useNotificationsListener ( NotificationsUrlKeys . STUDY , {
237
- listenerCallbackMessage : listenerUpdateEquipmentsLocal ,
238
- } ) ;
239
-
240
- const onFetchingDone = ( ) => {
241
- setIsFetching ( false ) ;
242
- } ;
230
+ useNotificationsListener ( NotificationsUrlKeys . STUDY , { listenerCallbackMessage : listenerUpdateEquipmentsLocal } ) ;
243
231
244
232
// Note: take care about the dependencies because any execution here implies equipment loading (large fetches).
245
233
// For example, we have 3 currentNode properties in deps rather than currentNode object itself.
@@ -253,7 +241,7 @@ export const useSpreadsheetEquipments = (
253
241
( currentNode ?. type === NodeType . ROOT || isStatusBuilt ( currentNode ?. data . globalBuildStatus ) )
254
242
) {
255
243
setIsFetching ( true ) ;
256
- fetchNodesEquipmentData ( nodesIdToFetch , currentNode . id , currentRootNetworkUuid , onFetchingDone ) ;
244
+ fetchNodesEquipmentData ( nodesIdToFetch , currentNode . id , currentRootNetworkUuid , ( ) => setIsFetching ( false ) ) ;
257
245
}
258
246
} , [
259
247
active ,
0 commit comments