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
4 changes: 4 additions & 0 deletions src/components/parameters/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export const VoltageAdornment = {
position: 'end',
text: KILO_VOLT,
};

export const VERSION_PARAMETER = 'version';
export const COMMON_PARAMETERS = 'commonParameters';
export const SPECIFIC_PARAMETERS = 'specificParametersPerProvider';
114 changes: 114 additions & 0 deletions src/components/parameters/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* 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 { UseFormReturn } from 'react-hook-form';
import {
ParameterType,
SpecificParameterInfos,
SpecificParametersDescription,
SpecificParametersValues,
} from '../../../utils';
import { SPECIFIC_PARAMETERS } from './constant';
import yup from '../../../utils/yupConfig';

export const getSpecificParametersFormSchema = (specificParameters: SpecificParameterInfos[] | undefined) => {
const shape: { [key: string]: yup.AnySchema } = {};

specificParameters?.forEach((param: SpecificParameterInfos) => {
switch (param.type) {
case ParameterType.STRING:
shape[param.name] = yup.string().required();
break;
case ParameterType.DOUBLE:
shape[param.name] = yup.number().required();
break;
case ParameterType.INTEGER:
shape[param.name] = yup.number().required();
break;
case ParameterType.BOOLEAN:
shape[param.name] = yup.boolean().required();
break;
case ParameterType.STRING_LIST:
shape[param.name] = yup.array().of(yup.string()).required();
break;
default:
shape[param.name] = yup.mixed().required();
}
});

return yup.object().shape({
[SPECIFIC_PARAMETERS]: yup.object().shape(shape),
});
};

export const getDefaultSpecificParamsValues = (
specificParametersDescriptionForProvider: SpecificParameterInfos[]
): SpecificParametersValues => {
return specificParametersDescriptionForProvider.reduce(
(acc: SpecificParametersValues, param: SpecificParameterInfos) => {
if (param.type === ParameterType.STRING_LIST && param.defaultValue === null) {
acc[param.name] = [];
} else if (
(param.type === ParameterType.DOUBLE || param.type === ParameterType.INTEGER) &&
Number.isNaN(Number(param.defaultValue))
) {
acc[param.name] = 0;
} else {
acc[param.name] = param.defaultValue;
}
return acc;
},
{}
);
};

export const formatSpecificParameters = (
specificParametersDescriptionForProvider: SpecificParameterInfos[],
specificParamsList: SpecificParametersValues
): SpecificParametersValues => {
return specificParametersDescriptionForProvider.reduce(
(acc: SpecificParametersValues, param: SpecificParameterInfos) => {
if (specificParamsList && Object.hasOwn(specificParamsList, param.name)) {
if (param.type === ParameterType.BOOLEAN) {
acc[param.name] = specificParamsList[param.name] === 'true';
} else if (param.type === ParameterType.STRING_LIST) {
acc[param.name] =
specificParamsList[param.name] === ''
? []
: (specificParamsList[param.name] as string).split(',');
} else {
acc[param.name] = specificParamsList[param.name];
}
} else {
acc[param.name] = getDefaultSpecificParamsValues([param])?.[param.name];
}
return acc;
},
{}
);
};

export const getAllSpecificParametersValues = (
formData: Record<string, any>,
_specificParametersValues: SpecificParametersValues
): SpecificParametersValues => {
return Object.keys(formData[SPECIFIC_PARAMETERS]).reduce((acc: SpecificParametersValues, key: string) => {
if (_specificParametersValues[key].toString() !== formData[SPECIFIC_PARAMETERS][key].toString()) {
acc[key] = formData[SPECIFIC_PARAMETERS][key].toString();
}
return acc;
}, {});
};

export const setSpecificParameters = (
provider: string,
specificParamsDescriptions: SpecificParametersDescription | null,
formMethods: UseFormReturn
) => {
const specificParams = provider ? (specificParamsDescriptions?.[provider] ?? []) : [];
const specificParamsValues = getDefaultSpecificParamsValues(specificParams);
formMethods.setValue(SPECIFIC_PARAMETERS, specificParamsValues);
};
5 changes: 0 additions & 5 deletions src/components/parameters/loadflow/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
export const PARAM_LIMIT_REDUCTION = 'limitReduction';
export const PARAM_PROVIDER_OPENLOADFLOW = 'OpenLoadFlow';

export const COMMON_PARAMETERS = 'commonParameters';
export const SPECIFIC_PARAMETERS = 'specificParametersPerProvider';

export const VERSION_PARAMETER = 'version';

