1
- import { VariableObjectWithKey } from 'shim/objects/variables' ;
2
1
import { useEffect , useState } from 'react' ;
3
2
import {
4
3
Sidebar ,
@@ -7,19 +6,23 @@ import {
7
6
SidebarProvider ,
8
7
} from '@/components/ui/sidebar' ;
9
8
import { EnvironmentMap } from 'shim/objects/environment' ;
10
- import { variableMapToArray } from '@/components/shared/settings/variableTabs/helper/EditVariableHelper' ;
9
+ import {
10
+ variableArrayToMap ,
11
+ variableMapToArray ,
12
+ } from '@/components/shared/settings/variableTabs/helper/EditVariableHelper' ;
11
13
import { VariableEditor } from '@/components/shared/settings/variableTabs/table/VariableEditor' ;
14
+ import { VariableObjectWithKey } from '../../../../../shim/objects/variables' ;
12
15
13
16
export interface EnvironmentVariableEditorProps {
14
17
environments : EnvironmentMap ;
15
18
onValidChange ?: ( valid : boolean ) => void ;
16
- onVariablesChange ?: ( variables : VariableObjectWithKey [ ] ) => void ;
19
+ onEnvironmentChange ?: ( variables : EnvironmentMap ) => void ;
17
20
}
18
21
19
22
/** A component that allows adding, editing and removing variables */
20
23
export const EnvironmentVariableTab = ( {
21
24
environments,
22
- onVariablesChange ,
25
+ onEnvironmentChange ,
23
26
onValidChange,
24
27
} : EnvironmentVariableEditorProps ) => {
25
28
const environmentList = Object . keys ( environments ) ;
@@ -30,6 +33,29 @@ export const EnvironmentVariableTab = ({
30
33
) ;
31
34
const [ selectedEnvironment , setSelectedEnvironment ] = useState ( environmentList [ 0 ] ) ;
32
35
36
+ const onVariableChange = ( variables : VariableObjectWithKey [ ] ) => {
37
+ const selectedEnvironmentKey = selectedEnvironment as keyof EnvironmentMap ;
38
+ const variableMap = variableArrayToMap ( variables ) ;
39
+ const updatedEnvironment = {
40
+ ...environments ,
41
+ [ selectedEnvironmentKey ] : variableMap ,
42
+ } as EnvironmentMap ;
43
+ console . log ( 'onVariableChange' , updatedEnvironment ) ;
44
+ setEditorEnvironmentVariables ( variables ) ;
45
+ onEnvironmentChange ( updatedEnvironment ) ;
46
+ } ;
47
+
48
+ const newEnvironment = ( ) => {
49
+ console . log ( 'newEnvironment' ) ;
50
+ const newEnvironmentName = ( Math . random ( ) + 1 ) . toString ( 36 ) . substring ( 7 ) ;
51
+ const updatedEnvironment = {
52
+ ...environments ,
53
+ [ newEnvironmentName ] : { } ,
54
+ } as EnvironmentMap ;
55
+ onEnvironmentChange ( updatedEnvironment ) ;
56
+ setSelectedEnvironment ( newEnvironmentName ) ;
57
+ } ;
58
+
33
59
useEffect ( ( ) => {
34
60
const selectedEnvironmentKey = selectedEnvironment as keyof EnvironmentMap ;
35
61
const selectedEnvironmentVariables = environments [ selectedEnvironmentKey ] ?? { } ;
@@ -51,12 +77,12 @@ export const EnvironmentVariableTab = ({
51
77
{ environment }
52
78
</ SidebarMenuButton >
53
79
) ) }
54
- < SidebarMenuButton > + Add Environment</ SidebarMenuButton >
80
+ < SidebarMenuButton onClick = { newEnvironment } > + Add Environment</ SidebarMenuButton >
55
81
</ Sidebar >
56
82
< VariableEditor
57
83
className = { 'p-4 m-1 flex-1' }
58
84
variables = { editorEnvironmentVariables }
59
- onVariablesChange = { onVariablesChange }
85
+ onVariablesChange = { onVariableChange }
60
86
onValidChange = { onValidChange }
61
87
/>
62
88
</ SidebarProvider >
0 commit comments