From 0f8ec324dd18eafdd339a37433188ae509804db6 Mon Sep 17 00:00:00 2001 From: Cameron Waterman Date: Thu, 4 Jan 2024 11:03:22 -0800 Subject: [PATCH] fix(system): Run query on initial load (#55) --- .../system/components/SystemQueryEditor.tsx | 10 +++++++++- .../workspace/components/WorkspaceQueryEditor.tsx | 11 +++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/datasources/system/components/SystemQueryEditor.tsx b/src/datasources/system/components/SystemQueryEditor.tsx index fc689ed9..444af943 100644 --- a/src/datasources/system/components/SystemQueryEditor.tsx +++ b/src/datasources/system/components/SystemQueryEditor.tsx @@ -1,4 +1,4 @@ -import React, { FormEvent } from 'react'; +import React, { FormEvent, useEffect } from 'react'; import { AutoSizeInput, RadioButtonGroup, Select } from '@grafana/ui'; import { QueryEditorProps, SelectableValue } from '@grafana/data'; import { SystemDataSource } from '../SystemDataSource'; @@ -11,6 +11,14 @@ type Props = QueryEditorProps; export function SystemQueryEditor({ query, onChange, onRunQuery, datasource }: Props) { query = datasource.prepareQuery(query); + useEffect(() => { + if (query.queryKind === SystemQueryType.Summary) { + onChange(query); + onRunQuery(); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); // Only run on mount + const workspaces = useWorkspaceOptions(datasource); const onQueryTypeChange = (value: SystemQueryType) => { diff --git a/src/datasources/workspace/components/WorkspaceQueryEditor.tsx b/src/datasources/workspace/components/WorkspaceQueryEditor.tsx index fb770ed5..82166733 100644 --- a/src/datasources/workspace/components/WorkspaceQueryEditor.tsx +++ b/src/datasources/workspace/components/WorkspaceQueryEditor.tsx @@ -5,8 +5,15 @@ import { WorkspaceDataSource } from '../WorkspaceDataSource'; type Props = QueryEditorProps; -export function WorkspaceQueryEditor({ onRunQuery }: Props) { - useEffect(onRunQuery, [onRunQuery]); +export function WorkspaceQueryEditor({ query, onChange, onRunQuery }: Props) { + useEffect(() => { + // The "init" property is required for the initial query in Explore to work since the query + // only runs if a change to a property is detected. It otherwise has no use and can be removed + // if a property is added to "WorkspaceQuery". + onChange(Object.assign({ init: true }, query)); + onRunQuery(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); // Only run on mount return ( This data source returns all SystemLink workspaces and does not include a query editor.