// BasicLoadFlowParameters
export const PHASE_SHIFTER_REGULATION_ON = 'phaseShifterRegulationOn';
export const DC = 'dc';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { memo } from 'react';
import LoadFlowParameterField from './load-flow-parameter-field';
import {
BALANCE_TYPE,
COMMON_PARAMETERS,
CONNECTED_COMPONENT_MODE,
COUNTRIES_TO_BALANCE,
DC,
Expand All @@ -20,7 +19,6 @@ import {
PHASE_SHIFTER_REGULATION_ON,
READ_SLACK_BUS,
SHUNT_COMPENSATOR_VOLTAGE_CONTROL_ON,
SPECIFIC_PARAMETERS,
TWT_SPLIT_SHUNT_ADMITTANCE,
USE_REACTIVE_LIMITS,
VOLTAGE_INIT_MODE,
Expand All @@ -29,6 +27,7 @@ import {
import { useLoadFlowContext } from './use-load-flow-context';
import { ParameterType, SpecificParameterInfos } from '../../../utils/types/parameters.type';
import { ParameterGroup } from '../common/widget';
import { COMMON_PARAMETERS, SPECIFIC_PARAMETERS } from '../common';

const basicParams: SpecificParameterInfos[] = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ export function LoadFlowParametersForm({
handleTabChange,
tabIndexesWithError,
formattedProviders,
specificParameters,
specificParametersDescriptionForProvider,
params,
currentProvider,
defaultLimitReductions,
paramsLoaded,
} = loadflowMethods;
console.log('SBO LoadFlowParametersForm paramsLoaded', paramsLoaded);
console.log('SBO LoadFlowParametersForm formState', formMethods.formState);

return (
<CustomFormProvider validationSchema={formSchema} {...formMethods} removeOptional language={language}>
Expand All @@ -83,7 +85,7 @@ export function LoadFlowParametersForm({
<LoadFlowParametersContent
selectedTab={selectedTab}
currentProvider={currentProvider ?? ''}
specificParameters={specificParameters}
specificParameters={specificParametersDescriptionForProvider}
params={params}
defaultLimitReductions={defaultLimitReductions}
/>
Expand Down
19 changes: 19 additions & 0 deletions src/components/parameters/loadflow/load-flow-parameters-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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 type { UUID } from 'node:crypto';
import { ILimitReductionsByVoltageLevel } from '../common';
import { SpecificParametersPerProvider } from '../../../utils';

export interface LoadFlowParametersInfos {
uuid?: UUID;
provider: string;
limitReduction: number;
commonParameters: Record<string, boolean | string | string[] | number>;
specificParametersPerProvider: SpecificParametersPerProvider;
limitReductions: ILimitReductionsByVoltageLevel[];
}
70 changes: 7 additions & 63 deletions src/components/parameters/loadflow/load-flow-parameters-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
*/

import { UseFormReturn } from 'react-hook-form';
import { ILimitReductionsByVoltageLevel, IST_FORM, LIMIT_DURATION_FORM, LIMIT_REDUCTIONS_FORM } from '../common';
import {
BALANCE_TYPE,
COMMON_PARAMETERS,
ILimitReductionsByVoltageLevel,
IST_FORM,
LIMIT_DURATION_FORM,
LIMIT_REDUCTIONS_FORM,
} from '../common';
import {
BALANCE_TYPE,
CONNECTED_COMPONENT_MODE,
COUNTRIES_TO_BALANCE,
DC,
Expand All @@ -23,16 +28,13 @@ import {
PHASE_SHIFTER_REGULATION_ON,
READ_SLACK_BUS,
SHUNT_COMPENSATOR_VOLTAGE_CONTROL_ON,
SPECIFIC_PARAMETERS,
TWT_SPLIT_SHUNT_ADMITTANCE,
USE_REACTIVE_LIMITS,
VOLTAGE_INIT_MODE,
WRITE_SLACK_BUS,
} from './constants';
import { toFormValuesLimitReductions } from '../common/limitreductions/limit-reductions-form-util';
import yup from '../../../utils/yupConfig';
import { ParameterType, SpecificParameterInfos } from '../../../utils/types/parameters.type';
import { SpecificParametersPerProvider } from '../../../utils/types/loadflow.type';

export enum TabValues {
GENERAL = 'General',
Expand Down Expand Up @@ -77,64 +79,6 @@ export const getCommonLoadFlowParametersFormSchema = () => {
});
};

export const getSpecificLoadFlowParametersFormSchema = (specificParameters: SpecificParameterInfos[]) => {
const shape: { [key: string]: yup.AnySchema } = {};

specificParameters?.forEach((param: SpecificParameterInfos) => {
switch (param.type) {
case ParameterType.STRING:
shape[param.name] = yup.string().required();
break;
case ParameterType.DOUBLE:
shape[param.name] = yup.number().required();
break;
case ParameterType.INTEGER:
shape[param.name] = yup.number().required();
break;
case ParameterType.BOOLEAN:
shape[param.name] = yup.boolean().required();
break;
case ParameterType.STRING_LIST:
shape[param.name] = yup.array().of(yup.string()).required();
break;
default:
shape[param.name] = yup.mixed().required();
}
});

return yup.object().shape({
[SPECIFIC_PARAMETERS]: yup.object().shape(shape),
});
};

export const getDefaultSpecificParamsValues = (
specificParams: SpecificParameterInfos[]
): SpecificParametersPerProvider => {
return specificParams?.reduce((acc: Record<string, any>, param: SpecificParameterInfos) => {
if (param.type === ParameterType.STRING_LIST && param.defaultValue === null) {
acc[param.name] = [];
} else if (
(param.type === ParameterType.DOUBLE || param.type === ParameterType.INTEGER) &&
Number.isNaN(Number(param.defaultValue))
) {
acc[param.name] = 0;
} else {
acc[param.name] = param.defaultValue;
}
return acc;
}, {});
};

export const setSpecificParameters = (
provider: string,
specificParamsDescriptions: Record<string, SpecificParameterInfos[]> | null,
formMethods: UseFormReturn
) => {
const specificParams = provider ? (specificParamsDescriptions?.[provider] ?? []) : [];
const specificParamsValues = getDefaultSpecificParamsValues(specificParams);
formMethods.setValue(SPECIFIC_PARAMETERS, specificParamsValues);
};

export const setLimitReductions = (
provider: string,
defaultLimitReductions: ILimitReductionsByVoltageLevel[],
Expand Down
Loading
Loading