1- import { QueryEditorProps } from '@grafana/data' ;
1+ import { QueryEditorProps , SelectableValue } from '@grafana/data' ;
22import { DataSourceOptions } from '@grafana/google-sdk' ;
33import { InlineFieldRow , InlineFormLabel , InlineSwitch , Input , LinkButton , Segment , SegmentAsync } from '@grafana/ui' ;
44import React , { ChangeEvent , PureComponent } from 'react' ;
@@ -51,15 +51,45 @@ export const formatCacheTimeLabel = (s: number = defaultCacheDuration) => {
5151} ;
5252
5353export class QueryEditor extends PureComponent < Props > {
54+ state = {
55+ selectedSheetOption : undefined as SelectableValue < string > | string | undefined ,
56+ } ;
57+
5458 componentDidMount ( ) {
5559 if ( ! this . props . query . hasOwnProperty ( 'cacheDurationSeconds' ) ) {
5660 this . props . onChange ( {
5761 ...this . props . query ,
5862 cacheDurationSeconds : defaultCacheDuration , // um :(
5963 } ) ;
6064 }
65+ this . updateSelectedSheetOption ( ) ;
66+ }
67+
68+ componentDidUpdate ( prevProps : Props ) {
69+ if ( prevProps . query . spreadsheet !== this . props . query . spreadsheet ) {
70+ this . updateSelectedSheetOption ( ) ;
71+ }
6172 }
6273
74+ updateSelectedSheetOption = async ( ) => {
75+ const { query, datasource } = this . props ;
76+ if ( ! query . spreadsheet ) {
77+ this . setState ( { selectedSheetOption : undefined } ) ;
78+ return ;
79+ }
80+ try {
81+ const sheetOptions = await datasource . getSpreadSheets ( ) ;
82+ const matchingOption = sheetOptions . find ( ( opt ) => opt . value === query . spreadsheet ) ;
83+ if ( matchingOption ) {
84+ this . setState ( { selectedSheetOption : matchingOption } ) ;
85+ } else {
86+ this . setState ( { selectedSheetOption : query . spreadsheet } ) ;
87+ }
88+ } catch {
89+ this . setState ( { selectedSheetOption : query . spreadsheet } ) ;
90+ }
91+ } ;
92+
6393 onRangeChange = ( event : ChangeEvent < HTMLInputElement > ) => {
6494 this . props . onChange ( {
6595 ...this . props . query ,
@@ -71,10 +101,12 @@ export class QueryEditor extends PureComponent<Props> {
71101 const { query, onRunQuery, onChange } = this . props ;
72102
73103 if ( ! item . value ) {
104+ this . setState ( { selectedSheetOption : undefined } ) ;
74105 return ; // ignore delete?
75106 }
76107
77108 const v = item . value ;
109+ this . setState ( { selectedSheetOption : item } ) ;
78110 // Check for pasted full URLs
79111 if ( / ( .* ) \/ s p r e a d s h e e t s \/ d \/ ( .* ) / . test ( v ) ) {
80112 onChange ( { ...query , ...getGoogleSheetRangeInfoFromURL ( v ) } ) ;
@@ -118,9 +150,19 @@ export class QueryEditor extends PureComponent<Props> {
118150 Spreadsheet ID
119151 </ InlineFormLabel >
120152 < SegmentAsync
121- loadOptions = { ( ) => datasource . getSpreadSheets ( ) }
153+ loadOptions = { async ( ) => {
154+ const options = await datasource . getSpreadSheets ( ) ;
155+ const { query } = this . props ;
156+ if ( query . spreadsheet ) {
157+ const matchingOption = options . find ( ( opt ) => opt . value === query . spreadsheet ) ;
158+ if ( matchingOption && this . state . selectedSheetOption !== matchingOption ) {
159+ this . setState ( { selectedSheetOption : matchingOption } ) ;
160+ }
161+ }
162+ return options ;
163+ } }
122164 placeholder = "Enter SpreadsheetID"
123- value = { query . spreadsheet }
165+ value = { this . state . selectedSheetOption ?? query . spreadsheet }
124166 allowCustomValue = { true }
125167 onChange = { this . onSpreadsheetIDChange }
126168 />
0 commit comments