Skip to content

Commit c1cb6b6

Browse files
fix
1 parent 934c798 commit c1cb6b6

File tree

7 files changed

+55
-64
lines changed

7 files changed

+55
-64
lines changed

src/components/spreadsheet-view/hooks/use-fetch-equipment.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,11 @@ export const useFetchEquipment = (type: SpreadsheetEquipmentType) => {
8181
const { snackError } = useSnackMessage();
8282
const studyUuid = useSelector((state: AppState) => state.studyUuid);
8383

84-
const mapEquipments = useCallback(
85-
(fetchedEquipments: any) => {
86-
//Format the equipments data to set calculated fields, so that the edition validation is consistent with the displayed data
87-
return mapSpreadsheetEquipments(type, fetchedEquipments);
88-
},
89-
[type]
90-
);
91-
9284
const fetchNodesEquipmentData = useCallback(
93-
(nodeIds: Set<string>, currentNodeUuid: UUID, currentRootNetworkUuid: UUID, onFetchingDone?: () => void) => {
85+
(nodeIds: Set<UUID>, currentNodeUuid: UUID, currentRootNetworkUuid: UUID, onFetchingDone?: () => void) => {
9486
if (studyUuid && currentNodeUuid && currentRootNetworkUuid) {
95-
let fetcherPromises: Promise<unknown>[] = [];
96-
let spreadsheetEquipmentsByNodes: SpreadsheetEquipmentsByNodes = {
87+
const fetcherPromises: ReturnType<typeof fetchNetworkElementsInfos>[] = [];
88+
const spreadsheetEquipmentsByNodes: SpreadsheetEquipmentsByNodes = {
9789
nodesId: [],
9890
equipmentsByNodeId: {},
9991
};
@@ -103,10 +95,12 @@ export const useFetchEquipment = (type: SpreadsheetEquipmentType) => {
10395
fetcherPromises.push(promise);
10496
promise
10597
.then((results) => {
106-
let fetchedEquipments = results.flat();
10798
spreadsheetEquipmentsByNodes.nodesId.push(nodeId);
108-
fetchedEquipments = mapEquipments(fetchedEquipments);
109-
spreadsheetEquipmentsByNodes.equipmentsByNodeId[nodeId] = fetchedEquipments;
99+
// Format the equipments data to set calculated fields so that the edition validation is consistent with the displayed data
100+
spreadsheetEquipmentsByNodes.equipmentsByNodeId[nodeId] = mapSpreadsheetEquipments(
101+
type,
102+
results
103+
);
110104
})
111105
.catch((err) => {
112106
console.error(

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fetchAllEquipments } from 'services/study/network-map';
2323
import type { NodeAlias } from '../../../types/node-alias.type';
2424
import { isStatusBuilt } from '../../../../graph/util/model-functions';
2525
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';
2727
import { NodeType } from '../../../../graph/tree-node.type';
2828
import { validAlias } from '../../../hooks/use-node-aliases';
2929
import { fetchNetworkElementInfos } from 'services/study/network';
@@ -83,13 +83,13 @@ export const useSpreadsheetEquipments = (
8383
}, [nodeAliases, treeNodes]);
8484

8585
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) {
8888
return nodesIdToFetch;
8989
}
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);
9393
}
9494
// Then we do the same for the other nodes we need the data of (the ones defined in aliases)
9595
builtAliasedNodesIds.forEach((builtAliasNodeId) => {
@@ -98,30 +98,25 @@ export const useSpreadsheetEquipments = (
9898
}
9999
});
100100
return nodesIdToFetch;
101-
}, [currentNode?.id, equipments, builtAliasedNodesIds]);
101+
}, [currentNode?.id, equipments.nodesId, builtAliasedNodesIds]);
102102

103103
// effect to unload equipment data when we remove an alias or unbuild an aliased node
104104
useEffect(() => {
105-
if (!equipments || !builtAliasedNodesIds) {
105+
if (!equipments || !builtAliasedNodesIds || !currentNode?.id) {
106106
return;
107107
}
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);
111110
const usedNodesId = new Set(builtAliasedNodesIds);
112111
usedNodesId.add(currentNodeId);
113112
usedNodesId.forEach((nodeId) => unwantedFetchedNodes.delete(nodeId));
114113
if (unwantedFetchedNodes.size !== 0) {
115114
dispatch(removeNodeData(Array.from(unwantedFetchedNodes)));
116115
}
117-
}, [builtAliasedNodesIds, currentNode, dispatch, equipments]);
116+
}, [builtAliasedNodesIds, currentNode?.id, dispatch, equipments]);
118117

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[]) => {
125120
if (!type) {
126121
return;
127122
}
@@ -221,25 +216,18 @@ export const useSpreadsheetEquipments = (
221216
currentNode?.id === eventNodeUuid &&
222217
currentRootNetworkUuid === eventRootNetworkUuid
223218
) {
224-
// @ts-ignore
225219
const payload = JSON.parse(eventData.payload) as NetworkImpactsInfos;
226220
const impactedSubstationsIds = payload.impactedSubstationsIds;
227221
const deletedEquipments = payload.deletedEquipments;
228222
const impactedElementTypes = payload.impactedElementTypes ?? [];
229-
updateEquipmentsLocal(impactedSubstationsIds, deletedEquipments, impactedElementTypes);
223+
deleteEquipmentsLocal(impactedSubstationsIds, deletedEquipments, impactedElementTypes);
230224
}
231225
}
232226
},
233-
[currentNode?.id, currentRootNetworkUuid, studyUuid, updateEquipmentsLocal]
227+
[currentNode?.id, currentRootNetworkUuid, studyUuid, deleteEquipmentsLocal]
234228
);
235229

