Skip to content

Commit bc6bf17

Browse files
committed
[ENHANCEMENT] plugin-system: add panel query editor options context
1 parent f3b2c70 commit bc6bf17

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

dashboards/src/components/PanelDrawer/PanelQueriesSharedControls.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export function PanelQueriesSharedControls({
8787
<PanelSpecEditor
8888
control={control}
8989
panelDefinition={panelDefinition}
90+
queryOptions={pluginQueryOptions}
9091
onJSONChange={onJSONChange}
9192
onQueriesChange={onQueriesChange}
9293
onQueryRun={handleRunQuery}

plugin-system/src/components/PanelSpecEditor/PanelSpecEditor.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import { ErrorAlert, JSONEditor, LinksEditor } from '@perses-dev/components';
1515
import { PanelDefinition, PanelEditorValues, QueryDefinition, UnknownSpec } from '@perses-dev/core';
1616
import { Control, Controller } from 'react-hook-form';
1717
import { forwardRef, ReactElement } from 'react';
18-
import { QueryCountProvider, useDataQueriesContext, usePlugin } from '../../runtime';
18+
import {
19+
QueryCountProvider,
20+
QueryEditorOptionsProvider,
21+
QueryOptions,
22+
useDataQueriesContext,
23+
usePlugin,
24+
} from '../../runtime';
1925
import { PanelPlugin } from '../../model';
2026
import { OptionsEditorTabsProps, OptionsEditorTabs } from '../OptionsEditorTabs';
2127
import { MultiQueryEditor } from '../MultiQueryEditor';
@@ -24,14 +30,15 @@ import { PluginEditorRef } from '../PluginEditor';
2430
export interface PanelSpecEditorProps {
2531
control: Control<PanelEditorValues>;
2632
panelDefinition: PanelDefinition;
33+
queryOptions?: QueryOptions;
2734
onQueriesChange: (queries: QueryDefinition[]) => void;
2835
onQueryRun: (index: number, query: QueryDefinition) => void;
2936
onPluginSpecChange: (spec: UnknownSpec) => void;
3037
onJSONChange: (panelDefinitionStr: string) => void;
3138
}
3239

3340
export const PanelSpecEditor = forwardRef<PluginEditorRef, PanelSpecEditorProps>((props, ref): ReactElement | null => {
34-
const { control, panelDefinition, onQueriesChange, onQueryRun, onPluginSpecChange, onJSONChange } = props;
41+
const { control, panelDefinition, queryOptions, onQueriesChange, onQueryRun, onPluginSpecChange, onJSONChange } = props;
3542
const { kind } = panelDefinition.spec.plugin;
3643
const { data: plugin, isLoading, error } = usePlugin('Panel', kind);
3744

@@ -132,9 +139,11 @@ export const PanelSpecEditor = forwardRef<PluginEditorRef, PanelSpecEditorProps>
132139
});
133140

134141
return (
135-
<QueryCountProvider queryCount={(panelDefinition.spec.queries ?? []).length}>
136-
<OptionsEditorTabs key={tabs.length} tabs={tabs} />
137-
</QueryCountProvider>
142+
<QueryEditorOptionsProvider options={queryOptions}>
143+
<QueryCountProvider queryCount={(panelDefinition.spec.queries ?? []).length}>
144+
<OptionsEditorTabs key={tabs.length} tabs={tabs} />
145+
</QueryCountProvider>
146+
</QueryEditorOptionsProvider>
138147
);
139148
});
140149

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright The Perses Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
import { createContext, ReactElement, ReactNode, useContext } from 'react';
15+
16+
export type QueryEditorOptions = Record<string, unknown>;
17+
18+
const QueryEditorOptionsContext = createContext<QueryEditorOptions>({});
19+
20+
export interface QueryEditorOptionsProviderProps {
21+
options?: QueryEditorOptions;
22+
children: ReactNode;
23+
}
24+
25+
export function QueryEditorOptionsProvider({ options, children }: QueryEditorOptionsProviderProps): ReactElement {
26+
return <QueryEditorOptionsContext.Provider value={options ?? {}}>{children}</QueryEditorOptionsContext.Provider>;
27+
}
28+
29+
export function useQueryEditorOptions<T extends QueryEditorOptions = QueryEditorOptions>(): T {
30+
return useContext(QueryEditorOptionsContext) as T;
31+
}

plugin-system/src/runtime/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ export * from './profile-queries';
2222
export * from './item-actions';
2323
export * from './DataQueriesProvider';
2424
export * from './QueryCountProvider';
25+
export * from './QueryEditorOptionsProvider';
2526
export * from './RouterProvider';
2627
export * from './UsageMetricsProvider';

0 commit comments

Comments
 (0)