11// Libraries
2- import React , { FC , createContext , useContext , useState , useMemo } from 'react'
2+ import React , { FC , createContext , useContext , useState } from 'react'
33
44// Contexts
55import { PipeContext } from 'src/flows/context/pipe'
@@ -63,10 +63,12 @@ interface QueryBuilderContextType {
6363 selectMeasurement : ( measurement ?: string ) => void
6464
6565 add : ( ) => void
66+ cancelKey : ( idx : number ) => void
67+ cancelValue : ( idx : number ) => void
6668 remove : ( idx : number ) => void
6769 update : ( idx : number , card : Partial < QueryBuilderCard > ) => void
68- loadKeys : ( idx : number , search ?: string ) => void
69- loadValues : ( idx : number , search ?: string ) => void
70+ loadKeys : ( idx : number , search ?: string ) => void | Promise < any >
71+ loadValues : ( idx : number , search ?: string ) => void | Promise < any >
7072}
7173
7274export const DEFAULT_CONTEXT : QueryBuilderContextType = {
@@ -76,6 +78,8 @@ export const DEFAULT_CONTEXT: QueryBuilderContextType = {
7678 selectMeasurement : _ => { } ,
7779
7880 add : ( ) => { } ,
81+ cancelKey : ( _idx : number ) => { } ,
82+ cancelValue : ( _idx : number ) => { } ,
7983 remove : ( _idx : number ) => { } ,
8084 update : ( _idx : number , _card : QueryBuilderCard ) => { } ,
8185 loadKeys : ( _idx : number , _search ?: string ) => { } ,
@@ -113,6 +117,8 @@ export const QueryBuilderProvider: FC = ({children}) => {
113117 const { id, data, range, update} = useContext ( PipeContext )
114118 const { query, getPanelQueries} = useContext ( FlowQueryContext )
115119 const { buckets} = useContext ( BucketContext )
120+ const [ cancelKey , setCancelKey ] = useState ( { } )
121+ const [ cancelValue , setCancelValue ] = useState ( { } )
116122
117123 const [ cardMeta , setCardMeta ] = useState < QueryBuilderMeta [ ] > (
118124 Array ( data . tags . length ) . fill ( {
@@ -181,9 +187,8 @@ export const QueryBuilderProvider: FC = ({children}) => {
181187 }
182188 }
183189
184- const cards = useMemo (
185- ( ) => data . tags . map ( ( tag , idx ) => fromBuilderConfig ( tag , cardMeta [ idx ] ) ) ,
186- [ data . tags , cardMeta ]
190+ const cards = data . tags . map ( ( tag , idx ) =>
191+ fromBuilderConfig ( tag , cardMeta [ idx ] )
187192 )
188193
189194 const add = ( ) => {
@@ -294,23 +299,31 @@ export const QueryBuilderProvider: FC = ({children}) => {
294299 |> limit(n: ${ limit } )`
295300 }
296301
297- query ( queryText , scope )
302+ const result = query ( queryText , scope )
303+
304+ setCancelKey ( prev => ( {
305+ ...prev ,
306+ [ idx ] : ( result as any ) . cancel ,
307+ } ) )
308+
309+ return result
298310 . then ( resp => {
299311 return ( Object . values ( resp . parsed . table . columns ) . filter (
300312 c => c . name === '_value' && c . type === 'string'
301313 ) [ 0 ] ?. data ?? [ ] ) as string [ ]
302314 } )
303315 . then ( keys => {
304- if ( ! cards [ idx ] . keys . selected [ 0 ] ) {
316+ const currentCards = data . tags . map ( fromBuilderConfig )
317+ if ( ! currentCards [ idx ] . keys . selected [ 0 ] ) {
305318 if ( idx === 0 && keys . includes ( '_measurement' ) ) {
306- cards [ idx ] . keys . selected = [ '_measurement' ]
319+ currentCards [ idx ] . keys . selected = [ '_measurement' ]
307320 } else {
308- cards [ idx ] . keys . selected = [ keys [ 0 ] ]
321+ currentCards [ idx ] . keys . selected = [ keys [ 0 ] ]
309322 }
310323
311- update ( { tags : cards . map ( toBuilderConfig ) } )
312- } else if ( ! keys . includes ( cards [ idx ] . keys . selected [ 0 ] ) ) {
313- keys . unshift ( cards [ idx ] . keys . selected [ 0 ] )
324+ update ( { tags : currentCards . map ( toBuilderConfig ) } )
325+ } else if ( ! keys . includes ( currentCards [ idx ] . keys . selected [ 0 ] ) ) {
326+ keys . unshift ( currentCards [ idx ] . keys . selected [ 0 ] )
314327 }
315328
316329 cardMeta . splice ( idx , 1 , {
@@ -326,6 +339,18 @@ export const QueryBuilderProvider: FC = ({children}) => {
326339 } )
327340 }
328341
342+ const handleCancelKey = idx => {
343+ if ( idx in cancelKey ) {
344+ cancelKey [ idx ] ( )
345+ }
346+ }
347+
348+ const handleCancelValue = idx => {
349+ if ( idx in cancelValue ) {
350+ cancelValue [ idx ] ( )
351+ }
352+ }
353+
329354 const loadValues = ( idx , search ) => {
330355 if (
331356 cardMeta [ idx ] . loadingValues === RemoteDataState . Loading ||
@@ -402,7 +427,14 @@ export const QueryBuilderProvider: FC = ({children}) => {
402427 |> sort()`
403428 }
404429
405- query ( queryText , scope )
430+ const result = query ( queryText , scope )
431+
432+ setCancelValue ( prev => ( {
433+ ...prev ,
434+ [ idx ] : ( result as any ) . cancel ,
435+ } ) )
436+
437+ return result
406438 . then ( resp => {
407439 return ( Object . values ( resp . parsed . table . columns ) . filter (
408440 c => c . name === '_value' && c . type === 'string'
@@ -480,7 +512,9 @@ export const QueryBuilderProvider: FC = ({children}) => {
480512 ..._card ,
481513 }
482514
483- update ( { tags : cards . map ( toBuilderConfig ) } )
515+ data . tags = cards . map ( toBuilderConfig )
516+
517+ update ( { tags : data . tags } )
484518 }
485519
486520 return (
@@ -490,7 +524,8 @@ export const QueryBuilderProvider: FC = ({children}) => {
490524
491525 selectBucket,
492526 selectMeasurement,
493-
527+ cancelKey : handleCancelKey ,
528+ cancelValue : handleCancelValue ,
494529 add,
495530 remove,
496531 update : updater ,
0 commit comments