11import { QuickwitDataSource } from "datasource" ;
22import { useState , useEffect , useCallback } from "react" ;
3+ import { MetricFindValue } from '@grafana/data' ;
34
45/**
56 * Provide suggestions based on datasource fields
67 */
78
8- // TODO : improve autocompleter interface
9+ export type Suggestion = {
10+ from : number ,
11+ options : Array < {
12+ label : string ,
13+ detail ?: string ,
14+ type ?: string ,
15+ } >
16+ }
917
10- export function useDatasourceFields ( datasource : QuickwitDataSource ) : [ any [ ] , ( word : string ) => any ] {
11- const [ fields , setFields ] = useState < any [ ] > ( [ ] ) ;
18+ export function useDatasourceFields ( datasource : QuickwitDataSource ) {
19+ const [ fields , setFields ] = useState < MetricFindValue [ ] > ( [ ] ) ;
1220
1321 useEffect ( ( ) => {
1422 if ( datasource . getTagKeys ) {
1523 datasource . getTagKeys ( { searchable : true } ) . then ( setFields ) ;
1624 }
1725 } , [ datasource , setFields ] ) ;
1826
19- const getSuggestions = useCallback ( async ( word : string ) => {
20- let suggestions : { from : number ; options : any [ ] ; } = { from : 0 , options : [ ] } ;
27+ const getSuggestions = useCallback ( async ( word : string ) : Promise < Suggestion > => {
28+ let suggestions : Suggestion = { from : 0 , options : [ ] } ;
2129
2230 const wordIsField = word . match ( / ( [ \w \. ] + ) : " ? ( \S * ) / ) ;
2331 if ( wordIsField ?. length ) {
@@ -40,5 +48,6 @@ export function useDatasourceFields(datasource: QuickwitDataSource): [any[], (wo
4048 return suggestions ;
4149
4250 } , [ datasource , fields ] ) ;
43- return [ fields , getSuggestions ] ;
51+
52+ return { fields, getSuggestions}
4453}
0 commit comments