Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS migration - components/utils #2616

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { IconButton, MenuItem, Select } from '@mui/material';
import ClearIcon from '@mui/icons-material/Clear';
import { useIntl } from 'react-intl';
import { SelectChangeEvent } from '@mui/material/Select/SelectInput';
import { mergeSx } from 'components/utils/functions';
import { useCustomAggridFilter } from './hooks/use-custom-aggrid-filter';
import { CustomAggridFilterParams, FILTER_DATA_TYPES, FILTER_TEXT_COMPARATORS } from '../custom-aggrid-header.type';
import { isNonEmptyStringOrArray } from '../../../utils/types-utils';
import { mergeSx } from '@gridsuite/commons-ui';
import { BooleanFilterValue } from './utils/aggrid-filters-utils';

const styles = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { TextField, Grid, InputAdornment, IconButton } from '@mui/material';
import ClearIcon from '@mui/icons-material/Clear';
import { DisplayRounding } from '../display-rounding';
import { FILTER_DATA_TYPES } from '../custom-aggrid-header.type';
import { mergeSx } from '../../utils/functions';
import { useIntl } from 'react-intl';
import { mergeSx } from '@gridsuite/commons-ui';

const styles = {
input: {
Expand Down Expand Up @@ -60,7 +60,7 @@ export const CustomAggridTextFilter: React.FC<CustomAggridTextFilterProps> = ({
inputProps={{
type: isNumberInput ? FILTER_DATA_TYPES.NUMBER : FILTER_DATA_TYPES.TEXT,
}}
sx={mergeSx(styles.input, isNumberInput && styles.noArrows)}
sx={mergeSx(styles.input, isNumberInput ? styles.noArrows : undefined)}
InputProps={{
endAdornment: value ? (
<InputAdornment position="end">
Expand Down
41 changes: 30 additions & 11 deletions src/components/diagrams/diagram-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,41 @@ export const useDiagram = () => {
};
};

export interface Svg {
export interface SldAdditionalMetadata {
id: string;
country: string;
substationId?: string;
}

export interface SldSvg {
svg: string | null;
metadata: SLDMetadata | null;
additionalMetadata: SldAdditionalMetadata | null;
error?: string | null;
svgUrl?: string | null;
}

export interface DiagramAdditionalMetadata {
nbVoltageLevels: number;
scalingFactor: number;
voltageLevels: {
id: string;
substationId: UUID;
country: string;
name: string;
}[];
}

export interface DiagramSvg {
svg: string | null;
metadata: SLDMetadata | DiagramMetadata | null;
additionalMetadata:
| (SLDMetadata & {
country: string;
substationId?: string;
voltageLevels: { name: string; substationId: UUID }[];
nbVoltageLevels?: number;
scalingFactor?: number;
})
| null;
metadata: DiagramMetadata | null;
additionalMetadata: DiagramAdditionalMetadata | null;
error?: string | null;
svgUrl?: string | null;
}

export type Svg = DiagramSvg | SldSvg;

export const NoSvg: Svg = {
svg: null,
metadata: null,
Expand Down
5 changes: 2 additions & 3 deletions src/components/diagrams/diagram-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import { useCallback, useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import Box from '@mui/material/Box';
import { OverflowableText } from '@gridsuite/commons-ui';
import { mergeSx, OverflowableText } from '@gridsuite/commons-ui';
import IconButton from '@mui/material/IconButton';
import MinimizeIcon from '@mui/icons-material/Minimize';
import PushPinIcon from '@mui/icons-material/PushPin';
import PushPinOutlinedIcon from '@mui/icons-material/PushPinOutlined';
import CloseIcon from '@mui/icons-material/Close';
import { stopDiagramBlink } from '../../redux/actions';
import { mergeSx } from '../utils/functions';
import { Theme } from '@mui/material';
import { AppState } from 'redux/reducer';

Expand Down Expand Up @@ -121,7 +120,7 @@ const DiagramHeader: React.FC<DiagramHeaderProps> = ({
*/

return (
<Box sx={mergeSx(styles.header, blinking && styles.blink)}>
<Box sx={mergeSx(styles.header, blinking ? styles.blink : undefined)}>
<OverflowableText sx={{ flexGrow: '1' }} text={diagramTitle} />
<Box>
<Box
Expand Down
46 changes: 25 additions & 21 deletions src/components/diagrams/diagram-pane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import {
DEFAULT_WIDTH_SUBSTATION,
DEFAULT_WIDTH_VOLTAGE_LEVEL,
DIAGRAM_MAP_RATIO_MIN_PERCENTAGE,
DiagramAdditionalMetadata,
DiagramSvg,
DiagramType,
MAP_BOTTOM_OFFSET,
NETWORK_AREA_DIAGRAM_NB_MAX_VOLTAGE_LEVELS,
NoSvg,
SldSvg,
Svg,
useDiagram,
ViewState,
Expand All @@ -36,12 +39,11 @@ import { useNameOrId } from '../utils/equipmentInfosHandler';
import { syncDiagramStateWithSessionStorage } from '../../redux/session-storage/diagram-state';
import SingleLineDiagramContent from './singleLineDiagram/single-line-diagram-content';
import NetworkAreaDiagramContent from './networkAreaDiagram/network-area-diagram-content';
import { EquipmentType, OverflowableText, useDebounce, useSnackMessage } from '@gridsuite/commons-ui';
import { EquipmentType, mergeSx, OverflowableText, useDebounce, useSnackMessage } from '@gridsuite/commons-ui';
import { setNetworkAreaDiagramNbVoltageLevels } from '../../redux/actions';
import { useIntl } from 'react-intl';
import { getSubstationSingleLineDiagram, getVoltageLevelSingleLineDiagram } from '../../services/study/network';
import { fetchSvg, getNetworkAreaDiagramUrl } from '../../services/study';
import { mergeSx } from '../utils/functions';
import { useLocalizedCountries } from 'components/utils/localized-countries-hook';
import { UUID } from 'crypto';
import { AppState, CurrentTreeNode, DiagramState } from 'redux/reducer';
Expand Down Expand Up @@ -131,18 +133,19 @@ const useDisplayView = (studyUuid: UUID, currentNode: CurrentTreeNode, currentRo
]
);

type FetchSvgDataFn = {
(svgUrl: string | null, svgType: DiagramType.SUBSTATION | DiagramType.VOLTAGE_LEVEL): Promise<SldSvg>;
(svgUrl: string | null, svgType: DiagramType.NETWORK_AREA_DIAGRAM): Promise<DiagramSvg>;
};
// this callback returns a promise
const fetchSvgData = useCallback(
(svgUrl: string | null, svgType: DiagramType): Promise<Svg> => {
const fetchSvgData = useCallback<FetchSvgDataFn>(
(svgUrl, svgType): any => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we avoid any ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should, but I've not found a clean way to do something cleaner
this "any" only prevents the method from checking its returned value

we still have good in/out types when using this method

if (svgUrl) {
const fetchSvgPromise: Promise<Svg> = fetchSvg(svgUrl);
return fetchSvgPromise
return fetchSvg(svgUrl)
.then((data: Svg | null) => {
if (data !== null) {
return {
svg: data.svg,
metadata: data.metadata,
additionalMetadata: data.additionalMetadata,
...data,
error: undefined,
};
} else {
Expand Down Expand Up @@ -206,7 +209,7 @@ const useDisplayView = (studyUuid: UUID, currentNode: CurrentTreeNode, currentRo
function createVoltageLevelDiagramView(id: UUID, state: ViewState | undefined) {
const svgUrl = checkAndGetVoltageLevelSingleLineDiagramUrl(id);
return fetchSvgData(svgUrl, DiagramType.VOLTAGE_LEVEL).then((svg) => {
let label = getNameOrId(svg.additionalMetadata) ?? id;
let label = getNameOrId(svg.additionalMetadata);
let substationId = svg.additionalMetadata?.substationId;
return {
id: id,
Expand All @@ -230,17 +233,14 @@ const useDisplayView = (studyUuid: UUID, currentNode: CurrentTreeNode, currentRo
let nadTitle = '';
let substationsIds: UUID[] = [];
svg.additionalMetadata?.voltageLevels
.map((vl: { name: string; substationId: UUID }) => ({
name: getNameOrId(vl),
.map((vl) => ({
name: getNameOrId({ name: vl.name, id: vl.substationId }),
substationId: vl.substationId,
}))
.sort(
(
vlA: { name: string; substationId: UUID },
vlB: { name: string; substationId: UUID }
) => vlA.name.toLowerCase().localeCompare(vlB.name.toLowerCase())
(vlA, vlB) => vlA.name?.toLowerCase().localeCompare(vlB.name?.toLowerCase() ?? '') || 0
)
.forEach((voltageLevel: { name: string; substationId: UUID }) => {
.forEach((voltageLevel) => {
const name = voltageLevel.name;
if (name !== null) {
nadTitle += (nadTitle !== '' ? ', ' : '') + name;
Expand All @@ -260,7 +260,7 @@ const useDisplayView = (studyUuid: UUID, currentNode: CurrentTreeNode, currentRo
svgType: DiagramType.NETWORK_AREA_DIAGRAM,
depth: depth,
substationIds: substationsIds,
nadMetadata: svg.metadata as DiagramMetadata,
nadMetadata: svg.metadata,
scalingFactor: svg.additionalMetadata?.scalingFactor,
...svg,
};
Expand Down Expand Up @@ -540,7 +540,8 @@ export function DiagramPane({
} as unknown as DiagramView;
dispatch(
setNetworkAreaDiagramNbVoltageLevels(
networkAreaDiagramView.additionalMetadata?.nbVoltageLevels ?? 0
(networkAreaDiagramView.additionalMetadata as DiagramAdditionalMetadata)
?.nbVoltageLevels ?? 0
)
);
return updatedViews;
Expand Down Expand Up @@ -1018,7 +1019,7 @@ export function DiagramPane({

const getDiagramTitle = (diagramView: DiagramView) => {
return diagramView.svgType !== DiagramType.NETWORK_AREA_DIAGRAM
? diagramView.name + ' - ' + translate(diagramView.country)
? diagramView.name + ' - ' + (diagramView.country ? translate(diagramView.country) : '')
: diagramView.name;
};

Expand Down Expand Up @@ -1047,7 +1048,10 @@ export function DiagramPane({
<AutoSizer doNotBailOutOnEmptyChildren>
{({ width, height }) => (
<Box
sx={mergeSx(styles.availableDiagramSurfaceArea, fullScreenDiagram?.id && styles.fullscreen)}
sx={mergeSx(
styles.availableDiagramSurfaceArea,
fullScreenDiagram?.id ? styles.fullscreen : undefined
)}
style={{
width: width + 'px',
height: height + 'px',
Expand Down
6 changes: 3 additions & 3 deletions src/components/diagrams/diagram-resizable-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import { ResizableBox } from 'react-resizable';
import ResizeHandleIcon from '@mui/icons-material/ChevronRight';
import { MIN_HEIGHT, MIN_WIDTH, LOADING_HEIGHT, LOADING_WIDTH } from './diagram-common';
import { mergeSx } from '../utils/functions';
import { styled, Theme } from '@mui/material';
import { mergeSx } from '@gridsuite/commons-ui';

// TODO can we avoid to define a component just to add sx support ?
const ResizableBoxSx = styled(ResizableBox)({});
Expand Down Expand Up @@ -79,8 +79,8 @@ const DiagramResizableBox: React.FC<DiagramResizableBoxProps> = ({
resizeHandles={align === 'right' ? ['sw'] : undefined}
sx={mergeSx(
styles.resizable,
!disableResize && align === 'right' && styles.leftHandle,
!disableResize && align === 'left' && styles.rightHandle
!disableResize && align === 'right' ? styles.leftHandle : undefined,
!disableResize && align === 'left' ? styles.rightHandle : undefined
)}
>
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '@powsybl/network-viewer';
import LinearProgress from '@mui/material/LinearProgress';
import Box from '@mui/material/Box';
import { mergeSx } from '../../utils/functions';
import ComputingType from '../../computing-status/computing-type';
import { AppState, NadNodeMovement, NadTextMovement } from 'redux/reducer';
import { storeNetworkAreaDiagramNodeMovement, storeNetworkAreaDiagramTextNodeMovement } from '../../../redux/actions';
Expand All @@ -35,6 +34,7 @@ import { UUID } from 'crypto';
import { Point } from '@svgdotjs/svg.js';
import { EQUIPMENT_TYPES } from 'components/utils/equipment-types';
import { FEEDER_TYPES } from 'components/utils/feederType';
import { mergeSx } from '@gridsuite/commons-ui';

const dynamicCssRules: CSS_RULE[] = [
{
Expand Down Expand Up @@ -343,7 +343,7 @@ function NetworkAreaDiagramContent(props: NetworkAreaDiagramContentProps) {
sx={mergeSx(
styles.divDiagram,
styles.divNetworkAreaDiagram,
loadFlowStatus !== RunningStatus.SUCCEED && styles.divDiagramInvalid
loadFlowStatus !== RunningStatus.SUCCEED ? styles.divDiagramInvalid : undefined
)}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import { useTheme } from '@mui/material/styles';
import LinearProgress from '@mui/material/LinearProgress';
import { SingleLineDiagramViewer, SLDMetadata } from '@powsybl/network-viewer';
import { styles, MAX_HEIGHT_VOLTAGE_LEVEL, MAX_WIDTH_VOLTAGE_LEVEL, NoSvg, MIN_WIDTH, Svg } from '../diagram-common';
import { useIntlRef, useSnackMessage } from '@gridsuite/commons-ui';
import { mergeSx, useIntlRef, useSnackMessage } from '@gridsuite/commons-ui';
import { Paper } from '@mui/material';
import DiagramHeader from '../diagram-header';
import { fetchSvg } from '../../../services/study';
import { mergeSx } from '../../utils/functions';
import { AppState } from 'redux/reducer';

interface PositionDiagramProps {
Expand Down Expand Up @@ -61,9 +60,7 @@ const PositionDiagram = forwardRef((props: PositionDiagramProps, ref: Ref<HTMLDi
.then((data) => {
if (data !== null) {
setSvg({
svg: data.svg,
metadata: data.metadata,
additionalMetadata: data.additionalMetadata,
...data,
error: null,
svgUrl: props.svgUrl,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { isNodeReadOnly } from '../../graph/util/model-functions';
import { useIsAnyNodeBuilding } from '../../utils/is-any-node-building-hook';
import Alert from '@mui/material/Alert';
import { useTheme } from '@mui/material/styles';
import { EquipmentType, useSnackMessage } from '@gridsuite/commons-ui';
import { EquipmentType, mergeSx, useSnackMessage } from '@gridsuite/commons-ui';
import Box from '@mui/material/Box';
import LinearProgress from '@mui/material/LinearProgress';
import GeneratorModificationDialog from 'components/dialogs/network-modifications/generator/modification/generator-modification-dialog';
Expand All @@ -46,7 +46,6 @@ import { EQUIPMENT_INFOS_TYPES, EQUIPMENT_TYPES, convertToEquipmentType } from '
import EquipmentDeletionDialog from '../../dialogs/network-modifications/equipment-deletion/equipment-deletion-dialog';
import { startShortCircuitAnalysis } from '../../../services/study/short-circuit-analysis';
import { fetchNetworkElementInfos } from '../../../services/study/network';
import { mergeSx } from '../../utils/functions';
import { useOneBusShortcircuitAnalysisLoader } from '../use-one-bus-shortcircuit-analysis-loader';
import { DynamicSimulationEventDialog } from '../../dialogs/dynamicsimulation/event/dynamic-simulation-event-dialog';
import { setComputationStarting, setComputingStatus, setLogsFilter } from '../../../redux/actions';
Expand Down
2 changes: 1 addition & 1 deletion src/components/dialogs/commons/formFiller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { mergeSx } from '@gridsuite/commons-ui';
import { Box, Grid } from '@mui/material';
import { useTheme } from '@mui/material/styles';
import { mergeSx } from '../../utils/functions';

const styles = {
filler: (theme) => ({
Expand Down
Loading
Loading