|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Button, Grid } from '@mui/material'; |
| 9 | +import { |
| 10 | + CURRENT_LIMITS, |
| 11 | + LIMITS, |
| 12 | + OPERATIONAL_LIMITS_GROUPS, |
| 13 | + SELECTED_LIMITS_GROUP_1, |
| 14 | + SELECTED_LIMITS_GROUP_2, |
| 15 | +} from 'components/utils/field-constants'; |
| 16 | +import { LimitsSidePane } from '../limits-side-pane'; |
| 17 | +import { SelectedOperationalLimitGroup } from '../selected-operational-limit-group.js'; |
| 18 | +import { useCallback, useRef, useState } from 'react'; |
| 19 | +import { useWatch } from 'react-hook-form'; |
| 20 | +import { CurrentLimits, OperationalLimitsGroup } from '../../../../services/network-modification-types'; |
| 21 | +import { OperationalLimitsGroupsTabs } from '../operational-limits-groups-tabs'; |
| 22 | +import { tabStyles } from 'components/utils/tab-utils'; |
| 23 | +import { CurrentTreeNode } from '../../../graph/tree-node.type'; |
| 24 | +import GridSection from '../../commons/grid-section'; |
| 25 | +import { styles } from '../../dialog-utils'; |
| 26 | +import AddIcon from '@mui/icons-material/ControlPoint'; |
| 27 | +import { APPLICABILITY } from '../../../network/constants'; |
| 28 | + |
| 29 | +export interface LimitsPaneCreationProps { |
| 30 | + id?: string; |
| 31 | + currentNode?: CurrentTreeNode; |
| 32 | + equipmentToModify?: any; |
| 33 | + clearableFields?: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +export function LimitsPaneCreation({ |
| 37 | + id = LIMITS, |
| 38 | + currentNode, |
| 39 | + equipmentToModify, |
| 40 | + clearableFields, |
| 41 | +}: Readonly<LimitsPaneCreationProps>) { |
| 42 | + const [indexSelectedLimitSet, setIndexSelectedLimitSet] = useState<number | null>(null); |
| 43 | + |
| 44 | + const myRef: any = useRef<any>(null); |
| 45 | + |
| 46 | + const limitsGroups: OperationalLimitsGroup[] = useWatch({ |
| 47 | + name: `${id}.${OPERATIONAL_LIMITS_GROUPS}`, |
| 48 | + }); |
| 49 | + |
| 50 | + const onAddClick = useCallback(() => myRef.current?.addNewLimitSet(), []); |
| 51 | + |
| 52 | + const getCurrentLimits = (equipmentToModify: any): CurrentLimits | null => { |
| 53 | + if (equipmentToModify?.currentLimits1) { |
| 54 | + return equipmentToModify.currentLimits1.find( |
| 55 | + (currentLimit: CurrentLimits) => currentLimit.id === equipmentToModify.selectedOperationalLimitsGroup1 |
| 56 | + ); |
| 57 | + } |
| 58 | + return null; |
| 59 | + }; |
| 60 | + |
| 61 | + /** |
| 62 | + * returns an error message id if : |
| 63 | + * - there are more than 2 limit sets with the same name |
| 64 | + * - there are exactly 2 limit set with this name but they have the same applicability side |
| 65 | + */ |
| 66 | + const checkLimitSetUnicity = useCallback( |
| 67 | + (editedLimitGroupName: string, newSelectedApplicability: string): string => { |
| 68 | + if (indexSelectedLimitSet == null) { |
| 69 | + return ''; |
| 70 | + } |
| 71 | + |
| 72 | + // checks if limit sets with that name already exist |
| 73 | + const sameNameInLs: OperationalLimitsGroup[] = limitsGroups |
| 74 | + .filter((_ls, index: number) => index !== indexSelectedLimitSet) |
| 75 | + .filter( |
| 76 | + (limitsGroup: OperationalLimitsGroup) => limitsGroup.name.trim() === editedLimitGroupName.trim() |
| 77 | + ); |
| 78 | + |
| 79 | + // only 2 limit sets with the same name are allowed and only if there have SIDE1 and SIDE2 applicability |
| 80 | + if (sameNameInLs.length > 0) { |
| 81 | + if (sameNameInLs.length > 1) { |
| 82 | + return 'LimitSetNamingError'; |
| 83 | + } |
| 84 | + |
| 85 | + if ( |
| 86 | + sameNameInLs[0].applicability === newSelectedApplicability || |
| 87 | + sameNameInLs[0].applicability === APPLICABILITY.EQUIPMENT.id || |
| 88 | + newSelectedApplicability === APPLICABILITY.EQUIPMENT.id |
| 89 | + ) { |
| 90 | + // only one limit set with this name exist => their applicability has to be different |
| 91 | + return 'LimitSetApplicabilityError'; |
| 92 | + } |
| 93 | + } |
| 94 | + return ''; |
| 95 | + }, |
| 96 | + [indexSelectedLimitSet, limitsGroups] |
| 97 | + ); |
| 98 | + |
| 99 | + return ( |
| 100 | + <> |
| 101 | + {/* active limit sets */} |
| 102 | + <GridSection title="SelectedOperationalLimitGroups" /> |
| 103 | + <Grid container item xs={8} columns={10.25} spacing={0}> |
| 104 | + <Grid item xs={3}> |
| 105 | + <SelectedOperationalLimitGroup |
| 106 | + selectedFormName={`${id}.${SELECTED_LIMITS_GROUP_1}`} |
| 107 | + optionsFormName={`${id}.${OPERATIONAL_LIMITS_GROUPS}`} |
| 108 | + label="Side1" |
| 109 | + filteredApplicability={APPLICABILITY.SIDE1.id} |
| 110 | + /> |
| 111 | + </Grid> |
| 112 | + <Grid item xs={3}> |
| 113 | + <SelectedOperationalLimitGroup |
| 114 | + selectedFormName={`${id}.${SELECTED_LIMITS_GROUP_2}`} |
| 115 | + optionsFormName={`${id}.${OPERATIONAL_LIMITS_GROUPS}`} |
| 116 | + label="Side2" |
| 117 | + filteredApplicability={APPLICABILITY.SIDE2.id} |
| 118 | + /> |
| 119 | + </Grid> |
| 120 | + </Grid> |
| 121 | + |
| 122 | + {/* limits */} |
| 123 | + <Grid container item xs={4.9} display="flex" flexDirection="row"> |
| 124 | + <Grid container item xs={3}> |
| 125 | + <GridSection title="LimitSets" /> |
| 126 | + </Grid> |
| 127 | + <Grid container item xs={0.5}> |
| 128 | + <Button sx={styles.button} startIcon={<AddIcon onClick={onAddClick} />} /> |
| 129 | + </Grid> |
| 130 | + </Grid> |
| 131 | + <Grid container item xs={12} columns={10.25}> |
| 132 | + <Grid item xs={4}> |
| 133 | + <OperationalLimitsGroupsTabs |
| 134 | + ref={myRef} |
| 135 | + parentFormName={id} |
| 136 | + limitsGroups={limitsGroups} |
| 137 | + indexSelectedLimitSet={indexSelectedLimitSet} |
| 138 | + setIndexSelectedLimitSet={setIndexSelectedLimitSet} |
| 139 | + checkLimitSetUnicity={checkLimitSetUnicity} |
| 140 | + /> |
| 141 | + </Grid> |
| 142 | + <Grid item xs={6} sx={tabStyles.parametersBox} marginLeft={2}> |
| 143 | + {indexSelectedLimitSet !== null && |
| 144 | + limitsGroups.map( |
| 145 | + (operationalLimitsGroup: OperationalLimitsGroup, index: number) => |
| 146 | + index === indexSelectedLimitSet && ( |
| 147 | + <LimitsSidePane |
| 148 | + key={operationalLimitsGroup.id} |
| 149 | + limitsGroupFormName={`${id}.${OPERATIONAL_LIMITS_GROUPS}[${index}].${CURRENT_LIMITS}`} |
| 150 | + limitsGroupApplicabilityName={`${id}.${OPERATIONAL_LIMITS_GROUPS}[${index}]`} |
| 151 | + clearableFields={clearableFields} |
| 152 | + permanentCurrentLimitPreviousValue={ |
| 153 | + getCurrentLimits(equipmentToModify)?.permanentLimit |
| 154 | + } |
| 155 | + temporaryLimitsPreviousValues={ |
| 156 | + getCurrentLimits(equipmentToModify)?.temporaryLimits ?? [] |
| 157 | + } |
| 158 | + currentNode={currentNode} |
| 159 | + onlySelectedLimitsGroup={false} |
| 160 | + selectedLimitSetName={operationalLimitsGroup.name} |
| 161 | + checkLimitSetUnicity={checkLimitSetUnicity} |
| 162 | + /> |
| 163 | + ) |
| 164 | + )} |
| 165 | + </Grid> |
| 166 | + </Grid> |
| 167 | + </> |
| 168 | + ); |
| 169 | +} |
0 commit comments