@@ -19,31 +19,30 @@ import {
19
19
} from 'graphql/utilities' ;
20
20
import * as React from 'react' ;
21
21
22
- import { defaultPresetName , PRESETS } from './presets' ;
23
-
24
22
enum InputType {
25
23
Presets = 'Presets' ,
26
24
SDL = 'SDL' ,
27
25
Introspection = 'Introspection' ,
28
26
}
29
27
30
- const initialConfig = {
31
- inputType : InputType . Presets ,
32
- activePreset : defaultPresetName ,
33
- sdlText : '' ,
34
- jsonText : '' ,
35
- } ;
36
-
37
28
interface IntrospectionModalProps {
38
29
open : boolean ;
30
+ presets ?: { [ name : string ] : any } ;
39
31
onClose : ( ) => void ;
40
32
onChange : ( introspection : any ) => void ;
41
33
}
42
34
43
35
export function IntrospectionModal ( props : IntrospectionModalProps ) {
44
- const { open, onChange, onClose } = props ;
36
+ const { open, presets, onChange, onClose } = props ;
37
+ const presetNames = presets != null ? Object . keys ( presets ) : [ ] ;
38
+ const hasPresets = presetNames . length > 0 ;
45
39
46
- const [ submitted , setSubmitted ] = React . useState ( initialConfig ) ;
40
+ const [ submitted , setSubmitted ] = React . useState ( {
41
+ inputType : hasPresets ? InputType . Presets : InputType . SDL ,
42
+ activePreset : presetNames . at ( 0 ) ?? '' ,
43
+ sdlText : '' ,
44
+ jsonText : '' ,
45
+ } ) ;
47
46
48
47
const [ inputType , setInputType ] = React . useState ( submitted . inputType ) ;
49
48
const [ sdlText , setSDLText ] = React . useState ( submitted . sdlText ) ;
@@ -65,19 +64,24 @@ export function IntrospectionModal(props: IntrospectionModalProps) {
65
64
textColor = "primary"
66
65
onChange = { ( _ , activeTab ) => setInputType ( activeTab ) }
67
66
>
68
- < Tab value = { InputType . Presets } label = { InputType . Presets } />
67
+ { hasPresets && (
68
+ < Tab value = { InputType . Presets } label = { InputType . Presets } />
69
+ ) }
69
70
< Tab value = { InputType . SDL } label = { InputType . SDL } />
70
71
< Tab
71
72
value = { InputType . Introspection }
72
73
label = { InputType . Introspection }
73
74
/>
74
75
</ TabList >
75
- < TabPanel value = { InputType . Presets } >
76
- < PresetsTab
77
- activePreset = { activePreset }
78
- onPresetChange = { setActivePreset }
79
- />
80
- </ TabPanel >
76
+ { hasPresets && (
77
+ < TabPanel value = { InputType . Presets } >
78
+ < PresetsTab
79
+ presets = { presets }
80
+ activePreset = { activePreset }
81
+ onPresetChange = { setActivePreset }
82
+ />
83
+ </ TabPanel >
84
+ ) }
81
85
< TabPanel value = { InputType . SDL } >
82
86
< SDLTab sdlText = { sdlText } onSDLTextChange = { setSDLText } />
83
87
</ TabPanel >
@@ -102,7 +106,7 @@ export function IntrospectionModal(props: IntrospectionModalProps) {
102
106
function handleSubmit ( ) {
103
107
switch ( inputType ) {
104
108
case InputType . Presets :
105
- onChange ( PRESETS [ activePreset ] ) ;
109
+ onChange ( presets [ activePreset ] ) ;
106
110
break ;
107
111
case InputType . Introspection :
108
112
onChange ( JSON . parse ( jsonText ) ) ;
@@ -167,13 +171,14 @@ function IntrospectionDialog(props: IntrospectionDialogProps) {
167
171
}
168
172
169
173
interface PresetsTabProps {
174
+ presets : { [ name : string ] : any } ;
170
175
activePreset : string ;
171
176
onPresetChange : ( presetName : string ) => void ;
172
177
}
173
178
174
179
function PresetsTab ( props : PresetsTabProps ) {
175
- const { activePreset, onPresetChange } = props ;
176
- const presetNames = Object . keys ( PRESETS ) ;
180
+ const { presets , activePreset, onPresetChange } = props ;
181
+ const presetNames = Object . keys ( presets ) ;
177
182
178
183
return (
179
184
< Grid container spacing = { 4 } >
0 commit comments