@@ -42,6 +42,46 @@ export type MaaSTierValue = {
4242 selectedTierNames ?: string [ ] ; // if tiersDropdownSelection is 'specify-tiers', this is the list of selected tier names
4343} ;
4444
45+ //// Dropdown options ////
46+
47+ export enum TierDropdownOption {
48+ AllTiers = 'all-tiers' ,
49+ NoTiers = 'no-tiers' ,
50+ SpecifyTiers = 'specify-tiers' ,
51+ }
52+
53+ const TIER_DROPDOWN_OPTIONS : Array < { key : TierDropdownOption ; label : string } > = [
54+ { key : TierDropdownOption . AllTiers , label : 'All tiers' } ,
55+ { key : TierDropdownOption . NoTiers , label : 'No tiers' } ,
56+ { key : TierDropdownOption . SpecifyTiers , label : 'Specific tiers' } ,
57+ ] ;
58+
59+ const getTierDropdownLabel = ( key : TierDropdownOption ) : string => {
60+ const option = TIER_DROPDOWN_OPTIONS . find ( ( opt ) => opt . key === key ) ;
61+ return option ?. label ?? 'All tiers' ;
62+ } ;
63+
64+ const getMaaSTierLabel = (
65+ value : MaaSTierValue ,
66+ externalData ?: { data : MaaSEndpointsExternalData } ,
67+ ) : string => {
68+ const availableTiers = externalData ?. data . tiers ?? [ ] ;
69+ const selection = value . tiersDropdownSelection ?? TierDropdownOption . AllTiers ;
70+ if ( selection === TierDropdownOption . NoTiers ) {
71+ return getTierDropdownLabel ( TierDropdownOption . NoTiers ) ;
72+ }
73+ if ( selection === TierDropdownOption . SpecifyTiers ) {
74+ // Matching the display name of the selected tiers
75+ return (
76+ availableTiers
77+ . filter ( ( tier : Tier ) => value . selectedTierNames ?. includes ( tier . name ?? '' ) )
78+ . map ( ( tier : Tier ) => tier . displayName ?? tier . name ?? '' )
79+ . join ( ', ' ) || 'Tiers selected'
80+ ) ;
81+ }
82+ return getTierDropdownLabel ( TierDropdownOption . AllTiers ) ;
83+ } ;
84+
4585//// Zod Validation Schema ////
4686
4787/**
@@ -53,7 +93,7 @@ export type MaaSTierValue = {
5393export const maasEndpointsFieldSchema = z
5494 . object ( {
5595 isChecked : z . boolean ( ) ,
56- tiersDropdownSelection : z . enum ( [ 'all-tiers' , 'no-tiers' , 'specify-tiers' ] ) . optional ( ) ,
96+ tiersDropdownSelection : z . nativeEnum ( TierDropdownOption ) . optional ( ) ,
5797 selectedTierNames : z . array ( z . string ( ) ) . optional ( ) ,
5898 } )
5999 . refine (
@@ -97,41 +137,6 @@ const useMaaSEndpointsExternalData: MaaSEndpointsFieldType['externalDataHook'] =
97137 [ tiers , hasViewTiersPermission , loaded , error ] ,
98138 ) ;
99139} ;
100- //// Dropdown options ////
101-
102- type TierDropdownOption = 'all-tiers' | 'no-tiers' | 'specify-tiers' ;
103-
104- const TIER_DROPDOWN_OPTIONS : Array < { key : TierDropdownOption ; label : string } > = [
105- { key : 'all-tiers' , label : 'All tiers' } ,
106- { key : 'no-tiers' , label : 'No tiers' } ,
107- { key : 'specify-tiers' , label : 'Specific tiers' } ,
108- ] ;
109-
110- const getTierDropdownLabel = ( key : TierDropdownOption ) : string => {
111- const option = TIER_DROPDOWN_OPTIONS . find ( ( opt ) => opt . key === key ) ;
112- return option ?. label ?? 'All tiers' ;
113- } ;
114-
115- const getMaaSTierLabel = (
116- value : MaaSTierValue ,
117- externalData ?: { data : MaaSEndpointsExternalData ; loaded : boolean ; loadError ?: Error } ,
118- ) : string => {
119- const availableTiers = externalData ?. data . tiers ?? [ ] ;
120- const selection = value . tiersDropdownSelection ?? 'all-tiers' ;
121- if ( selection === 'no-tiers' ) {
122- return 'No tiers' ;
123- }
124- if ( selection === 'specify-tiers' ) {
125- // Matching the display name of the selected tiers
126- return (
127- availableTiers
128- . filter ( ( tier : Tier ) => value . selectedTierNames ?. includes ( tier . name ?? '' ) )
129- . map ( ( tier : Tier ) => tier . displayName ?? tier . name ?? '' )
130- . join ( ', ' ) || 'Tiers selected'
131- ) ;
132- }
133- return 'All tiers' ;
134- } ;
135140
136141//// Component ////
137142
@@ -175,7 +180,7 @@ const MaasEndpointField: React.FC<MaasEndpointFieldProps> = ({
175180 if ( checked ) {
176181 onChange ( {
177182 isChecked : true ,
178- tiersDropdownSelection : 'all-tiers' ,
183+ tiersDropdownSelection : TierDropdownOption . AllTiers ,
179184 selectedTierNames : [ ] ,
180185 } ) ;
181186 } else {
@@ -266,7 +271,7 @@ const MaasEndpointField: React.FC<MaasEndpointFieldProps> = ({
266271 placeholder = "Select tier access"
267272 value = { value . tiersDropdownSelection ?? 'all-tiers' }
268273 toggleLabel = { getTierDropdownLabel (
269- value . tiersDropdownSelection ?? 'all-tiers' ,
274+ value . tiersDropdownSelection ?? TierDropdownOption . AllTiers ,
270275 ) }
271276 onChange = { handleDropdownChange }
272277 popperProps = { { appendTo : 'inline' } }
@@ -346,8 +351,6 @@ export const MaaSEndpointFieldWizardField: MaaSEndpointsFieldType = {
346351 value : ( ) =>
347352 getMaaSTierLabel ( value , {
348353 data : externalData ?? { tiers : [ ] , hasViewTiersPermission : false } ,
349- loaded : true ,
350- loadError : undefined ,
351354 } ) ,
352355 optional : true ,
353356 isVisible : ( ) => value . isChecked ,
0 commit comments