From 3a33297885cd9ade8ccac8c33807b682da2d7ccc Mon Sep 17 00:00:00 2001 From: alexandra vargas Date: Fri, 18 Jul 2025 14:52:12 +0200 Subject: [PATCH] implement onCancelEditingQuery library --- .../QueryLibrary/QueryLibraryContext.tsx | 2 +- public/app/features/explore/QueryRows.tsx | 6 ++ .../hooks/useStateSync/external.utils.ts | 3 +- .../hooks/useStateSync/synchronizer/toURL.ts | 3 +- .../query/components/QueryEditorRow.tsx | 71 +++++++++++++------ .../query/components/QueryEditorRows.tsx | 3 + 6 files changed, 62 insertions(+), 26 deletions(-) diff --git a/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx b/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx index 90640203d48..e5acaf3cc3a 100644 --- a/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx +++ b/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx @@ -46,7 +46,7 @@ export type QueryLibraryContextType = { * Returns a predefined small button that can be used to save a query to the library. * @param query */ - renderSaveQueryButton: (query: DataQuery) => ReactNode; + renderSaveQueryButton: (query: DataQuery, queryRef?: string) => ReactNode; queryLibraryEnabled: boolean; }; diff --git a/public/app/features/explore/QueryRows.tsx b/public/app/features/explore/QueryRows.tsx index 2355b8bbc07..040532c4abb 100644 --- a/public/app/features/explore/QueryRows.tsx +++ b/public/app/features/explore/QueryRows.tsx @@ -11,6 +11,7 @@ import { QueryEditorRows } from '../query/components/QueryEditorRows'; import { ContentOutlineItem } from './ContentOutline/ContentOutlineItem'; import { changeDatasource } from './state/datasource'; +import { updateQueryRefAction } from './state/explorePane'; import { changeQueries, runQueries } from './state/query'; import { getExploreItemSelector } from './state/selectors'; @@ -86,6 +87,10 @@ export const QueryRows = ({ exploreId }: Props) => { reportInteraction('grafana_query_row_toggle', queryStatus === undefined ? {} : { queryEnabled: queryStatus }); }; + const onCancelQueryLibraryEdit = useCallback(() => { + dispatch(updateQueryRefAction({ exploreId, queryRef: undefined })); + }, [dispatch, exploreId]); + return ( { history={history} eventBus={eventBridge} queryRef={queryRef} + onCancelQueryLibraryEdit={onCancelQueryLibraryEdit} queryRowWrapper={(children, refId) => ( , action: Ac changeRangeAction.type, changePanelsStateAction.type, changeDatasource.fulfilled.type, + updateQueryRefAction.type, ].includes(action.type) && !paused.current ); } diff --git a/public/app/features/query/components/QueryEditorRow.tsx b/public/app/features/query/components/QueryEditorRow.tsx index 41330d74228..cdcaabf1979 100644 --- a/public/app/features/query/components/QueryEditorRow.tsx +++ b/public/app/features/query/components/QueryEditorRow.tsx @@ -22,7 +22,7 @@ import { selectors } from '@grafana/e2e-selectors'; import { Trans, t } from '@grafana/i18n'; import { getDataSourceSrv, renderLimitedComponents, reportInteraction, usePluginComponents } from '@grafana/runtime'; import { DataQuery } from '@grafana/schema'; -import { Badge, ErrorBoundaryAlert, List } from '@grafana/ui'; +import { Badge, Divider, ErrorBoundaryAlert, List } from '@grafana/ui'; import { OperationRowHelp } from 'app/core/components/QueryOperationRow/OperationRowHelp'; import { QueryOperationAction, @@ -68,6 +68,7 @@ export interface Props { collapsable?: boolean; hideRefId?: boolean; queryRef?: string; + onCancelQueryLibraryEdit?: () => void; } interface State { @@ -232,6 +233,13 @@ export class QueryEditorRow extends PureComponent { + const { onCancelQueryLibraryEdit } = this.props; + if (onCancelQueryLibraryEdit) { + onCancelQueryLibraryEdit(); + } + }; + onCopyQuery = () => { const { query, onAddQuery, onQueryCopied } = this.props; const copy = cloneDeep(query); @@ -327,7 +335,7 @@ export class QueryEditorRow extends PureComponent { const { queryRef } = this.props; - return ; + return ; }; renderExtraActions = () => { @@ -368,11 +376,13 @@ export class QueryEditorRow extends PureComponent @@ -385,19 +395,33 @@ export class QueryEditorRow extends PureComponent )} {this.renderExtraActions()} - - - { - onQueryReplacedFromLibrary?.(); - onReplace?.(query); - }} - /> + + {isEditingQueryLibraryRef && ( + <> + + + + )} + {!isEditingQueryLibraryRef && ( + + )} + {!isEditingQueryLibraryRef && ( + { + onQueryReplacedFromLibrary?.(); + onReplace?.(query); + }} + /> + )} {!hideHideQueryButton ? ( extends PureComponent ) : null} - + {!isEditingQueryLibraryRef && ( + + )} ); }; @@ -499,7 +525,6 @@ export function QueryLibraryBadgeWrapper(props: { queryRef?: string }) { return ( { diff --git a/public/app/features/query/components/QueryEditorRows.tsx b/public/app/features/query/components/QueryEditorRows.tsx index 2d969abbb7a..49d7ff97514 100644 --- a/public/app/features/query/components/QueryEditorRows.tsx +++ b/public/app/features/query/components/QueryEditorRows.tsx @@ -41,6 +41,7 @@ export interface Props { onQueryReplacedFromLibrary?: () => void; queryRowWrapper?: (children: ReactNode, refId: string) => ReactNode; queryRef?: string; + onCancelQueryLibraryEdit?: () => void; } export class QueryEditorRows extends PureComponent { @@ -177,6 +178,7 @@ export class QueryEditorRows extends PureComponent { onQueryReplacedFromLibrary, queryRowWrapper, queryRef, + onCancelQueryLibraryEdit, } = this.props; return ( @@ -215,6 +217,7 @@ export class QueryEditorRows extends PureComponent { history={history} eventBus={eventBus} queryRef={queryRef} + onCancelQueryLibraryEdit={onCancelQueryLibraryEdit} /> );