6
6
*/
7
7
import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
8
8
import {
9
+ ALL_BUS_BAR_SECTIONS ,
9
10
BUS_BAR_INDEX ,
10
11
BUSBAR_SECTION_ID ,
11
12
IS_AFTER_BUSBAR_SECTION_ID ,
@@ -29,23 +30,45 @@ import { useFormContext, useWatch } from 'react-hook-form';
29
30
import { BusBarSectionInfos } from './voltage-level-section.type' ;
30
31
import { areIdsEqual , getObjectId } from '../../../../utils/utils' ;
31
32
33
+ const getArrayPosition = ( data : BusBarSectionInfos [ ] , selectedOptionId : string ) => {
34
+ if ( ! selectedOptionId || ! data ) {
35
+ return { position : - 1 , length : 0 } ;
36
+ }
37
+
38
+ for ( const array of Object . values ( data ) ) {
39
+ if ( Array . isArray ( array ) ) {
40
+ const position = array . indexOf ( selectedOptionId ) ;
41
+ if ( position !== - 1 ) {
42
+ return { position, length : array . length } ;
43
+ }
44
+ }
45
+ }
46
+ return { position : - 1 , length : 0 } ;
47
+ } ;
48
+
49
+ type OptionWithDisabled = Option & { disabled ?: boolean } ;
50
+
32
51
interface VoltageLevelSectionsCreationFormProps {
33
52
busBarSectionInfos ?: BusBarSectionInfos [ ] ;
34
53
voltageLevelId : string ;
54
+ allBusbarSectionsList : string [ ] ;
35
55
studyUuid : UUID ;
36
56
currentNode : CurrentTreeNode ;
37
57
currentRootNetworkUuid : UUID ;
38
58
isUpdate ?: boolean ;
59
+ isSymmetricalNbBusBarSections : boolean ;
39
60
isNotFoundOrNotSupported : boolean ;
40
61
}
41
62
42
63
export function CreateVoltageLevelSectionForm ( {
43
64
busBarSectionInfos,
44
65
voltageLevelId,
66
+ allBusbarSectionsList,
45
67
studyUuid,
46
68
currentNode,
47
69
currentRootNetworkUuid,
48
70
isUpdate,
71
+ isSymmetricalNbBusBarSections,
49
72
isNotFoundOrNotSupported,
50
73
} : Readonly < VoltageLevelSectionsCreationFormProps > ) {
51
74
const intl = useIntl ( ) ;
@@ -74,6 +97,21 @@ export function CreateVoltageLevelSectionForm({
74
97
useEffect ( ( ) => {
75
98
if ( busBarSectionInfos && sectionCount ) {
76
99
const selectedKey = sectionCount ?. id ;
100
+ if ( selectedKey === 'all' ) {
101
+ setValue ( ALL_BUS_BAR_SECTIONS , true ) ;
102
+ if ( allBusbarSectionsList && Array . isArray ( allBusbarSectionsList ) ) {
103
+ const options = allBusbarSectionsList
104
+ . filter ( ( id ) : id is string => Boolean ( id ) )
105
+ . map ( ( id ) => ( {
106
+ id : id ,
107
+ label : id ,
108
+ } ) ) ;
109
+ setBusBarSectionsIdOptions ( options ) ;
110
+ } else {
111
+ setBusBarSectionsIdOptions ( [ ] ) ;
112
+ }
113
+ return ;
114
+ }
77
115
const sections = busBarSectionInfos [ selectedKey ] ;
78
116
if ( ! sections || ! Array . isArray ( sections ) ) {
79
117
setBusBarSectionsIdOptions ( [ ] ) ;
@@ -89,13 +127,17 @@ export function CreateVoltageLevelSectionForm({
89
127
} else {
90
128
setBusBarSectionsIdOptions ( [ ] ) ;
91
129
}
92
- } , [ busBarSectionInfos , sectionCount ] ) ;
130
+ } , [ allBusbarSectionsList , busBarSectionInfos , intl , sectionCount , setValue ] ) ;
131
+
132
+ const arrayPosition = useMemo (
133
+ ( ) => busBarSectionInfos && getArrayPosition ( busBarSectionInfos , selectedOption ?. id ) ,
134
+ [ busBarSectionInfos , selectedOption ?. id ]
135
+ ) ;
93
136
94
137
useEffect ( ( ) => {
95
- if ( selectedOption && selectedPositionOption ) {
96
- const selectedSectionIndex = busBarSectionsIdOptions . findIndex ( ( option : Option ) =>
97
- areIdsEqual ( option , selectedOption )
98
- ) ;
138
+ if ( selectedOption && selectedPositionOption && busBarSectionInfos && arrayPosition ) {
139
+ const selectedSectionIndex = arrayPosition . position ;
140
+ const busBarSections = arrayPosition . length - 1 ;
99
141
if ( selectedSectionIndex === 0 && selectedPositionOption === POSITION_NEW_SECTION_SIDE . BEFORE . id ) {
100
142
setValue ( SWITCH_BEFORE_NOT_REQUIRED , true ) ;
101
143
setIsNotRequiredSwitchBefore ( true ) ;
@@ -104,7 +146,7 @@ export function CreateVoltageLevelSectionForm({
104
146
setIsNotRequiredSwitchBefore ( false ) ;
105
147
}
106
148
if (
107
- busBarSectionsIdOptions ?. length - 1 === selectedSectionIndex &&
149
+ busBarSections === selectedSectionIndex &&
108
150
selectedPositionOption === POSITION_NEW_SECTION_SIDE . AFTER . id
109
151
) {
110
152
setValue ( SWITCH_AFTER_NOT_REQUIRED , true ) ;
@@ -115,26 +157,38 @@ export function CreateVoltageLevelSectionForm({
115
157
}
116
158
}
117
159
if ( isUpdate && isNodeBuilt ( currentNode ) ) {
118
- selectedPositionOption === POSITION_NEW_SECTION_SIDE . AFTER . id
119
- ? setValue ( SWITCH_AFTER_NOT_REQUIRED , false )
120
- : setValue ( SWITCH_BEFORE_NOT_REQUIRED , false ) ;
160
+ setValue ( SWITCH_AFTER_NOT_REQUIRED , true ) ;
161
+ setValue ( SWITCH_BEFORE_NOT_REQUIRED , true ) ;
121
162
}
122
- } , [ selectedOption , busBarSectionsIdOptions , setValue , selectedPositionOption , isUpdate , currentNode ] ) ;
163
+ } , [ selectedOption , setValue , busBarSectionInfos , selectedPositionOption , arrayPosition , isUpdate , currentNode ] ) ;
123
164
124
- const busBarIndexOptions = useMemo ( ( ) => {
165
+ const busBarIndexOptions = useMemo ( ( ) : OptionWithDisabled [ ] => {
125
166
if ( busBarSectionInfos ) {
126
- return Object . keys ( busBarSectionInfos || { } )
167
+ const sortedOptions = Object . keys ( busBarSectionInfos || { } )
127
168
. sort ( ( a , b ) => parseInt ( a ) - parseInt ( b ) )
128
169
. map ( ( key ) => ( {
129
170
id : key ,
130
171
label : key ,
131
172
} ) ) ;
173
+ const allOption = {
174
+ id : 'all' ,
175
+ label : intl . formatMessage ( { id : 'allBusbarSections' } ) ,
176
+ disabled : ! isSymmetricalNbBusBarSections ,
177
+ } as Option & { disabled ?: boolean } ;
178
+
179
+ return [ ...sortedOptions , allOption ] ;
132
180
}
133
181
return [ ] ;
134
- } , [ busBarSectionInfos ] ) ;
182
+ } , [ busBarSectionInfos , intl , isSymmetricalNbBusBarSections ] ) ;
135
183
136
- const getOptionLabel = ( object : string | { id : string | number } ) => {
137
- return typeof object === 'string' ? object : String ( object ?. id ?? '' ) ;
184
+ const getOptionLabel = ( object : string | { id : string | number ; label : string | number } ) => {
185
+ if ( typeof object === 'string' ) {
186
+ return object ;
187
+ }
188
+ if ( object ?. id === 'all' ) {
189
+ return intl . formatMessage ( { id : 'allBusbarSections' } ) ?? '' ;
190
+ }
191
+ return String ( object ?. id ?? '' ) ;
138
192
} ;
139
193
140
194
const isOptionEqualToValue = ( val1 : Option , val2 : Option ) => {
@@ -154,9 +208,32 @@ export function CreateVoltageLevelSectionForm({
154
208
name = { BUS_BAR_INDEX }
155
209
label = "Busbar"
156
210
onChangeCallback = { handleChangeBusbarIndex }
157
- options = { busBarIndexOptions }
211
+ options = { busBarIndexOptions as Option [ ] }
158
212
getOptionLabel = { getOptionLabel }
159
213
isOptionEqualToValue = { isOptionEqualToValue }
214
+ renderOption = { ( props , option ) => {
215
+ const allOptionsDisabled = ( option as any ) . id === 'all' && ( option as any ) ?. disabled ;
216
+ const { key, ...otherProps } = props ;
217
+ return (
218
+ < li key = { key } { ...otherProps } >
219
+ < div >
220
+ < div > { getOptionLabel ( option ) } </ div >
221
+ { allOptionsDisabled && (
222
+ < div
223
+ style = { {
224
+ fontSize : '0.85rem' ,
225
+ color : 'red' ,
226
+ marginTop : '2px' ,
227
+ } }
228
+ >
229
+ { intl . formatMessage ( { id : 'allOptionHelperText' } ) }
230
+ </ div >
231
+ ) }
232
+ </ div >
233
+ </ li >
234
+ ) ;
235
+ } }
236
+ getOptionDisabled = { ( option ) => ( option as any ) ?. disabled }
160
237
size = { 'small' }
161
238
disabled = { isNotFoundOrNotSupported }
162
239
/>
@@ -165,8 +242,8 @@ export function CreateVoltageLevelSectionForm({
165
242
const busbarSectionsField = (
166
243
< AutocompleteInput
167
244
name = { BUSBAR_SECTION_ID }
168
- label = "BusBarSections "
169
- options = { Object . values ( busBarSectionsIdOptions ) }
245
+ label = "BusBarSectionsReference "
246
+ options = { busBarSectionsIdOptions }
170
247
getOptionLabel = { getObjectId }
171
248
isOptionEqualToValue = { areIdsEqual }
172
249
size = { 'small' }
0 commit comments