From 00edc1c6b1c184d8640fff21aae231a1aa507dfb Mon Sep 17 00:00:00 2001 From: Sven Grossmann Date: Fri, 10 Feb 2023 14:23:17 +0100 Subject: [PATCH] Loki Context UI: Do not disable last label (#62866) * improved text * prevent all labels removed * use `structuredClone` instead of lodash * update test * get `structuredClone` in test? * fix using structuredClone --- .betterer.results | 5 +++++ .../loki/components/LokiContextUi.test.tsx | 16 +++++++++++++++- .../datasource/loki/components/LokiContextUi.tsx | 11 ++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.betterer.results b/.betterer.results index 71ad4e909bc..1f4e6445dee 100644 --- a/.betterer.results +++ b/.betterer.results @@ -5998,6 +5998,11 @@ exports[`better eslint`] = { "public/app/plugins/datasource/loki/LiveStreams.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], + "public/app/plugins/datasource/loki/components/LokiContextUi.test.tsx:5381": [ + [0, 0, 0, "Unexpected any. Specify a different type.", "0"], + [0, 0, 0, "Unexpected any. Specify a different type.", "1"], + [0, 0, 0, "Unexpected any. Specify a different type.", "2"] + ], "public/app/plugins/datasource/loki/components/LokiLabelBrowser.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"] diff --git a/public/app/plugins/datasource/loki/components/LokiContextUi.test.tsx b/public/app/plugins/datasource/loki/components/LokiContextUi.test.tsx index 587beb46e2a..0989604338c 100644 --- a/public/app/plugins/datasource/loki/components/LokiContextUi.test.tsx +++ b/public/app/plugins/datasource/loki/components/LokiContextUi.test.tsx @@ -15,6 +15,20 @@ jest.mock('@grafana/runtime', () => ({ })); describe('LokiContextUi', () => { + const savedGlobal = global; + beforeAll(() => { + // TODO: `structuredClone` is not yet in jsdom https://github.com/jsdom/jsdom/issues/3363 + if (!(global as any).structuredClone) { + (global as any).structuredClone = function structuredClone(objectToClone: any) { + const stringified = JSON.stringify(objectToClone); + const parsed = JSON.parse(stringified); + return parsed; + }; + } + }); + afterAll(() => { + global = savedGlobal; + }); const setupProps = (): LokiContextUiProps => { const mockLanguageProvider = { start: jest.fn().mockImplementation(() => Promise.resolve()), @@ -66,7 +80,7 @@ describe('LokiContextUi', () => { render(); // Initial set of labels is available and not selected - expect(await screen.findByText(/Select labels to include in the context query/)).toBeInTheDocument(); + expect(await screen.findByText(/Select labels to be included in the context query/)).toBeInTheDocument(); }); it('starts the languageProvider', async () => { diff --git a/public/app/plugins/datasource/loki/components/LokiContextUi.tsx b/public/app/plugins/datasource/loki/components/LokiContextUi.tsx index 0df61348beb..76a0068ad07 100644 --- a/public/app/plugins/datasource/loki/components/LokiContextUi.tsx +++ b/public/app/plugins/datasource/loki/components/LokiContextUi.tsx @@ -49,9 +49,11 @@ export function LokiContextUi(props: LokiContextUiProps) { const styles = useStyles2(getStyles); const [contextFilters, setContextFilters] = useState([]); + const [initialized, setInitialized] = useState(false); const timerHandle = React.useRef(); const previousInitialized = React.useRef(false); + const previousContextFilters = React.useRef([]); useEffect(() => { if (!initialized) { return; @@ -63,6 +65,13 @@ export function LokiContextUi(props: LokiContextUiProps) { return; } + if (contextFilters.filter(({ enabled, fromParser }) => enabled && !fromParser).length === 0) { + setContextFilters(previousContextFilters.current); + return; + } + + previousContextFilters.current = structuredClone(contextFilters); + if (timerHandle.current) { clearTimeout(timerHandle.current); } @@ -143,7 +152,7 @@ export function LokiContextUi(props: LokiContextUiProps) { colorIndex={1} /> {' '} - Select labels to include in the context query: + Select labels to be included in the context query: