1
- import React , { useCallback , useEffect , useState } from 'react'
1
+ import React , { useCallback , useContext , useEffect , useState } from 'react'
2
2
import {
3
3
EuiButton ,
4
4
EuiIcon ,
@@ -10,49 +10,75 @@ import {
10
10
EuiTextColor ,
11
11
EuiTitle ,
12
12
} from '@elastic/eui'
13
- import { toNumber , filter } from 'lodash'
13
+ import { toNumber , filter , get , find , first } from 'lodash'
14
14
import { useDispatch , useSelector } from 'react-redux'
15
15
import cx from 'classnames'
16
16
17
17
import {
18
18
createFreeDbJob ,
19
19
oauthCloudPlanSelector ,
20
+ oauthCloudSelector ,
20
21
setIsOpenSelectPlanDialog ,
22
+ setSocialDialogState ,
21
23
} from 'uiSrc/slices/oauth/cloud'
22
24
import { TelemetryEvent , sendEventTelemetry } from 'uiSrc/telemetry'
23
25
import { addInfiniteNotification } from 'uiSrc/slices/app/notifications'
24
26
import { INFINITE_MESSAGES } from 'uiSrc/components/notifications/components'
25
27
import { CloudJobStep } from 'uiSrc/electron/constants'
26
-
28
+ import { appFeatureFlagsFeaturesSelector } from 'uiSrc/slices/app/features'
29
+ import { FeatureFlags , Theme } from 'uiSrc/constants'
30
+ import { OAuthSocialSource , TFRegion } from 'uiSrc/slices/interfaces'
31
+ import { ThemeContext } from 'uiSrc/contexts/themeContext'
32
+ import TriggeredFunctionsDarkSVG from 'uiSrc/assets/img/sidebar/gears.svg'
33
+ import TriggeredFunctionsLightSVG from 'uiSrc/assets/img/sidebar/gears_active.svg'
34
+
35
+ import { CloudSubscriptionPlanResponse } from 'apiSrc/modules/cloud/subscription/dto'
27
36
import { OAuthProvider , OAuthProviders } from './constants'
28
37
import styles from './styles.module.scss'
29
38
30
39
export const DEFAULT_REGION = 'us-east-1'
31
40
export const DEFAULT_PROVIDER = OAuthProvider . AWS
41
+ const getTFProviderRegions = ( regions : TFRegion [ ] , provider : OAuthProvider ) =>
42
+ ( find ( regions , { provider } ) || { } ) . regions || [ ]
32
43
33
44
const OAuthSelectPlan = ( ) => {
45
+ const { theme } = useContext ( ThemeContext )
34
46
const { isOpenDialog, data : plansInit = [ ] , loading } = useSelector ( oauthCloudPlanSelector )
47
+ const { source } = useSelector ( oauthCloudSelector )
48
+ const { [ FeatureFlags . cloudSso ] : cloudSsoFeature = { } } = useSelector ( appFeatureFlagsFeaturesSelector )
49
+
50
+ const tfRegions : TFRegion [ ] = get ( cloudSsoFeature , 'data.selectPlan.components.triggersAndFunctions' , [ ] )
35
51
36
52
const [ plans , setPlans ] = useState ( plansInit || [ ] )
37
53
const [ planIdSelected , setPlanIdSelected ] = useState ( '' )
38
54
const [ providerSelected , setProviderSelected ] = useState < OAuthProvider > ( DEFAULT_PROVIDER )
55
+ const [ tfProviderRegions , setTfProviderRegions ] = useState ( getTFProviderRegions ( tfRegions , providerSelected ) )
39
56
40
57
const dispatch = useDispatch ( )
41
58
59
+ const isTFSource = source ?. endsWith ( OAuthSocialSource . TriggersAndFunctions )
60
+
61
+ useEffect ( ( ) => {
62
+ setTfProviderRegions ( getTFProviderRegions ( tfRegions , providerSelected ) )
63
+ } , [ providerSelected ] )
64
+
42
65
useEffect ( ( ) => {
43
66
if ( ! plansInit . length ) {
44
67
return
45
68
}
46
69
47
- const newPlans = filter ( plansInit , { provider : providerSelected } )
48
- . sort ( ( a , b ) => a ?. details ?. displayOrder - b ?. details ?. displayOrder )
49
- const planId = newPlans ?. find ( ( { region } ) => region === DEFAULT_REGION ) ?. id ?. toString ( )
50
- || newPlans [ 0 ] ?. id ?. toString ( )
51
- || ''
70
+ const defaultRegion = isTFSource ? first ( tfProviderRegions ) || DEFAULT_REGION : DEFAULT_REGION
71
+
72
+ const filteredPlans = filter ( plansInit , { provider : providerSelected } )
73
+ . sort ( ( a , b ) => ( a ?. details ?. displayOrder || 0 ) - ( b ?. details ?. displayOrder || 0 ) )
74
+
75
+ const defaultPlan = filteredPlans . find ( ( { region } ) => region === defaultRegion )
52
76
53
- setPlans ( newPlans )
77
+ const planId = ( defaultPlan || first ( filteredPlans ) || { } ) . id ?. toString ( ) || ''
78
+
79
+ setPlans ( filteredPlans )
54
80
setPlanIdSelected ( planId )
55
- } , [ plansInit , providerSelected ] )
81
+ } , [ isTFSource , plansInit , providerSelected , tfProviderRegions ] )
56
82
57
83
const handleOnClose = useCallback ( ( ) => {
58
84
sendEventTelemetry ( {
@@ -61,32 +87,45 @@ const OAuthSelectPlan = () => {
61
87
setPlanIdSelected ( '' )
62
88
setProviderSelected ( DEFAULT_PROVIDER )
63
89
dispatch ( setIsOpenSelectPlanDialog ( false ) )
90
+ dispatch ( setSocialDialogState ( null ) )
64
91
} , [ ] )
65
92
66
93
if ( ! isOpenDialog ) return null
67
94
95
+ const getOptionDisplay = ( item : CloudSubscriptionPlanResponse ) => {
96
+ const { region = '' , details : { countryName = '' , cityName = '' } , provider } = item
97
+ const tfProviderRegions : string [ ] = find ( tfRegions , { provider } ) ?. regions || [ ]
98
+
99
+ return (
100
+ < EuiText color = "subdued" size = "s" >
101
+ { `${ countryName } (${ cityName } )` }
102
+ < EuiTextColor className = { styles . regionName } > { region } </ EuiTextColor >
103
+ { tfProviderRegions ?. includes ( region ) && (
104
+ < EuiIcon
105
+ type = { theme === Theme . Dark ? TriggeredFunctionsDarkSVG : TriggeredFunctionsLightSVG }
106
+ className = { styles . tfOptionIcon }
107
+ data-testid = { `tf-icon-${ region } ` }
108
+ />
109
+ ) }
110
+ </ EuiText >
111
+ )
112
+ }
113
+
68
114
const regionOptions : EuiSuperSelectOption < string > [ ] = plans . map (
69
115
( item ) => {
70
- const { id, region, details : { countryName = '' , cityName = '' } } = item
116
+ const { id, region = '' } = item
71
117
return {
72
118
value : `${ id } ` ,
73
- inputDisplay : (
74
- < EuiText color = "subdued" size = "s" >
75
- { `${ countryName } (${ cityName } )` }
76
- < EuiTextColor className = { styles . regionName } > { region } </ EuiTextColor >
77
- </ EuiText >
78
- ) ,
79
- dropdownDisplay : (
80
- < EuiText color = "subdued" size = "s" >
81
- { `${ countryName } (${ cityName } )` }
82
- < EuiTextColor className = { styles . regionName } > { region } </ EuiTextColor >
83
- </ EuiText >
84
- ) ,
119
+ inputDisplay : getOptionDisplay ( item ) ,
120
+ dropdownDisplay : getOptionDisplay ( item ) ,
85
121
'data-test-subj' : `oauth-region-${ region } ` ,
86
122
}
87
123
}
88
124
)
89
125
126
+ const isVendorWithTFRegions = ! ! regionOptions . length
127
+ && ! plans . some ( ( { region = '' } ) => tfProviderRegions ?. includes ( region ) )
128
+
90
129
const onChangeRegion = ( region : string ) => {
91
130
setPlanIdSelected ( region )
92
131
}
@@ -110,7 +149,7 @@ const OAuthSelectPlan = () => {
110
149
< h2 className = { styles . title } > Select cloud vendor</ h2 >
111
150
</ EuiTitle >
112
151
< section className = { styles . providers } >
113
- { OAuthProviders . map ( ( { icon, id, label } ) => (
152
+ { OAuthProviders . map ( ( { icon, id, label } ) => (
114
153
< div className = { styles . provider } >
115
154
{ id === providerSelected
116
155
&& < div className = { cx ( styles . providerActiveIcon ) } > < EuiIcon type = "check" /> </ div > }
@@ -121,7 +160,7 @@ const OAuthSelectPlan = () => {
121
160
/>
122
161
< EuiText className = { styles . providerLabel } > { label } </ EuiText >
123
162
</ div >
124
- ) ) }
163
+ ) ) }
125
164
</ section >
126
165
< section className = { styles . region } >
127
166
< EuiText className = { styles . regionLabel } > Region</ EuiText >
@@ -136,6 +175,16 @@ const OAuthSelectPlan = () => {
136
175
onChange = { onChangeRegion }
137
176
data-testid = "select-oauth-region"
138
177
/>
178
+ { isVendorWithTFRegions && (
179
+ < EuiText className = { styles . selectDescription } data-testid = "select-region-select-description" >
180
+ This vendor does not support triggers and functions capability.
181
+ </ EuiText >
182
+ ) }
183
+ { ! regionOptions . length && (
184
+ < EuiText className = { styles . selectDescription } data-testid = "select-region-select-description" >
185
+ No regions available, try another vendor.
186
+ </ EuiText >
187
+ ) }
139
188
</ section >
140
189
< footer className = { styles . footer } >
141
190
< EuiButton
@@ -156,7 +205,7 @@ const OAuthSelectPlan = () => {
156
205
data-testid = "submit-oauth-select-plan-dialog"
157
206
aria-labelledby = "submit oauth select plan dialog"
158
207
>
159
- Create subscription
208
+ Create database
160
209
</ EuiButton >
161
210
</ footer >
162
211
</ section >
0 commit comments