Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/components/spreadsheet-view/hooks/use-built-nodes-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { useSelector } from 'react-redux';
import type { AppState } from '../../../redux/reducer';
import { useStableComputedSet } from '../../../hooks/use-stable-computed-set';
import type { UUID } from 'crypto';
import { validAlias } from './use-node-aliases';
import { NodeType } from '../../graph/tree-node.type';
import { isStatusBuilt } from '../../graph/util/model-functions';
import type { NodeAlias } from '../types/node-alias.type';

export function useBuiltNodesIds(nodeAliases: NodeAlias[] | undefined) {
const currentNode = useSelector((state: AppState) => state.currentTreeNode);
const treeNodes = useSelector((state: AppState) => state.networkModificationTreeModel?.treeNodes);

return useStableComputedSet(() => {
const aliasedNodesIds = nodeAliases
?.filter((nodeAlias) => validAlias(nodeAlias))
.map((nodeAlias) => nodeAlias.id);
if (currentNode?.id) {
aliasedNodesIds?.push(currentNode.id);
}

const ids = new Set<UUID>();
if (aliasedNodesIds && aliasedNodesIds.length > 0) {
treeNodes?.forEach((treeNode) => {
if (
aliasedNodesIds.includes(treeNode.id) &&
(treeNode.type === NodeType.ROOT || isStatusBuilt(treeNode.data.globalBuildStatus))
) {
ids.add(treeNode.id);
}
});
}
return ids;
}, [nodeAliases, treeNodes]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { useCallback } from 'react';
import type { UUID } from 'crypto';
import { DeletedEquipment, isStudyNotification, type NetworkImpactsInfos } from '../../../types/notification-types';
import { isSpreadsheetEquipmentType, SpreadsheetEquipmentType } from '../types/spreadsheet.type';
import {
deleteEquipments,
type EquipmentToDelete,
resetEquipments,
resetEquipmentsByTypes,
updateEquipments,
} from '../../../redux/actions';
import { fetchAllEquipments } from '../../../services/study/network-map';
import { useDispatch, useSelector } from 'react-redux';
import { AppState } from 'redux/reducer';
import { NotificationsUrlKeys, useNotificationsListener } from '@gridsuite/commons-ui';
import { NodeAlias } from '../types/node-alias.type';
import { useBuiltNodesIds } from './use-built-nodes-ids';

const SPREADSHEET_EQUIPMENTS_LISTENER_ID = 'spreadsheet-equipments-listener';

export function useUpdateEquipmentsOnNotification(nodeAliases: NodeAlias[] | undefined) {
const dispatch = useDispatch();
const allEquipments = useSelector((state: AppState) => state.spreadsheetNetwork);
const studyUuid = useSelector((state: AppState) => state.studyUuid);
const currentRootNetworkUuid = useSelector((state: AppState) => state.currentRootNetworkUuid);

const builtNodesIds = useBuiltNodesIds(nodeAliases);

const updateEquipmentsLocal = useCallback(
(
nodeUuid: UUID,
impactedSubstationsIds: UUID[],
deletedEquipments: DeletedEquipment[],
impactedElementTypes: string[]
) => {
// Handle updates and resets based on impacted element types
if (impactedElementTypes.length > 0) {
if (impactedElementTypes.includes(SpreadsheetEquipmentType.SUBSTATION)) {
dispatch(resetEquipments());
return;
}
const impactedSpreadsheetEquipmentsTypes = impactedElementTypes.filter((type) =>
Object.keys(allEquipments).includes(type)
);
if (impactedSpreadsheetEquipmentsTypes.length > 0) {
dispatch(
resetEquipmentsByTypes(impactedSpreadsheetEquipmentsTypes.filter(isSpreadsheetEquipmentType))
);
}
return;
}

if (impactedSubstationsIds.length > 0 && studyUuid && currentRootNetworkUuid) {
fetchAllEquipments(studyUuid, nodeUuid, currentRootNetworkUuid, impactedSubstationsIds).then(
(values) => {
dispatch(updateEquipments(values, nodeUuid));
}
);
}

if (deletedEquipments.length > 0) {
const equipmentsToDelete = deletedEquipments
.filter(({ equipmentType, equipmentId }) => equipmentType && equipmentId)
.map(({ equipmentType, equipmentId }) => {
console.info(
'removing equipment with id=',
equipmentId,
' and type=',
equipmentType,
' from the network'
);
return { equipmentType, equipmentId };
});

if (equipmentsToDelete.length > 0) {
const equipmentsToDeleteArray = equipmentsToDelete
.filter((e) => isSpreadsheetEquipmentType(e.equipmentType))
.map<EquipmentToDelete>((equipment) => ({
equipmentType: equipment.equipmentType as unknown as SpreadsheetEquipmentType,
equipmentId: equipment.equipmentId,
}));
dispatch(deleteEquipments(equipmentsToDeleteArray, nodeUuid));
}
}
},
[studyUuid, currentRootNetworkUuid, dispatch, allEquipments]
);

const listenerUpdateEquipmentsLocal = useCallback(
(event: MessageEvent) => {
const eventData = JSON.parse(event.data);
if (isStudyNotification(eventData)) {
const eventStudyUuid = eventData.headers.studyUuid;
const eventNodeUuid = eventData.headers.node;
const eventRootNetworkUuid = eventData.headers.rootNetworkUuid;
if (
studyUuid === eventStudyUuid &&
currentRootNetworkUuid === eventRootNetworkUuid &&
builtNodesIds.has(eventNodeUuid)
) {
const networkImpacts = JSON.parse(eventData.payload) as NetworkImpactsInfos;
updateEquipmentsLocal(
eventNodeUuid,
networkImpacts.impactedSubstationsIds,
networkImpacts.deletedEquipments,
networkImpacts.impactedElementTypes ?? []
);
}
}
},
[builtNodesIds, currentRootNetworkUuid, studyUuid, updateEquipmentsLocal]
);

useNotificationsListener(NotificationsUrlKeys.STUDY, {
listenerCallbackMessage: listenerUpdateEquipmentsLocal,
propsId: SPREADSHEET_EQUIPMENTS_LISTENER_ID,
});
}
4 changes: 4 additions & 0 deletions src/components/spreadsheet-view/spreadsheet-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { initTableDefinitions, setActiveSpreadsheetTab } from 'redux/actions';
import { type MuiStyles, PopupConfirmationDialog, useSnackMessage } from '@gridsuite/commons-ui';
import { processSpreadsheetsCollectionData } from './add-spreadsheet/dialogs/add-spreadsheet-utils';
import { DiagramType } from 'components/grid-layout/cards/diagrams/diagram.type';
import { useUpdateEquipmentsOnNotification } from './hooks/use-update-equipments-on-notification';

const styles = {
invalidNode: {
Expand Down Expand Up @@ -60,6 +61,8 @@ export const SpreadsheetView: FunctionComponent<SpreadsheetViewProps> = ({
const studyUuid = useSelector((state: AppState) => state.studyUuid);
const [resetConfirmationDialogOpen, setResetConfirmationDialogOpen] = useState(false);

useUpdateEquipmentsOnNotification(nodeAliases);

const handleSwitchTab = useCallback(
(tabUuid: UUID) => {
dispatch(setActiveSpreadsheetTab(tabUuid));
Expand Down Expand Up @@ -137,6 +140,7 @@ export const SpreadsheetView: FunctionComponent<SpreadsheetViewProps> = ({
<FormattedMessage id={'NoSpreadsheets'} />
</Alert>
) : (
nodeAliases &&
tablesDefinitions.map((tabDef) => {
const isActive = activeSpreadsheetTabUuid === tabDef.uuid;
const equipmentIdToScrollTo = tabDef.type === equipmentType && isActive ? equipmentId : null;
Expand Down
Loading
Loading