1- import React , { FC , useState , useContext } from 'react'
1+ import React , { FC , useCallback , useState , useContext } from 'react'
22import { useHistory } from 'react-router-dom'
33import { useSelector } from 'react-redux'
44
55// Components
66import {
77 ComponentColor ,
88 DraggableResizer ,
9+ Dropdown ,
910 FlexBox ,
1011 FlexDirection ,
1112 Orientation ,
1213 Button ,
14+ Icon ,
1315 IconFont ,
1416 AlignItems ,
1517 JustifyContent ,
@@ -24,6 +26,7 @@ import {
2426 PersistanceProvider ,
2527 PersistanceContext ,
2628} from 'src/dataExplorer/context/persistance'
29+ import { LanguageType } from 'src/dataExplorer/components/resources'
2730import ResultsPane from 'src/dataExplorer/components/ResultsPane'
2831import Sidebar from 'src/dataExplorer/components/Sidebar'
2932import Schema from 'src/dataExplorer/components/Schema'
@@ -47,27 +50,43 @@ export enum OverlayType {
4750
4851const FluxQueryBuilder : FC = ( ) => {
4952 const history = useHistory ( )
50- const { hasChanged , resource , vertical, setVertical} =
53+ const { resource , hasChanged , vertical, setVertical, setHasChanged } =
5154 useContext ( PersistanceContext )
5255 const [ overlayType , setOverlayType ] = useState < OverlayType | null > ( null )
56+ const [ selectedLanguage , setSelectedLanguage ] = useState (
57+ resource ?. language ?? LanguageType . FLUX
58+ )
5359 const [ isOverlayVisible , setIsOverlayVisible ] = useState ( false )
5460 const { cancel} = useContext ( QueryContext )
5561 const { setStatus, setResult} = useContext ( ResultsContext )
5662 const org = useSelector ( getOrg )
5763
58- const handleClear = ( ) => {
64+ const handleClear = useCallback ( ( ) => {
5965 cancel ( )
6066 setStatus ( RemoteDataState . NotStarted )
6167 setResult ( null )
6268
63- history . replace ( `/orgs/${ org . id } /data-explorer/from/script` )
69+ if ( isFlagEnabled ( 'uiSqlSupport' ) ) {
70+ history . replace (
71+ `/orgs/${ org . id } /data-explorer/from/script?language=${ selectedLanguage } `
72+ )
73+ } else {
74+ history . replace ( `/orgs/${ org . id } /data-explorer/from/script` )
75+ }
6476
6577 if ( ! isFlagEnabled ( 'saveAsScript' ) ) {
6678 setIsOverlayVisible ( false )
6779 }
80+ } , [ cancel , org . id , history , setResult , setStatus , selectedLanguage ] )
81+
82+ const handleSelectDropdown = ( language : LanguageType ) => {
83+ // set the language in the state until we can confirm the selection
84+ setSelectedLanguage ( language )
85+ setHasChanged ( true )
86+ setOverlayType ( OverlayType . NEW )
6887 }
6988
70- const handleNewScript = ( ) => {
89+ const handleNewScript = useCallback ( ( ) => {
7190 if ( isFlagEnabled ( 'saveAsScript' ) ) {
7291 if ( hasChanged ) {
7392 setOverlayType ( OverlayType . NEW )
@@ -81,7 +100,7 @@ const FluxQueryBuilder: FC = () => {
81100 handleClear ( )
82101 }
83102 }
84- }
103+ } , [ handleClear , hasChanged ] )
85104
86105 const handleUserpilot = ( ) => {
87106 if ( window . userpilot ) {
@@ -95,6 +114,7 @@ const FluxQueryBuilder: FC = () => {
95114 { isFlagEnabled ( 'saveAsScript' ) ? (
96115 < Overlay visible = { overlayType !== null } >
97116 < SaveAsScript
117+ language = { selectedLanguage }
98118 type = { overlayType }
99119 setOverlayType = { setOverlayType }
100120 onClose = { ( ) => setOverlayType ( null ) }
@@ -137,13 +157,43 @@ const FluxQueryBuilder: FC = () => {
137157 direction = { FlexDirection . Row }
138158 justifyContent = { JustifyContent . SpaceBetween }
139159 >
140- < div >
141- < Button
142- onClick = { handleNewScript }
143- text = { isFlagEnabled ( 'saveAsScript' ) ? 'New Script' : 'Clear' }
144- icon = { IconFont . Plus_New }
145- testID = "flux-query-builder--new-script"
146- />
160+ < div style = { { display : 'flex' } } >
161+ { isFlagEnabled ( 'uiSqlSupport' ) ? (
162+ < Dropdown
163+ menu = { onCollapse => (
164+ < Dropdown . Menu onCollapse = { onCollapse } >
165+ { [ LanguageType . FLUX , LanguageType . SQL ] . map ( option => (
166+ < Dropdown . Item
167+ className = { `script-dropdown__${ option } ` }
168+ key = { option }
169+ onClick = { ( ) => handleSelectDropdown ( option ) }
170+ selected = { resource ?. language === option }
171+ >
172+ { option }
173+ </ Dropdown . Item >
174+ ) ) }
175+ </ Dropdown . Menu >
176+ ) }
177+ button = { ( active , onClick ) => (
178+ < Dropdown . Button active = { active } onClick = { onClick } >
179+ < >
180+ < Icon glyph = { IconFont . Plus_New } />
181+ New Script
182+ </ >
183+ </ Dropdown . Button >
184+ ) }
185+ testID = "select-option-dropdown"
186+ />
187+ ) : (
188+ < Button
189+ onClick = { handleNewScript }
190+ text = {
191+ isFlagEnabled ( 'saveAsScript' ) ? 'New Script' : 'Clear'
192+ }
193+ icon = { IconFont . Plus_New }
194+ testID = "flux-query-builder--new-script"
195+ />
196+ ) }
147197 { isFlagEnabled ( 'saveAsScript' ) && (
148198 < Button
149199 className = "flux-query-builder__action-button"
0 commit comments