AutoRefreshIntervals.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { FC, useCallback, useEffect, useMemo, useState } from 'react';
  2. import { Input, defaultIntervals, Field } from '@grafana/ui';
  3. import { getTimeSrv } from '../../services/TimeSrv';
  4. export interface Props {
  5. refreshIntervals: string[];
  6. onRefreshIntervalChange: (interval: string[]) => void;
  7. getIntervalsFunc?: typeof getValidIntervals;
  8. validateIntervalsFunc?: typeof validateIntervals;
  9. }
  10. export const AutoRefreshIntervals: FC<Props> = ({
  11. refreshIntervals,
  12. onRefreshIntervalChange,
  13. getIntervalsFunc = getValidIntervals,
  14. validateIntervalsFunc = validateIntervals,
  15. }) => {
  16. const [intervals, setIntervals] = useState<string[]>(getIntervalsFunc(refreshIntervals ?? defaultIntervals));
  17. const [invalidIntervalsMessage, setInvalidIntervalsMessage] = useState<string | null>(null);
  18. useEffect(() => {
  19. const intervals = getIntervalsFunc(refreshIntervals ?? defaultIntervals);
  20. setIntervals(intervals);
  21. }, [getIntervalsFunc, refreshIntervals]);
  22. const intervalsString = useMemo(() => {
  23. if (!Array.isArray(intervals)) {
  24. return '';
  25. }
  26. return intervals.join(',');
  27. }, [intervals]);
  28. const onIntervalsChange = useCallback(
  29. (event: React.FormEvent<HTMLInputElement>) => {
  30. const newIntervals = event.currentTarget.value ? event.currentTarget.value.split(',') : [];
  31. setIntervals(newIntervals);
  32. },
  33. [setIntervals]
  34. );
  35. const onIntervalsBlur = useCallback(
  36. (event: React.FormEvent<HTMLInputElement>) => {
  37. const invalidMessage = validateIntervalsFunc(intervals);
  38. if (invalidMessage === null) {
  39. // only refresh dashboard JSON if intervals are valid
  40. onRefreshIntervalChange(getIntervalsFunc(intervals));
  41. }
  42. setInvalidIntervalsMessage(invalidMessage);
  43. },
  44. [getIntervalsFunc, intervals, onRefreshIntervalChange, validateIntervalsFunc]
  45. );
  46. return (
  47. <Field
  48. label="Auto refresh"
  49. description="Define the auto refresh intervals that should be available in the auto refresh list."
  50. error={invalidIntervalsMessage}
  51. invalid={!!invalidIntervalsMessage}
  52. >
  53. <Input
  54. id="auto-refresh-input"
  55. invalid={!!invalidIntervalsMessage}
  56. value={intervalsString}
  57. onChange={onIntervalsChange}
  58. onBlur={onIntervalsBlur}
  59. />
  60. </Field>
  61. );
  62. };
  63. export const validateIntervals = (
  64. intervals: string[],
  65. dependencies: { getTimeSrv: typeof getTimeSrv } = { getTimeSrv }
  66. ): string | null => {
  67. try {
  68. getValidIntervals(intervals, dependencies);
  69. return null;
  70. } catch (err) {
  71. return err.message;
  72. }
  73. };
  74. export const getValidIntervals = (
  75. intervals: string[],
  76. dependencies: { getTimeSrv: typeof getTimeSrv } = { getTimeSrv }
  77. ) => {
  78. const cleanIntervals = intervals.filter((i) => i.trim() !== '').map((interval) => interval.replace(/\s+/g, ''));
  79. return [...new Set(dependencies.getTimeSrv().getValidIntervals(cleanIntervals))];
  80. };