Skip to content

Commit f96712f

Browse files
authored
Feat GridLayoutPanel: disable Store button if nothing to store (#3307)
Signed-off-by: sBouzols <[email protected]>
1 parent 32860c3 commit f96712f

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/components/grid-layout/grid-layout-panel.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
167167
const responsiveGridLayoutRef = useRef<any>(null);
168168
const currentBreakpointRef = useRef<string>('lg');
169169
const lastModifiedBreakpointRef = useRef<string>('lg'); // Track the last modified breakpoint
170+
const [disableStoreButton, setDisableStoreButton] = useState<boolean>(true);
170171

171172
const { snackInfo } = useSnackMessage();
172173

@@ -198,6 +199,7 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
198199
const addLayoutItem = useCallback((diagram: Diagram) => {
199200
lastModifiedBreakpointRef.current = currentBreakpointRef.current;
200201
setLayouts((currentLayouts) => createLayoutItem(diagram.diagramUuid, currentLayouts));
202+
setDisableStoreButton(false);
201203
}, []);
202204

203205
const removeLayoutItem = useCallback((cardUuid: UUID | string) => {
@@ -212,6 +214,7 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
212214

213215
return newLayouts;
214216
});
217+
setDisableStoreButton(false);
215218
}, []);
216219

217220
const onAddMapCard = useCallback(() => {
@@ -237,6 +240,22 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
237240
onDiagramAlreadyExists,
238241
});
239242

243+
const handleUpdateDiagram = useCallback(
244+
(diagram: Diagram) => {
245+
setDisableStoreButton(false);
246+
updateDiagram(diagram);
247+
},
248+
[updateDiagram]
249+
);
250+
251+
const handleUpdateDiagramPositions = useCallback(
252+
(diagramParams: DiagramParams) => {
253+
setDisableStoreButton(false);
254+
updateDiagramPositions(diagramParams);
255+
},
256+
[updateDiagramPositions]
257+
);
258+
240259
const onRemoveCard = useCallback(
241260
(diagramUuid: UUID) => {
242261
removeLayoutItem(diagramUuid);
@@ -369,6 +388,7 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
369388

370389
return newLayouts;
371390
});
391+
setDisableStoreButton(false);
372392
}, []);
373393

374394
/**
@@ -400,6 +420,7 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
400420
lastModifiedBreakpointRef.current = currentBreakpointRef.current;
401421
// Ensure final order is propagated to all breakpoints
402422
propagateOrder(layout, currentBreakpointRef.current);
423+
setDisableStoreButton(false);
403424
},
404425
[propagateOrder]
405426
);
@@ -411,6 +432,7 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
411432
} else {
412433
setLayouts(initialLayouts);
413434
}
435+
setDisableStoreButton(true);
414436
}, []);
415437
useDiagramsGridLayoutInitialization({ onLoadDiagramLayout });
416438

@@ -429,10 +451,15 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
429451
[diagrams, snackInfo]
430452
);
431453

432-
const handleGridLayoutSave = useSaveDiagramLayout({ layouts, diagrams });
454+
const gridLayoutSave = useSaveDiagramLayout({ layouts, diagrams });
433455

434456
// Debounce the layout save function to avoid excessive calls
435-
const debouncedGridLayoutSave = useDebounce(handleGridLayoutSave, 300);
457+
const debouncedGridLayoutSave = useDebounce(gridLayoutSave, 300);
458+
459+
const handleGridLayoutSave = useCallback(() => {
460+
setDisableStoreButton(true);
461+
debouncedGridLayoutSave();
462+
}, [debouncedGridLayoutSave]);
436463

437464
return (
438465
<Box sx={styles.container}>
@@ -441,7 +468,8 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
441468
onSearch={showVoltageLevelDiagram}
442469
onOpenNetworkAreaDiagram={showGrid}
443470
onMap={!isMapCardAdded() ? onAddMapCard : undefined}
444-
onLayoutSave={debouncedGridLayoutSave}
471+
onLayoutSave={handleGridLayoutSave}
472+
disableStore={disableStoreButton}
445473
/>
446474
<ResponsiveGridLayout
447475
ref={responsiveGridLayoutRef} // the provided innerRef prop is bugged (see https://github.com/react-grid-layout/react-grid-layout/issues/1444)
@@ -490,8 +518,8 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
490518
onClose={() => onRemoveCard(diagram.diagramUuid)}
491519
showInSpreadsheet={showInSpreadsheet}
492520
createDiagram={createDiagram}
493-
updateDiagram={updateDiagram}
494-
updateDiagramPositions={updateDiagramPositions}
521+
updateDiagram={handleUpdateDiagram}
522+
updateDiagramPositions={handleUpdateDiagramPositions}
495523
/>
496524
);
497525
})}

src/components/grid-layout/grid-layout-toolbar.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ interface DiagramGridHeaderProps {
4141
onOpenNetworkAreaDiagram?: (elementId?: string) => void;
4242
onLayoutSave: () => void;
4343
onMap?: () => void;
44+
disableStore?: boolean;
4445
}
4546

4647
export const GridLayoutToolbar = (props: DiagramGridHeaderProps) => {
47-
const { onLoad, onSearch, onOpenNetworkAreaDiagram, onMap, onLayoutSave } = props;
48+
const { onLoad, onSearch, onOpenNetworkAreaDiagram, onMap, onLayoutSave, disableStore = true } = props;
4849

4950
const intl = useIntl();
5051

@@ -85,9 +86,11 @@ export const GridLayoutToolbar = (props: DiagramGridHeaderProps) => {
8586
</Box>
8687
<Box>
8788
<Tooltip title={<FormattedMessage id="StoreButtonTooltip" />}>
88-
<Button onClick={onLayoutSave}>
89-
<FormattedMessage id="StoreDiagramLayout" />
90-
</Button>
89+
<span>
90+
<Button disabled={disableStore} onClick={onLayoutSave}>
91+
<FormattedMessage id="StoreDiagramLayout" />
92+
</Button>
93+
</span>
9194
</Tooltip>
9295
</Box>
9396
</Box>

0 commit comments

Comments
 (0)