From 626b799cffbce517d80b1653811f63db54dc1bf9 Mon Sep 17 00:00:00 2001 From: Collin Fingar Date: Fri, 17 Oct 2025 15:48:15 -0400 Subject: [PATCH] Saved Queries: Context State Migration (#112018) * wip: moving saved queries items to context * more wip * Tidying up unused types --- .../QueryLibrary/QueryLibraryContext.tsx | 28 +++++++++++++++++-- .../features/explore/QueryLibrary/mocks.tsx | 13 +++++++++ .../features/explore/QueryLibrary/types.ts | 7 +++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx b/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx index 4d72a56f1a1..50ebe16e49c 100644 --- a/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx +++ b/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx @@ -4,7 +4,7 @@ import { CoreApp } from '@grafana/data'; import { DataQuery } from '@grafana/schema'; import { SavedQuery } from 'app/features/explore/QueryLibrary/types'; -import { OnSelectQueryType, QueryLibraryEventsPropertyMap } from './types'; +import { OnSelectQueryType, QueryLibraryEventsPropertyMap, QueryLibraryTab } from './types'; export type QueryLibraryDrawerOptions = { datasourceFilters?: string[]; @@ -30,7 +30,7 @@ export type QueryLibraryContextType = { * @param newQuery New query to be added to the library. */ openDrawer: (options: QueryLibraryDrawerOptions) => void; - closeDrawer: (isSelectingQuery?: boolean, isEditingQuery?: boolean) => void; + closeDrawer: (isSelectingQuery?: boolean) => void; isDrawerOpen: boolean; onSave?: () => void; @@ -73,6 +73,18 @@ export type QueryLibraryContextType = { ) => void; setNewQuery: (query?: SavedQuery) => void; onSelectQuery: (query: DataQuery) => void; + onFavorite: (uid: string) => void; + onUnfavorite: (uid: string) => void; + userFavorites: { [key: string]: boolean }; + isEditingQuery: boolean; + setIsEditingQuery: (isEditingQuery: boolean) => void; + onAddHistoryQueryToLibrary: (newQuery: SavedQuery) => void; + activeTab: QueryLibraryTab; + setActiveTab: (activeTab: QueryLibraryTab) => void; + onTabChange: (activeTab: QueryLibraryTab) => void; + highlightedQuery: string | undefined; + newQuery: SavedQuery | undefined; + activeDatasources: string[]; /** Set a guard function that returns true to allow closing, false to prevent closing */ setCloseGuard: (shouldAllowClose: () => boolean) => void; }; @@ -97,6 +109,18 @@ export const QueryLibraryContext = createContext({ context: 'unknown', triggerAnalyticsEvent: () => {}, onSelectQuery: () => {}, + onFavorite: () => {}, + onUnfavorite: () => {}, + userFavorites: {}, + isEditingQuery: false, + setIsEditingQuery: () => {}, + onAddHistoryQueryToLibrary: () => {}, + activeTab: QueryLibraryTab.ALL, + setActiveTab: () => {}, + onTabChange: () => {}, + highlightedQuery: undefined, + newQuery: undefined, + activeDatasources: [], setCloseGuard: () => {}, }); diff --git a/public/app/features/explore/QueryLibrary/mocks.tsx b/public/app/features/explore/QueryLibrary/mocks.tsx index bbac5371e3b..bc6abbc6f34 100644 --- a/public/app/features/explore/QueryLibrary/mocks.tsx +++ b/public/app/features/explore/QueryLibrary/mocks.tsx @@ -1,6 +1,7 @@ import { PropsWithChildren } from 'react'; import { QueryLibraryContext } from './QueryLibraryContext'; +import { QueryLibraryTab } from './types'; type Props = { queryLibraryEnabled?: boolean; @@ -20,6 +21,18 @@ export function QueryLibraryContextProviderMock(props: PropsWithChildren) triggerAnalyticsEvent: jest.fn(), setNewQuery: jest.fn(), onSelectQuery: jest.fn(), + onFavorite: jest.fn(), + onUnfavorite: jest.fn(), + userFavorites: {}, + isEditingQuery: false, + activeTab: QueryLibraryTab.ALL, + activeDatasources: [], + setActiveTab: jest.fn(), + onTabChange: jest.fn(), + setIsEditingQuery: jest.fn(), + onAddHistoryQueryToLibrary: jest.fn(), + highlightedQuery: undefined, + newQuery: undefined, setCloseGuard: jest.fn(), }} > diff --git a/public/app/features/explore/QueryLibrary/types.ts b/public/app/features/explore/QueryLibrary/types.ts index c0934ccbf3b..fd693dfb430 100644 --- a/public/app/features/explore/QueryLibrary/types.ts +++ b/public/app/features/explore/QueryLibrary/types.ts @@ -33,3 +33,10 @@ export type SavedQuery = { datasourceRef?: DataSourceRef | null; datasourceType?: string; } & Omit, 'targets'>; + +export enum QueryLibraryTab { + ALL = 'all', + FAVORITES = 'favorites', + RECENT = 'history', + FEEDBACK = 'feedback', +}