@@ -18,13 +18,12 @@ import {
1818 DEFAULT_QUERY ,
1919 OutputFormat ,
2020 SUPPORTED_OUTPUT_FORMATS ,
21- TinybirdNode ,
2221 TinybirdOptions ,
2322 TinybirdParam ,
2423 TinybirdPipe ,
2524 TinybirdQuery ,
2625} from '../types' ;
27- import { capitalize , isEqual , pickBy } from 'lodash' ;
26+ import { capitalize , isEqual , pickBy , uniqWith } from 'lodash' ;
2827
2928export default function QueryEditor ( {
3029 app,
@@ -36,8 +35,6 @@ export default function QueryEditor({
3635 const styles = getStyles ( theme ) ;
3736 const [ isOptionsOpen , setIsOptionsOpen ] = useState ( false ) ;
3837 const [ pipes , setPipes ] = useState < TinybirdPipe [ ] > ( [ ] ) ;
39- const [ nodes , setNodes ] = useState < TinybirdNode [ ] > ( [ ] ) ;
40- const [ nodeIndex , setNodeIndex ] = useState ( 0 ) ;
4138 const formatAsOptions : Array < SelectableValue < OutputFormat > > = SUPPORTED_OUTPUT_FORMATS . map ( ( f ) => ( {
4239 label : capitalize ( f ) ,
4340 value : f ,
@@ -67,33 +64,33 @@ export default function QueryEditor({
6764 return ;
6865 }
6966
70- datasource . getNodes ( pipe . id ) . then ( setNodes ) . catch ( console . error ) ;
71- // eslint-disable-next-line react-hooks/exhaustive-deps
72- } , [ datasource . url , pipes , query . pipeName ] ) ;
73-
74- useEffect ( ( ) => {
75- if ( ! nodes . length ) {
76- return ;
77- }
67+ datasource
68+ . getNodes ( pipe . id )
69+ . then ( ( nodes ) => {
70+ const paramOptions = Object . fromEntries (
71+ uniqWith (
72+ nodes . reduce ( ( acc , node ) => [ ...acc , ...node . params ] , [ ] ) ,
73+ isEqual
74+ ) . map ( ( { name, ...param } ) => [ name , pickBy ( param ) ] )
75+ ) as Record < string , TinybirdParam > ;
7876
79- const paramOptions = Object . fromEntries (
80- nodes [ nodeIndex ] . params . map ( ( { name, ...param } : any ) => [ name , pickBy ( param ) ] )
81- ) ;
82- const params =
83- ! query . params || isEqual ( query . params , DEFAULT_QUERY . params )
84- ? Object . entries ( paramOptions ) . reduce (
85- ( acc , [ name , param ] ) => ( { ...acc , [ name ] : String ( ( param as TinybirdParam ) . default ?? '' ) } ) ,
86- { }
87- )
88- : query . params ;
77+ const params =
78+ ! query . params || isEqual ( query . params , DEFAULT_QUERY . params )
79+ ? Object . entries ( paramOptions ) . reduce (
80+ ( acc , [ name , param ] ) => ( { ...acc , [ name ] : String ( param . default ?? '' ) } ) ,
81+ { }
82+ )
83+ : query . params ;
8984
90- onChange ( {
91- ...query ,
92- paramOptions,
93- params,
94- } ) ;
85+ onChange ( {
86+ ...query ,
87+ paramOptions,
88+ params,
89+ } ) ;
90+ } )
91+ . catch ( console . error ) ;
9592 // eslint-disable-next-line react-hooks/exhaustive-deps
96- } , [ nodes , nodeIndex ] ) ;
93+ } , [ datasource . url , pipes , query . pipeName ] ) ;
9794
9895 return (
9996 < div className = { styles . root } >
@@ -136,65 +133,46 @@ export default function QueryEditor({
136133 ) }
137134
138135 < div className = { styles . cogIconWrapper } >
139- < IconButton
140- name = "cog"
141- variant = { isOptionsOpen ? 'primary' : 'secondary' }
142- onClick = { ( ) => setIsOptionsOpen ( ( value ) => ! value ) }
143- />
136+ { query . format === 'timeseries' && (
137+ < IconButton
138+ name = "cog"
139+ variant = { isOptionsOpen ? 'primary' : 'secondary' }
140+ onClick = { ( ) => setIsOptionsOpen ( ( value ) => ! value ) }
141+ />
142+ ) }
144143 </ div >
145144 </ InlineFieldRow >
146145
147146 { isOptionsOpen && (
148147 < InlineFieldRow >
149- < InlineField label = "Node index" tooltip = "Index of the pipe nodes" labelWidth = { 15 } >
148+ < InlineField
149+ label = "Extrapolate"
150+ labelWidth = { 15 }
151+ tooltip = "Turn on if you don't like when last data point in time series much lower then previous"
152+ >
153+ < InlineSwitch
154+ value = { query . extrapolate }
155+ onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , extrapolate : ! ! value } ) }
156+ />
157+ </ InlineField >
158+ < InlineField label = "Time key" labelWidth = { 16 } tooltip = "Time key of the data" >
150159 < Input
151- value = { nodeIndex }
152- type = "number"
153- min = { 0 }
154- max = { nodes . length - 1 }
155- onChange = { ( { currentTarget : { valueAsNumber } } ) => setNodeIndex ( valueAsNumber ) }
160+ value = { query . timeKey }
161+ onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , timeKey : value } ) }
162+ />
163+ </ InlineField >
164+ < InlineField label = "Data keys" labelWidth = { 16 } tooltip = "Comma-separated keys to access values of the data" >
165+ < Input
166+ value = { query . dataKeys }
167+ onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , dataKeys : value } ) }
168+ />
169+ </ InlineField >
170+ < InlineField label = "Label keys" labelWidth = { 16 } tooltip = "Comma-separated keys to access labels of the data" >
171+ < Input
172+ value = { query . labelKeys }
173+ onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , labelKeys : value } ) }
156174 />
157175 </ InlineField >
158- { query . format === 'timeseries' && (
159- < >
160- < InlineField
161- label = "Extrapolate"
162- labelWidth = { 15 }
163- tooltip = "Turn on if you don't like when last data point in time series much lower then previous"
164- >
165- < InlineSwitch
166- value = { query . extrapolate }
167- onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , extrapolate : ! ! value } ) }
168- />
169- </ InlineField >
170- < InlineField label = "Time key" labelWidth = { 16 } tooltip = "Time key of the data" >
171- < Input
172- value = { query . timeKey }
173- onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , timeKey : value } ) }
174- />
175- </ InlineField >
176- < InlineField
177- label = "Data keys"
178- labelWidth = { 16 }
179- tooltip = "Comma-separated keys to access values of the data"
180- >
181- < Input
182- value = { query . dataKeys }
183- onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , dataKeys : value } ) }
184- />
185- </ InlineField >
186- < InlineField
187- label = "Label keys"
188- labelWidth = { 16 }
189- tooltip = "Comma-separated keys to access labels of the data"
190- >
191- < Input
192- value = { query . labelKeys }
193- onChange = { ( { currentTarget : { value } } ) => onChange ( { ...query , labelKeys : value } ) }
194- />
195- </ InlineField >
196- </ >
197- ) }
198176 </ InlineFieldRow >
199177 ) }
200178
0 commit comments