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
152 changes: 44 additions & 108 deletions src/components/grid-layout/cards/map/map-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
* 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 { Box, Dialog, Fab, Theme, useTheme } from '@mui/material';
import { forwardRef, MouseEventHandler, Ref, TouchEventHandler, useCallback, useRef, useState } from 'react';
import CustomCardHeader from '../custom-card-header';
import { Box, Dialog, Fab, Theme } from '@mui/material';
import { useCallback, useRef } from 'react';
import { UUID } from 'crypto';
import AlertCustomMessageNode from 'components/utils/alert-custom-message-node';
import { EquipmentType, LineFlowMode, mergeSx, useStateBoolean } from '@gridsuite/commons-ui';
import { EquipmentType, LineFlowMode, NetworkVisualizationParameters, useStateBoolean } from '@gridsuite/commons-ui';
import { useDispatch, useSelector } from 'react-redux';
import { AppState } from 'redux/reducer';
import { resetMapEquipment, setMapDataLoading, setOpenMap, setReloadMapNeeded } from 'redux/actions';
import WorldSvg from 'images/world.svg?react';
import NetworkMapPanel, { NetworkMapPanelRef } from 'components/network/network-map-panel';
import { cardStyles } from '../card-styles';
import { Close } from '@mui/icons-material';
import { FormattedMessage, useIntl } from 'react-intl';
import { FormattedMessage } from 'react-intl';
import { CurrentTreeNode } from 'components/graph/tree-node.type';

