2022-04-22 14:33:13 +01:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
2020-03-02 09:31:09 -05:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2021-05-19 08:16:05 +02:00
|
|
|
import { Select } from '@grafana/ui';
|
2022-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
|
import { SELECT_WIDTH } from '../constants';
|
|
|
|
|
import CloudMonitoringDatasource from '../datasource';
|
2018-12-20 16:55:02 +01:00
|
|
|
|
2022-04-22 14:33:13 +01:00
|
|
|
import { QueryEditorRow } from '.';
|
|
|
|
|
|
2018-12-20 16:55:02 +01:00
|
|
|
export interface Props {
|
2022-01-25 17:28:59 +01:00
|
|
|
refId: string;
|
2020-06-30 18:47:13 +03:00
|
|
|
datasource: CloudMonitoringDatasource;
|
2020-03-02 09:31:09 -05:00
|
|
|
onChange: (projectName: string) => void;
|
|
|
|
|
templateVariableOptions: Array<SelectableValue<string>>;
|
2018-12-20 16:55:02 +01:00
|
|
|
projectName: string;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 17:28:59 +01:00
|
|
|
export function Project({ refId, projectName, datasource, onChange, templateVariableOptions }: Props) {
|
2021-05-19 08:16:05 +02:00
|
|
|
const [projects, setProjects] = useState<Array<SelectableValue<string>>>([]);
|
|
|
|
|
useEffect(() => {
|
2021-11-10 08:58:04 -05:00
|
|
|
datasource.getProjects().then((projects) => setProjects(projects));
|
|
|
|
|
}, [datasource]);
|
|
|
|
|
|
|
|
|
|
const projectsWithTemplateVariables = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
projects,
|
|
|
|
|
{
|
|
|
|
|
label: 'Template Variables',
|
|
|
|
|
options: templateVariableOptions,
|
|
|
|
|
},
|
|
|
|
|
...projects,
|
|
|
|
|
],
|
|
|
|
|
[projects, templateVariableOptions]
|
|
|
|
|
);
|
2021-05-19 08:16:05 +02:00
|
|
|
|
2020-03-02 09:31:09 -05:00
|
|
|
return (
|
2022-01-25 17:28:59 +01:00
|
|
|
<QueryEditorRow label="Project" htmlFor={`${refId}-project`}>
|
2021-05-19 08:16:05 +02:00
|
|
|
<Select
|
2021-08-04 15:47:53 +01:00
|
|
|
menuShouldPortal
|
2021-05-19 08:16:05 +02:00
|
|
|
width={SELECT_WIDTH}
|
2020-03-02 09:31:09 -05:00
|
|
|
allowCustomValue
|
2021-05-19 08:16:05 +02:00
|
|
|
formatCreateLabel={(v) => `Use project: ${v}`}
|
2020-03-02 09:31:09 -05:00
|
|
|
onChange={({ value }) => onChange(value!)}
|
2021-11-10 08:58:04 -05:00
|
|
|
options={projectsWithTemplateVariables}
|
2021-05-19 08:16:05 +02:00
|
|
|
value={{ value: projectName, label: projectName }}
|
2020-03-02 09:31:09 -05:00
|
|
|
placeholder="Select Project"
|
2022-01-25 17:28:59 +01:00
|
|
|
inputId={`${refId}-project`}
|
2020-03-02 09:31:09 -05:00
|
|
|
/>
|
2021-05-19 08:16:05 +02:00
|
|
|
</QueryEditorRow>
|
2020-03-02 09:31:09 -05:00
|
|
|
);
|
2018-12-20 16:55:02 +01:00
|
|
|
}
|