236-
useNotificationsListener(NotificationsUrlKeys.STUDY, {
237-
listenerCallbackMessage: listenerUpdateEquipmentsLocal,
238-
});
239-
240-
const onFetchingDone = () => {
241-
setIsFetching(false);
242-
};
230+
useNotificationsListener(NotificationsUrlKeys.STUDY, { listenerCallbackMessage: listenerUpdateEquipmentsLocal });
243231

244232
// Note: take care about the dependencies because any execution here implies equipment loading (large fetches).
245233
// For example, we have 3 currentNode properties in deps rather than currentNode object itself.
@@ -253,7 +241,7 @@ export const useSpreadsheetEquipments = (
253241
(currentNode?.type === NodeType.ROOT || isStatusBuilt(currentNode?.data.globalBuildStatus))
254242
) {
255243
setIsFetching(true);
256-
fetchNodesEquipmentData(nodesIdToFetch, currentNode.id, currentRootNetworkUuid, onFetchingDone);
244+
fetchNodesEquipmentData(nodesIdToFetch, currentNode.id, currentRootNetworkUuid, () => setIsFetching(false));
257245
}
258246
}, [
259247
active,

src/components/spreadsheet-view/spreadsheet/spreadsheet-toolbar/nodes-config/nodes-config-button.tsx

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

8+
import type { UUID } from 'crypto';
89
import { FormattedMessage } from 'react-intl';
9-
import { Badge, Button, Theme, Tooltip } from '@mui/material';
10+
import { Badge, Button, Menu, MenuItem, type Theme, Tooltip } from '@mui/material';
1011
import { useStateBoolean } from '@gridsuite/commons-ui';
1112
import BuildIcon from '@mui/icons-material/Build';
1213
import { ROOT_NODE_LABEL } from '../../../../../constants/node.constant';
13-
import { NodeAlias } from '../../../types/node-alias.type';
14-
import { MouseEvent, useCallback, useMemo, useState } from 'react';
14+
import type { NodeAlias } from '../../../types/node-alias.type';
15+
import { type MouseEvent, useCallback, useMemo, useState } from 'react';
1516
import { useSelector } from 'react-redux';
16-
import { AppState } from '../../../../../redux/reducer';
17-
import MenuItem from '@mui/material/MenuItem';
18-
import Menu from '@mui/material/Menu';
17+
import { type AppState } from '../../../../../redux/reducer';
1918
import { useFetchEquipment } from '../../../hooks/use-fetch-equipment';
2019
import { validAlias } from '../../../hooks/use-node-aliases';
2120
import { NodeType } from '../../../../graph/tree-node.type';
2221
import { isStatusBuilt } from '../../../../graph/util/model-functions';
23-
import { SpreadsheetEquipmentType } from '../../../types/spreadsheet.type';
22+
import { type SpreadsheetEquipmentType } from '../../../types/spreadsheet.type';
2423
import NodesConfigDialog from './nodes-config-dialog';
2524
import { spreadsheetStyles } from '../../../spreadsheet.style';
25+
import { notUndefined } from '../../../../utils/ts-utils';
2626

2727
const styles = {
2828
icon: {
@@ -100,7 +100,7 @@ export default function NodesConfigButton({
100100

101101
const handleRefresh = useCallback(() => {
102102
if (currentNode?.id && currentRootNetworkUuid && nodesToReload?.length) {
103-
const nodesIdsToReload = new Set<string>(nodesToReload.map((n) => n.id as string));
103+
const nodesIdsToReload = new Set(nodesToReload.map((n) => n.id).filter(notUndefined));
104104
fetchNodesEquipmentData(nodesIdsToReload, currentNode.id, currentRootNetworkUuid);
105105
}
106106
}, [currentNode?.id, currentRootNetworkUuid, fetchNodesEquipmentData, nodesToReload]);

src/components/spreadsheet-view/types/spreadsheet.type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ export type ColumnStateDto = {
7373
};
7474

7575
export type SpreadsheetEquipmentsByNodes = {
76-
nodesId: string[];
77-
equipmentsByNodeId: Record<string, Identifiable[]>;
76+
nodesId: UUID[];
77+
equipmentsByNodeId: Record<UUID, Identifiable[]>;
7878
};
7979

8080
export type SpreadsheetConfig = {

src/components/utils/ts-utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ export type Nullable<T> = { [K in keyof T]: T[K] | null };
88
export type DeepNullable<T> = {
99
[K in keyof T]: DeepNullable<T[K]> | null;
1010
};
11+
12+
export function notUndefined<T>(value: T | undefined): value is T {
13+
return value !== undefined;
14+
}
15+
16+
export function notNull<T>(value: T | null): value is T {
17+
return value !== null;
18+
}

src/hooks/use-get-study-impacts.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ export const useGetStudyImpacts = (): StudyImpactsWithReset => {
5050
impactedSubstationsIds: substationsIds,
5151
deletedEquipments,
5252
impactedElementTypes,
53-
} = JSON.parse(
54-
// @ts-ignore
55-
studyUpdatedForce.eventData.payload
56-
) as NetworkImpactsInfos;
53+
} = JSON.parse(studyUpdatedForce.eventData.payload) as NetworkImpactsInfos;
5754

5855
if (impactedElementTypes?.length > 0) {
5956
setImpactedElementTypes(impactedElementTypes);

src/types/notification-types.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
7-
import { EQUIPMENT_TYPES as NetworkViewerEquipmentType } from '@powsybl/network-viewer';
8-
import { ComputingType } from '@gridsuite/commons-ui';
9-
import { UUID } from 'crypto';
7+
import type { EQUIPMENT_TYPES as NetworkViewerEquipmentType } from '@powsybl/network-viewer';
8+
import type { ComputingType } from '@gridsuite/commons-ui';
9+
import type { UUID } from 'crypto';
1010

1111
export enum NotificationType {
1212
// Study status
@@ -539,7 +539,8 @@ interface CommonStudyEventData {
539539

540540
export interface StudyEventData {
541541
headers: StudyEventDataHeaders;
542-
payload: NetworkImpactsInfos;
542+
/** @see NetworkImpactsInfos */
543+
payload: string;
543544
}
544545

545546
export interface ComputationParametersUpdatedEventData {
@@ -584,7 +585,8 @@ export interface MetadataUpdatedEventData {
584585

585586
export interface StudyAlertEventData {
586587
headers: StudyAlertEventDataHeaders;
587-
payload: StudyAlert;
588+
/** @see StudyAlert */
589+
payload: string;
588590
}
589591

590592
export interface NodeCreatedEventData {
@@ -639,7 +641,8 @@ export interface SubtreeCreatedEventData {
639641

640642
export interface NodesColumnPositionsChangedEventData {
641643
headers: NodesColumnPositionsChangedEventDataHeaders;
642-
payload: UUID[];
644+
/** JSON of <code>{@link UUID}[]</code> */
645+
payload: string;
643646
}
644647

645648
export interface ModificationsCreationInProgressEventData extends CommonStudyEventData {
@@ -1132,6 +1135,7 @@ export interface StudyUpdatedEventDataHeader {
11321135
*/
11331136
export interface StudyUpdatedEventData {
11341137
headers: StudyUpdatedEventDataHeader;
1135-
payload: NetworkImpactsInfos;
1138+
/** @see NetworkImpactsInfos */
1139+
payload: string;
11361140
}
11371141
/******************* TO REMOVE LATER ****************/

0 commit comments

Comments
 (0)