Files
grafana/public/app/features/query/components/QueryActionComponent.ts
Kristina 783ff71560 Explore: Allow saving to Query Library from query row (#89381)
* wip

* Add save functionality to query row

* add success conditional

* move around translations

* Add translations

* Add key to fix test

* Add key to the right spot

* define specific save button

* WIP - Use RowActionComponents to add action without modifying the core component

* Only add component once on render

* Move logic to main explore page

* Add keyed render actions to prevent redundancy, use this to add keyed action

* Overcome the forces of dayquil to attempt to make actual sense

* Add scoped actions to query action component

* Spaces not allowed in generateName
2024-07-30 13:56:44 -05:00

59 lines
1.9 KiB
TypeScript

import { CoreApp, DataQuery, DataSourceInstanceSettings, TimeRange } from '@grafana/data';
interface ActionComponentProps {
query?: DataQuery;
queries?: Array<Partial<DataQuery>>;
onAddQuery?: (q: DataQuery) => void;
onChangeDataSource?: (ds: DataSourceInstanceSettings) => void;
timeRange?: TimeRange;
dataSource?: DataSourceInstanceSettings;
key: string | number;
}
export type QueryActionComponent = (props: ActionComponentProps) => JSX.Element | null;
type ScopedQueryActionComponent = {
scope: CoreApp;
queryActionComponent: QueryActionComponent;
};
class QueryActionComponents {
extraRenderActions: QueryActionComponent[] = [];
/* additional actions added in core grafana are likely to be needed in only one kind of app,
and the add function may be ran multiple times by the component so it is keyed to ensure uniqueness
*/
keyedScopedExtraRenderActions: Map<string, ScopedQueryActionComponent> = new Map();
addExtraRenderAction(extra: QueryActionComponent) {
this.extraRenderActions = this.extraRenderActions.concat(extra);
}
// for adding actions that will need to be unique, even if the add function is ran multiple times
addKeyedExtraRenderAction(key: string, extra: ScopedQueryActionComponent) {
this.keyedScopedExtraRenderActions.set(key, extra);
}
// only returns actions that are not scoped to a specific CoreApp
getAllExtraRenderAction(): QueryActionComponent[] {
return this.extraRenderActions;
}
getScopedExtraRenderAction(scope: CoreApp): QueryActionComponent[] {
const scopedActions = Array.from(this.keyedScopedExtraRenderActions, (value) => value[1]).filter(
(sra) => sra.scope === scope
);
return scopedActions.map((sa) => sa.queryActionComponent);
}
}
/**
* @internal and experimental
*/
export const GroupActionComponents = new QueryActionComponents();
/**
* @internal and experimental
*/
export const RowActionComponents = new QueryActionComponents();