const styles = {
closeButton: (theme: Theme) => ({
Expand All @@ -27,61 +25,32 @@ const styles = {
}),
};

interface ReactGridLayoutCustomChildComponentProps {
style?: React.CSSProperties;
className?: string;
onMouseDown?: MouseEventHandler<HTMLElement>;
onMouseUp?: MouseEventHandler<HTMLElement>;
onTouchEnd?: TouchEventHandler<HTMLElement>;
children?: React.ReactNode;
}

interface MapCardProps extends ReactGridLayoutCustomChildComponentProps {
interface MapCardProps {
studyUuid: UUID;
onClose: () => void;
errorMessage?: string;
showInSpreadsheet: (equipment: { equipmentId: string | null; equipmentType: EquipmentType | null }) => void;
onOpenNetworkAreaDiagram: (elementId?: string) => void;
key: string; // Required for React Grid Layout to identify the component
currentRootNetworkUuid: UUID;
networkVisuParams: NetworkVisualizationParameters;
currentNode: CurrentTreeNode;
}

export const MapCard = forwardRef((props: MapCardProps, ref: Ref<HTMLDivElement>) => {
export const MapCard = (props: MapCardProps) => {
const {
studyUuid,
onClose,
errorMessage,
currentRootNetworkUuid,
networkVisuParams,
showInSpreadsheet,
onOpenNetworkAreaDiagram,
...reactGridLayoutCustomChildComponentProps
currentNode,
} = props;
const { style, children, ...otherProps } = reactGridLayoutCustomChildComponentProps;
const [isHover, setIsHover] = useState(false);
const intl = useIntl();

const handleMouseEnter = () => {
setIsHover(true);
};
const handleMouseLeave = () => {
setIsHover(false);
};

const dispatch = useDispatch();
const theme = useTheme();

const mapOpen = useSelector((state: AppState) => state.mapOpen);

const currentNode = useSelector((state: AppState) => state.currentTreeNode);
const currentRootNetworkUuid = useSelector((state: AppState) => state.currentRootNetworkUuid);
const networkVisuParams = useSelector((state: AppState) => state.networkVisualizationsParameters);
const clickable = !errorMessage;

const handleOpenMap = useCallback(() => {
dispatch(resetMapEquipment());
dispatch(setMapDataLoading(false));
dispatch(setReloadMapNeeded(true));
dispatch(setOpenMap(true));
}, [dispatch]);

const isInDrawingMode = useStateBoolean(false);
const networkMapPanelRef = useRef<NetworkMapPanelRef>(null);

Expand All @@ -97,77 +66,44 @@ export const MapCard = forwardRef((props: MapCardProps, ref: Ref<HTMLDivElement>
dispatch(resetMapEquipment());
dispatch(setMapDataLoading(false));
dispatch(setReloadMapNeeded(true));
onClose();
},
[dispatch, isInDrawingMode]
[dispatch, isInDrawingMode, onClose]
);

if (!studyUuid || !currentNode || !currentRootNetworkUuid || !networkVisuParams) {
return (
<Box sx={mergeSx(style, cardStyles.card)} ref={ref} {...otherProps}>
<CustomCardHeader title={intl.formatMessage({ id: 'MapCard' })} onClose={onClose} />
<AlertCustomMessageNode message={'MapCardNotAvailable'} noMargin style={cardStyles.alertMessage} />
<Box sx={cardStyles.diagramContainer} /> {/* Empty container to keep the layout */}
</Box>
);
}

return (
<Box sx={mergeSx(style, cardStyles.card)} ref={ref} {...otherProps}>
<CustomCardHeader title={intl.formatMessage({ id: 'MapCard' })} onClose={onClose} />
{errorMessage && <AlertCustomMessageNode message={errorMessage} noMargin style={cardStyles.alertMessage} />}
<Box sx={cardStyles.diagramContainer}>
<WorldSvg
style={{
width: '100%',
height: '100%',
cursor: clickable ? 'pointer' : 'default',
backgroundColor: clickable && isHover ? 'rgba(0, 0, 0, 0.4)' : 'inherit',
<Box sx={cardStyles.diagramContainer}>
<Dialog open={mapOpen} onClose={handleCloseMap} fullScreen>
<Fab
onClick={handleCloseMap}
size="small"
aria-label="close"
variant="extended"
sx={styles.closeButton}
>
<Close fontSize="small" />
<FormattedMessage id="close" />
</Fab>
<NetworkMapPanel
ref={networkMapPanelRef}
studyUuid={studyUuid}
visible={mapOpen}
lineFullPath={networkVisuParams.mapParameters.lineFullPath}
lineParallelPath={networkVisuParams.mapParameters.lineParallelPath}
lineFlowMode={networkVisuParams.mapParameters.lineFlowMode as LineFlowMode}
currentNode={currentNode}
currentRootNetworkUuid={currentRootNetworkUuid}
showInSpreadsheet={(eq) => {
handleCloseMap();
showInSpreadsheet(eq);
}}
fill={clickable && isHover ? theme.palette.grey[800] : theme.palette.grey[500]}
onClick={
clickable
? (e) => {
e.stopPropagation();
handleOpenMap();
}
: undefined
}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onOpenNetworkAreaDiagram={onOpenNetworkAreaDiagram}
onPolygonChanged={() => {}}
isInDrawingMode={isInDrawingMode}
/>
<Dialog open={mapOpen} onClose={handleCloseMap} fullScreen>
<Fab
onClick={handleCloseMap}
size="small"
aria-label="close"
variant="extended"
sx={styles.closeButton}
>
<Close fontSize="small" />
<FormattedMessage id="close" />
</Fab>
<NetworkMapPanel
ref={networkMapPanelRef}
studyUuid={studyUuid}
visible={mapOpen}
lineFullPath={networkVisuParams.mapParameters.lineFullPath}
lineParallelPath={networkVisuParams.mapParameters.lineParallelPath}
lineFlowMode={networkVisuParams.mapParameters.lineFlowMode as LineFlowMode}
currentNode={currentNode}
currentRootNetworkUuid={currentRootNetworkUuid}
showInSpreadsheet={(eq) => {
handleCloseMap();
showInSpreadsheet(eq);
}}
onOpenNetworkAreaDiagram={onOpenNetworkAreaDiagram}
onPolygonChanged={() => {}}
isInDrawingMode={isInDrawingMode}
/>
</Dialog>
</Box>
{children}
</Dialog>
</Box>
);
});
};

export default MapCard;
8 changes: 1 addition & 7 deletions src/components/grid-layout/diagram-grid-layout.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import { DiagramParams, DiagramParamsDto } from 'components/grid-layout/cards/diagrams/diagram.type';
import { UUID } from 'crypto';
import { Layout } from 'react-grid-layout';

export type DiagramLayoutParam = DiagramParams & {
Expand All @@ -17,12 +16,7 @@ export interface DiagramGridLayout {
diagramLayouts: DiagramLayoutParam[];
}

type MapDTO = {
diagramUuid: UUID;
type: 'map';
};

export type DiagramLayoutDto = (DiagramParamsDto | MapDTO) & {
export type DiagramLayoutDto = DiagramParamsDto & {
diagramPositions: Record<string, Pick<Layout, 'w' | 'h' | 'x' | 'y'>>;
};

Expand Down
68 changes: 42 additions & 26 deletions src/components/grid-layout/grid-layout-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { useCallback, useState, useRef } from 'react';
import { useCallback, useState, useRef, useMemo } from 'react';
import { Layout, Layouts, ItemCallback, Responsive, WidthProvider } from 'react-grid-layout';
import { useDiagramModel } from './hooks/use-diagram-model';
import { Diagram, DiagramParams, DiagramType } from './cards/diagrams/diagram.type';
Expand All @@ -22,6 +22,9 @@ import { BLINK_LENGTH_MS } from './cards/custom-card-header';
import CustomResizeHandle from './custom-resize-handle';
import { useSaveDiagramLayout } from './hooks/use-save-diagram-layout';
import { isThereTooManyOpenedNadDiagrams } from './cards/diagrams/diagram-utils';
import { resetMapEquipment, setMapDataLoading, setOpenMap, setReloadMapNeeded } from 'redux/actions';
import { useDispatch, useSelector } from 'react-redux';
import { AppState } from 'redux/reducer';

const styles = {
container: {
Expand Down Expand Up @@ -159,8 +162,14 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
const responsiveGridLayoutRef = useRef<any>(null);
const currentBreakpointRef = useRef<string>('lg');
const lastModifiedBreakpointRef = useRef<string>('lg'); // Track the last modified breakpoint
const [isMapOpen, setIsMapOpen] = useState<boolean>(false);
const dispatch = useDispatch();
const currentNode = useSelector((state: AppState) => state.currentTreeNode);

const { snackInfo } = useSnackMessage();
const currentRootNetworkUuid = useSelector((state: AppState) => state.currentRootNetworkUuid);
const networkVisuParams = useSelector((state: AppState) => state.networkVisualizationsParameters);

const { snackInfo, snackError } = useSnackMessage();

// Blinking diagrams management
const stopDiagramBlinking = useCallback((diagramUuid: UUID) => {
Expand All @@ -181,12 +190,6 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
);

// Grid operations
const isMapCardAdded = () => {
return Object.values(layouts).some((breakpointLayouts) =>
breakpointLayouts.some((layout) => layout.i === 'MapCard')
);
};

const addLayoutItem = useCallback((diagram: Diagram) => {
lastModifiedBreakpointRef.current = currentBreakpointRef.current;
setLayouts((currentLayouts) => createLayoutItem(diagram.diagramUuid, currentLayouts));
Expand All @@ -206,13 +209,25 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
});
}, []);

const onAddMapCard = useCallback(() => {
setLayouts((currentLayouts) => createLayoutItem('MapCard', currentLayouts));
}, []);
const loadingMapError = useMemo(() => {
return !currentNode || !currentRootNetworkUuid;
}, [currentNode, currentRootNetworkUuid]);

const handleRemoveMapCard = useCallback(() => {
removeLayoutItem('MapCard');
}, [removeLayoutItem]);
const onOpenMap = useCallback(() => {
if (loadingMapError) {
snackError({ messageId: 'MapCardNotAvailable' });
return;
}
dispatch(resetMapEquipment());
dispatch(setMapDataLoading(false));
dispatch(setReloadMapNeeded(true));
dispatch(setOpenMap(true));
setIsMapOpen(true);
}, [dispatch, loadingMapError, snackError]);

const handleCloseMap = useCallback(() => {
setIsMapOpen(false);
}, []);

const {
diagrams,
Expand Down Expand Up @@ -425,14 +440,13 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re

// Debounce the layout save function to avoid excessive calls
const debouncedGridLayoutSave = useDebounce(handleGridLayoutSave, 300);

return (
<Box sx={styles.container}>
<GridLayoutToolbar
onLoad={handleLoadNad}
onSearch={showVoltageLevelDiagram}
onOpenNetworkAreaDiagram={showGrid}
onMap={!isMapCardAdded() ? onAddMapCard : undefined}
onMap={onOpenMap}
onLayoutSave={debouncedGridLayoutSave}
/>
<ResponsiveGridLayout
Expand Down Expand Up @@ -487,17 +501,19 @@ function GridLayoutPanel({ studyUuid, showInSpreadsheet, showGrid, visible }: Re
/>
);
})}
{isMapCardAdded() && (
<MapCard
key={'MapCard'}
studyUuid={studyUuid}
onClose={handleRemoveMapCard}
errorMessage={globalError}
showInSpreadsheet={showInSpreadsheet}
onOpenNetworkAreaDiagram={onOpenNetworkAreaDiagram}
/>
)}
</ResponsiveGridLayout>
{isMapOpen && currentRootNetworkUuid && currentNode && (
<MapCard
studyUuid={studyUuid}
currentNode={currentNode}
currentRootNetworkUuid={currentRootNetworkUuid}
networkVisuParams={networkVisuParams}
onClose={handleCloseMap}
errorMessage={'MapCardNotAvailable'}
showInSpreadsheet={showInSpreadsheet}
onOpenNetworkAreaDiagram={onOpenNetworkAreaDiagram}
/>
)}
</Box>
);
}
Expand Down
9 changes: 0 additions & 9 deletions src/components/grid-layout/hooks/use-save-diagram-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { DiagramGridLayoutDto, DiagramLayoutDto } from 'components/grid-layout/d
import { MAX_INT32 } from 'services/utils';
import { saveDiagramGridLayout } from 'services/study/study-config';
import { useSnackMessage } from '@gridsuite/commons-ui';
import { v4 } from 'uuid';

interface UseSaveDiagramLayoutProps {
layouts: Layouts;
Expand Down Expand Up @@ -64,14 +63,6 @@ const frontendToBackendDiagramGridLayout = (diagram: DiagramGridLayoutConfig): D
}
});

// get the map from gridLayoutById
const transformedMapDTO: DiagramLayoutDto = {
diagramUuid: v4() as UUID,
type: 'map',
diagramPositions: gridLayoutById['MapCard'],
};
diagramLayouts.push(transformedMapDTO);

return {
diagramLayouts: diagramLayouts,
};
Expand Down
Loading
Loading