Skip to content

Commit 85d4e84

Browse files
authored
Refactor diagram card generic (#3369)
* refactor(DiagramCard): move NAD or SLD code into NADContent or SLDContent components to keep DiagramCard as generic as possible * remove unnecessary type cast * refactor(DiagramModel): Add a `disableOnAddCallback` param defaulted to false, to avoid calling onAddDiagram at initialisation. refactor(GridLayoutPanel): Add `focusOnDiagram` in `addLayoutItem` * refactor(): remove `diagramAlreadyExists` which kindof duplicates `findSimilarDiagram` It now more clearly impossible to find a similar NAD. Add comment // no good criteria to get similar NAD for now Signed-off-by: sBouzols <[email protected]>
1 parent e453d99 commit 85d4e84

File tree

6 files changed

+316
-430
lines changed

6 files changed

+316
-430
lines changed

src/components/grid-layout/cards/diagrams/diagram-card.tsx

Lines changed: 12 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
*/
77

88
import { Box } from '@mui/material';
9-
import { forwardRef, MouseEventHandler, Ref, TouchEventHandler, useCallback, useMemo, useState } from 'react';
9+
import { forwardRef, MouseEventHandler, Ref, TouchEventHandler, useCallback, useMemo } from 'react';
1010
import CustomCardHeader from '../custom-card-header';
11-
import { Diagram, DiagramAdditionalMetadata, DiagramParams, DiagramType } from './diagram.type';
11+
import { Diagram, DiagramAdditionalMetadata, DiagramType } from './diagram.type';
1212
import type { UUID } from 'node:crypto';
1313
import AlertCustomMessageNode from 'components/utils/alert-custom-message-node';
1414
import SingleLineDiagramContent from './singleLineDiagram/single-line-diagram-content';
1515
import NetworkAreaDiagramContent from './networkAreaDiagram/network-area-diagram-content';
16-
import { ElementType, EquipmentType, mergeSx } from '@gridsuite/commons-ui';
16+
import { EquipmentType, mergeSx } from '@gridsuite/commons-ui';
1717
import { DiagramMetadata, SLDMetadata } from '@powsybl/network-viewer';
1818
import { useIntl } from 'react-intl';
1919
import { cardStyles } from '../card-styles';
20-
import { v4 } from 'uuid';
20+
import { CreateDiagramFuncType, UpdateDiagramFuncType } from 'components/grid-layout/hooks/use-diagram-model';
2121

2222
interface ReactGridLayoutCustomChildComponentProps {
2323
style?: React.CSSProperties;
@@ -37,9 +37,8 @@ interface DiagramCardProps extends ReactGridLayoutCustomChildComponentProps {
3737
onClose: () => void;
3838
errorMessage?: string;
3939
showInSpreadsheet: (equipment: { equipmentId: string | null; equipmentType: EquipmentType | null }) => void;
40-
createDiagram: (diagram: DiagramParams) => void;
41-
updateDiagram: (diagram: Diagram) => void;
42-
updateDiagramPositions: (diagram: DiagramParams) => void;
40+
createDiagram: CreateDiagramFuncType;
41+
updateDiagram: UpdateDiagramFuncType;
4342
key: string; // Required for React Grid Layout to identify the component
4443
}
4544

@@ -55,155 +54,18 @@ export const DiagramCard = forwardRef((props: DiagramCardProps, ref: Ref<HTMLDiv
5554
showInSpreadsheet,
5655
createDiagram,
5756
updateDiagram,
58-
updateDiagramPositions,
5957
...reactGridLayoutCustomChildComponentProps
6058
} = props;
6159
const { style, children, ...otherProps } = reactGridLayoutCustomChildComponentProps;
6260

63-
const [diagramsInEditMode, setDiagramsInEditMode] = useState<boolean>(false);
6461
const intl = useIntl();
6562

66-
const handleExpandAllVoltageLevels = useCallback(() => {
67-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
68-
updateDiagram({
69-
...diagram,
70-
voltageLevelIds: [],
71-
voltageLevelToExpandIds: [...diagram.voltageLevelIds],
72-
});
73-
}
74-
}, [diagram, updateDiagram]);
75-
76-
const handleExpandVoltageLevelId = useCallback(
77-
(voltageLevelIdToExpand: string) => {
78-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
79-
updateDiagram({
80-
...diagram,
81-
voltageLevelIds: diagram.voltageLevelIds.filter((id) => id !== voltageLevelIdToExpand),
82-
voltageLevelToExpandIds: [...diagram.voltageLevelToExpandIds, voltageLevelIdToExpand],
83-
});
84-
}
85-
},
86-
[diagram, updateDiagram]
87-
);
88-
89-
const handleHideVoltageLevelId = useCallback(
90-
(voltageLevelIdToOmit: string) => {
91-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
92-
updateDiagram({
93-
...diagram,
94-
voltageLevelIds: diagram.voltageLevelIds.filter((id) => id !== voltageLevelIdToOmit),
95-
voltageLevelToOmitIds: [...diagram.voltageLevelToOmitIds, voltageLevelIdToOmit],
96-
});
97-
}
98-
},
99-
[diagram, updateDiagram]
100-
);
101-
102-
const handleAddVoltageLevel = useCallback(
103-
(voltageLevelIdToAdd: string) => {
104-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
105-
if (diagram.voltageLevelIds.includes(voltageLevelIdToAdd)) {
106-
return;
107-
}
108-
updateDiagram({
109-
...diagram,
110-
voltageLevelIds: [...diagram.voltageLevelIds, voltageLevelIdToAdd],
111-
voltageLevelToOmitIds: diagram.voltageLevelToOmitIds.filter((id) => id !== voltageLevelIdToAdd),
112-
});
113-
}
114-
},
115-
[diagram, updateDiagram]
116-
);
117-
118-
const handleMoveNode = useCallback(
119-
(vlId: string, x: number, y: number) => {
120-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
121-
const updatedPositions = diagram.positions.map((position) =>
122-
position.voltageLevelId === vlId ? { ...position, xPosition: x, yPosition: y } : position
123-
);
124-
125-
updateDiagramPositions({
126-
...diagram,
127-
positions: updatedPositions,
128-
});
129-
}
130-
},
131-
[diagram, updateDiagramPositions]
132-
);
133-
134-
const handleMoveTextnode = useCallback(
135-
(vlId: string, x: number, y: number) => {
136-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
137-
const updatedPositions = diagram.positions.map((position) =>
138-
position.voltageLevelId === vlId ? { ...position, xLabelPosition: x, yLabelPosition: y } : position
139-
);
140-
141-
updateDiagramPositions({
142-
...diagram,
143-
positions: updatedPositions,
144-
});
145-
}
146-
},
147-
[diagram, updateDiagramPositions]
148-
);
149-
const handleReplaceNad = useCallback(
150-
(elementUuid: UUID, elementType: ElementType, elementName: string) => {
151-
if (diagram.type === DiagramType.NETWORK_AREA_DIAGRAM) {
152-
updateDiagram({
153-
...diagram,
154-
name: elementName,
155-
nadConfigUuid: elementType === ElementType.DIAGRAM_CONFIG ? elementUuid : undefined,
156-
filterUuid: elementType === ElementType.FILTER ? elementUuid : undefined,
157-
initializationNadConfigUuid: undefined,
158-
voltageLevelIds: [],
159-
voltageLevelToExpandIds: [],
160-
voltageLevelToOmitIds: [],
161-
positions: [],
162-
});
163-
}
164-
},
165-
[diagram, updateDiagram]
166-
);
167-
16863
// This function is called by the diagram's contents, when they get their sizes from the backend.
16964
const setDiagramSize = useCallback((diagramId: UUID, diagramType: DiagramType, width: number, height: number) => {
17065
console.log('TODO setDiagramSize', diagramId, diagramType, width, height);
17166
// TODO adapt the layout w and h considering those values.
17267
}, []);
17368

