From a779622982c676e269a633164870bde077a7a359 Mon Sep 17 00:00:00 2001 From: Marcus Andersson Date: Wed, 14 Oct 2020 12:55:28 +0200 Subject: [PATCH] Variables: prevent adhoc filters from crashing when they are not loaded properly (#28226) * fixing so we will present a better error message when loading ad hoc filter variables. * added tests to verify url parsing. * added a test to make sure it works the oppisite way. --- .../src/components/Segment/SegmentAsync.tsx | 38 +++++++----- .../variables/adhoc/urlParser.test.ts | 60 +++++++++++++++++++ .../app/features/variables/adhoc/urlParser.ts | 8 +-- 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx b/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx index ef32da8a1cc..6e09ce1a990 100644 --- a/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx +++ b/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx @@ -1,9 +1,11 @@ -import React, { useState } from 'react'; +import React from 'react'; import { cx } from 'emotion'; import _ from 'lodash'; import { SegmentSelect } from './SegmentSelect'; import { SelectableValue } from '@grafana/data'; import { useExpandableLabel, SegmentProps } from '.'; +import { useAsyncFn } from 'react-use'; +import { AsyncState } from 'react-use/lib/useAsync'; export interface SegmentAsyncProps extends SegmentProps { value?: T | SelectableValue; @@ -20,20 +22,14 @@ export function SegmentAsync({ allowCustomValue, placeholder, }: React.PropsWithChildren>) { - const [selectPlaceholder, setSelectPlaceholder] = useState(''); - const [loadedOptions, setLoadedOptions] = useState>>([]); + const [state, fetchOptions] = useAsyncFn(loadOptions, [loadOptions]); const [Label, width, expanded, setExpanded] = useExpandableLabel(false); if (!expanded) { const label = _.isObject(value) ? value.label : value; return (