2023-07-28 10:09:31 -03:00
|
|
|
import React, { PropsWithChildren, useMemo, useState } from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-10-12 09:43:41 +01:00
|
|
|
import { VariableRefresh } from '@grafana/data';
|
2023-07-28 10:09:31 -03:00
|
|
|
import { Field, RadioButtonGroup, useTheme2 } from '@grafana/ui';
|
|
|
|
|
import { useMediaQueryChange } from 'app/core/hooks/useMediaQueryChange';
|
2020-11-25 10:21:48 +01:00
|
|
|
|
|
|
|
|
interface Props {
|
2022-10-12 09:43:41 +01:00
|
|
|
onChange: (option: VariableRefresh) => void;
|
2020-11-25 10:21:48 +01:00
|
|
|
refresh: VariableRefresh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const REFRESH_OPTIONS = [
|
2021-04-01 09:17:39 -07:00
|
|
|
{ label: 'On dashboard load', value: VariableRefresh.onDashboardLoad },
|
|
|
|
|
{ label: 'On time range change', value: VariableRefresh.onTimeRangeChanged },
|
2020-11-25 10:21:48 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export function QueryVariableRefreshSelect({ onChange, refresh }: PropsWithChildren<Props>) {
|
2023-07-28 10:09:31 -03:00
|
|
|
const theme = useTheme2();
|
|
|
|
|
|
|
|
|
|
const [isSmallScreen, setIsSmallScreen] = useState(false);
|
|
|
|
|
useMediaQueryChange({
|
|
|
|
|
breakpoint: theme.breakpoints.values.sm,
|
|
|
|
|
onChange: (e) => {
|
|
|
|
|
setIsSmallScreen(!e.matches);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-12 09:43:41 +01:00
|
|
|
const value = useMemo(
|
|
|
|
|
() => REFRESH_OPTIONS.find((o) => o.value === refresh)?.value ?? REFRESH_OPTIONS[0].value,
|
|
|
|
|
[refresh]
|
|
|
|
|
);
|
2020-11-25 10:21:48 +01:00
|
|
|
|
|
|
|
|
return (
|
2022-10-12 09:43:41 +01:00
|
|
|
<Field label="Refresh" description="When to update the values of this variable">
|
2023-07-28 10:09:31 -03:00
|
|
|
<RadioButtonGroup
|
|
|
|
|
options={REFRESH_OPTIONS}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
value={value}
|
|
|
|
|
size={isSmallScreen ? 'sm' : 'md'}
|
|
|
|
|
/>
|
2022-10-12 09:43:41 +01:00
|
|
|
</Field>
|
2020-11-25 10:21:48 +01:00
|
|
|
);
|
|
|
|
|
}
|