174-
const handleNadVoltageLevelClick = useCallback(
175-
(vlId: string): void => {
176-
createDiagram({
177-
diagramUuid: v4() as UUID,
178-
type: DiagramType.VOLTAGE_LEVEL,
179-
voltageLevelId: vlId,
180-
name: '',
181-
});
182-
},
183-
[createDiagram]
184-
);
185-
186-
const handleSldVoltageLevelClick = useCallback(
187-
(vlId: string, event: MouseEvent): void => {
188-
if (event.ctrlKey) {
189-
createDiagram({
190-
diagramUuid: v4() as UUID,
191-
type: DiagramType.VOLTAGE_LEVEL,
192-
voltageLevelId: vlId,
193-
name: '',
194-
});
195-
} else {
196-
updateDiagram({
197-
...diagram,
198-
type: DiagramType.VOLTAGE_LEVEL,
199-
voltageLevelId: vlId,
200-
name: '',
201-
});
202-
}
203-
},
204-
[diagram, createDiagram, updateDiagram]
205-
);
206-
20769
const cardTitle = useMemo((): string => {
20870
if (loading) {
20971
return intl.formatMessage({ id: 'loadingOptions' });
@@ -226,25 +88,23 @@ export const DiagramCard = forwardRef((props: DiagramCardProps, ref: Ref<HTMLDiv
22688
<Box sx={cardStyles.diagramContainer}>
22789
{(diagram.type === DiagramType.VOLTAGE_LEVEL || diagram.type === DiagramType.SUBSTATION) && (
22890
<SingleLineDiagramContent
91+
diagramParams={diagram}
22992
showInSpreadsheet={showInSpreadsheet}
23093
studyUuid={studyUuid}
231-
diagramId={diagram.diagramUuid}
23294
svg={diagram.svg?.svg ?? undefined}
233-
svgType={diagram.type}
23495
svgMetadata={(diagram.svg?.metadata as SLDMetadata) ?? undefined}
23596
loadingState={loading}
23697
diagramSizeSetter={setDiagramSize}
23798
visible={visible}
238-
onNextVoltageLevelClick={handleSldVoltageLevelClick}
239-
diagramTitle={diagram.name}
99+
onNewVoltageLevelDiagram={createDiagram}
100+
onNextVoltageLevelDiagram={updateDiagram}
240101
/>
241102
)}
242103
{diagram.type === DiagramType.NETWORK_AREA_DIAGRAM && (
243104
<NetworkAreaDiagramContent
105+
diagramParams={diagram}
244106
showInSpreadsheet={showInSpreadsheet}
245-
diagramId={diagram.diagramUuid}
246107
svg={diagram.svg?.svg ?? undefined}
247-
svgType={diagram.type}
248108
svgMetadata={(diagram.svg?.metadata as DiagramMetadata) ?? undefined}
249109
svgScalingFactor={
250110
(diagram.svg?.additionalMetadata as DiagramAdditionalMetadata)?.scalingFactor ??
@@ -258,17 +118,8 @@ export const DiagramCard = forwardRef((props: DiagramCardProps, ref: Ref<HTMLDiv
258118
loadingState={loading}
259119
diagramSizeSetter={setDiagramSize}
260120
visible={visible}
261-
isEditNadMode={diagramsInEditMode}
262-
onToggleEditNadMode={(isEditMode) => setDiagramsInEditMode(isEditMode)}
263-
onLoadNad={handleReplaceNad}
264-
onExpandVoltageLevel={handleExpandVoltageLevelId}
265-
onAddVoltageLevel={handleAddVoltageLevel}
266-
onExpandAllVoltageLevels={handleExpandAllVoltageLevels}
267-
onHideVoltageLevel={handleHideVoltageLevelId}
268-
onMoveNode={handleMoveNode}
269-
onMoveTextnode={handleMoveTextnode}
270-
onVoltageLevelClick={handleNadVoltageLevelClick}
271-
customPositions={diagram.positions}
121+
onVoltageLevelClick={createDiagram}
122+
onNadChange={updateDiagram}
272123
/>
273124
)}
274125
</Box>

src/components/grid-layout/cards/diagrams/diagram.type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ type DiagramBaseParams = {
2222
name: string;
2323
};
2424

25-
type VoltageLevelDiagramParams = DiagramBaseParams & {
25+
export type VoltageLevelDiagramParams = DiagramBaseParams & {
2626
type: DiagramType.VOLTAGE_LEVEL;
2727
voltageLevelId: string;
2828
};
29-
type SubstationDiagramParams = DiagramBaseParams & {
29+
export type SubstationDiagramParams = DiagramBaseParams & {
3030
type: DiagramType.SUBSTATION;
3131
substationId: string;
3232
};
33-
type NetworkAreaDiagramParams = DiagramBaseParams & {
33+
export type NetworkAreaDiagramParams = DiagramBaseParams & {
3434
type: DiagramType.NETWORK_AREA_DIAGRAM;
3535
nadConfigUuid: UUID | undefined;
3636
initializationNadConfigUuid?: UUID; // used for initialization, not saved

0 commit comments

Comments
 (0)