Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fridgepoet committed Feb 28, 2024
1 parent 3b7ced0 commit 99f6ed0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DataSourceInstanceSettings,
ScopedVars,
SelectableValue,
CoreApp,
} from '@grafana/data';
import { DataSourceOptions } from '@grafana/google-sdk';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
Expand All @@ -13,6 +14,7 @@ import { trackRequest } from 'tracking';

export class DataSource extends DataSourceWithBackend<SheetsQuery, DataSourceOptions> {
authType: string;
defaultSheetID?: string;
constructor(instanceSettings: DataSourceInstanceSettings<DataSourceOptions>) {
super(instanceSettings);
this.authType = instanceSettings.jsonData.authenticationType;
Expand Down Expand Up @@ -43,4 +45,8 @@ export class DataSource extends DataSourceWithBackend<SheetsQuery, DataSourceOpt
: []
);
}

getDefaultQuery(app: CoreApp): Partial<SheetsQuery> {
return { spreadsheet: this.defaultSheetID };
}
}
51 changes: 44 additions & 7 deletions src/components/ConfigEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { DataSourcePluginOptionsEditorProps, onUpdateDatasourceSecureJsonDataOption } from '@grafana/data';
import {
DataSourcePluginOptionsEditorProps,
onUpdateDatasourceSecureJsonDataOption,
onUpdateDatasourceJsonDataOptionSelect,
} from '@grafana/data';
import { AuthConfig, DataSourceOptions } from '@grafana/google-sdk';
import { Field, SecretInput } from '@grafana/ui';
import { Field, SecretInput, InlineFormLabel, SegmentAsync } from '@grafana/ui';
import React from 'react';
import { GoogleSheetsAuth, GoogleSheetsSecureJSONData, googleSheetsAuthTypes } from '../types';
import { getBackwardCompatibleOptions } from '../utils';
import { ConfigurationHelp } from './ConfigurationHelp';
import { DataSourceDescription } from '@grafana/experimental';
import { Divider } from './Divider';
import { getDataSourceSrv } from '@grafana/runtime';
import { DataSource } from 'DataSource';

export type Props = DataSourcePluginOptionsEditorProps<DataSourceOptions, GoogleSheetsSecureJSONData>;

Expand All @@ -28,6 +34,16 @@ export function ConfigEditor(props: Props) {
onChange: onUpdateDatasourceSecureJsonDataOption(props, 'apiKey'),
};

const loadSheetIDs = async () => {
const { options } = props;
try {
const ds = (await getDataSourceSrv().get(options.uid)) as DataSource;
return ds.getSpreadSheets();
} catch {
return [];
}
};

return (
<>
<DataSourceDescription
Expand All @@ -36,17 +52,38 @@ export function ConfigEditor(props: Props) {
hasRequiredFields={false}
/>

<Divider />
<div className="gf-form">
<InlineFormLabel
className="width-10"
tooltip="The id of a default google sheet. The datasource must be saved before this can be set."
>
Default SheetID
</InlineFormLabel>
<SegmentAsync
className="width-30"
loadOptions={() => this.loadSheetIDs()}
placeholder="Select Spreadsheet ID"
value={jsonData.defaultSheetID}
allowCustomValue={true}
onChange={onUpdateDatasourceJsonDataOptionSelect(this.props, 'defaultSheetID')}
disabled={
(jsonData.authType === GoogleAuthType.KEY && (!secureJsonFields || !secureJsonFields.apiKey)) ||
(jsonData.authType === GoogleAuthType.JWT && (!secureJsonFields || !secureJsonFields.jwt))
}
/>
</div>

<Divider/>

Check failure on line 76 in src/components/ConfigEditor.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`

<ConfigurationHelp authenticationType={options.jsonData.authenticationType} />
<ConfigurationHelp authenticationType={options.jsonData.authenticationType}/>

Check failure on line 78 in src/components/ConfigEditor.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`

<Divider />
<Divider/>

Check failure on line 80 in src/components/ConfigEditor.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`

<AuthConfig authOptions={googleSheetsAuthTypes} onOptionsChange={props.onOptionsChange} options={options} />
<AuthConfig authOptions={googleSheetsAuthTypes} onOptionsChange={props.onOptionsChange} options={options}/>

Check failure on line 82 in src/components/ConfigEditor.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`

{options.jsonData.authenticationType === GoogleSheetsAuth.API && (
<Field label="API Key">
<SecretInput {...apiKeyProps} label="API key" width={40} />
<SecretInput {...apiKeyProps} label="API key" width={40}/>

Check failure on line 86 in src/components/ConfigEditor.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
</Field>
)}
</>
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DataSourceJsonData } from '@grafana/data';
import { DataQuery } from '@grafana/schema';
import { GoogleAuthType, GOOGLE_AUTH_TYPE_OPTIONS, DataSourceSecureJsonData } from '@grafana/google-sdk';

Expand Down Expand Up @@ -36,3 +37,7 @@ export interface SheetsQuery extends DataQuery {
cacheDurationSeconds?: number;
useTimeFilter?: boolean;
}

export interface SheetsSourceOptions extends DataSourceJsonData {
defaultSheetID?: string;
}

0 comments on commit 99f6ed0

Please sign in to comment.