2022-04-22 14:33:13 +01:00
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
|
|
2021-05-19 08:16:05 +02:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
|
|
|
|
import { Select } from '@grafana/ui';
|
2022-01-25 17:28:59 +01:00
|
|
|
|
2022-05-09 13:43:10 +02:00
|
|
|
import { periodOption } from '../constants';
|
2021-05-19 08:16:05 +02:00
|
|
|
|
2022-05-09 13:43:10 +02:00
|
|
|
export interface Props {
|
2022-01-25 17:28:59 +01:00
|
|
|
inputId: string;
|
2022-05-09 13:43:10 +02:00
|
|
|
onChange: (period: string) => void;
|
2021-05-19 08:16:05 +02:00
|
|
|
templateVariableOptions: Array<SelectableValue<string>>;
|
2022-05-09 13:43:10 +02:00
|
|
|
aligmentPeriods: periodOption[];
|
2021-05-19 08:16:05 +02:00
|
|
|
selectWidth?: number;
|
2022-05-09 13:43:10 +02:00
|
|
|
category?: string;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
current?: string;
|
2021-05-19 08:16:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 13:43:10 +02:00
|
|
|
export function PeriodSelect({
|
2022-01-25 17:28:59 +01:00
|
|
|
inputId,
|
2021-10-20 09:49:49 +02:00
|
|
|
templateVariableOptions,
|
|
|
|
|
onChange,
|
2022-05-09 13:43:10 +02:00
|
|
|
current,
|
|
|
|
|
disabled,
|
|
|
|
|
aligmentPeriods,
|
|
|
|
|
}: Props) {
|
2021-05-19 08:16:05 +02:00
|
|
|
const options = useMemo(
|
|
|
|
|
() =>
|
2022-05-09 13:43:10 +02:00
|
|
|
aligmentPeriods.map((ap) => ({
|
2021-05-19 08:16:05 +02:00
|
|
|
...ap,
|
|
|
|
|
label: ap.text,
|
|
|
|
|
})),
|
2022-05-09 13:43:10 +02:00
|
|
|
[aligmentPeriods]
|
2021-05-19 08:16:05 +02:00
|
|
|
);
|
|
|
|
|
const visibleOptions = useMemo(() => options.filter((ap) => !ap.hidden), [options]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Select
|
2022-09-15 10:12:26 -04:00
|
|
|
width="auto"
|
2022-05-09 13:43:10 +02:00
|
|
|
onChange={({ value }) => onChange(value!)}
|
|
|
|
|
value={[...options, ...templateVariableOptions].find((s) => s.value === current)}
|
2021-05-19 08:16:05 +02:00
|
|
|
options={[
|
|
|
|
|
{
|
|
|
|
|
label: 'Template Variables',
|
|
|
|
|
options: templateVariableOptions,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Aggregations',
|
|
|
|
|
expanded: true,
|
|
|
|
|
options: visibleOptions,
|
|
|
|
|
},
|
|
|
|
|
]}
|
2022-05-09 13:43:10 +02:00
|
|
|
placeholder="Select Period"
|
2022-01-25 17:28:59 +01:00
|
|
|
inputId={inputId}
|
2022-05-09 13:43:10 +02:00
|
|
|
disabled={disabled}
|
|
|
|
|
allowCustomValue
|
2022-10-31 12:52:25 +01:00
|
|
|
menuPlacement="top"
|
2022-09-15 10:12:26 -04:00
|
|
|
/>
|
2021-05-19 08:16:05 +02:00
|
|
|
);
|
2021-10-20 09:49:49 +02:00
|
|
|
}
|