import { css } from '@emotion/css'; import React from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { Button, Card, HorizontalGroup, useStyles2 } from '@grafana/ui'; import { LayerName } from 'app/core/components/Layers/LayerName'; import { SavedQuery, useUpdateSavedQueryMutation, Variable } from '../api/SavedQueriesApi'; import { SavedQueryUpdateOpts } from './QueryEditorDrawer'; type Props = { savedQuery: SavedQuery; options: SavedQueryUpdateOpts; }; export const VariablesTab = ({ savedQuery, options }: Props) => { const styles = useStyles2(getStyles); const [updateSavedQuery] = useUpdateSavedQueryMutation(); const onVariableNameChange = (variable: Variable, newName: string) => { const newVariables = savedQuery.variables.map((v: Variable) => { if (v.name === variable.name) { v.name = newName; } return v; }); updateSavedQuery({ query: { ...savedQuery, variables: newVariables, }, opts: options, }); }; const onVariableValueChange = (variable: Variable, newValue: string) => { const newVariables = savedQuery.variables.map((v: Variable) => { if (v.name === variable.name) { v.current.value = newValue; } return v; }); updateSavedQuery({ query: { ...savedQuery, variables: newVariables, }, opts: options, }); }; const onAddVariable = () => { // NOTE: doing mutation to force re-render savedQuery.variables.unshift({ name: 'New variable', current: { value: 'General', }, }); updateSavedQuery({ query: savedQuery, opts: options }); }; const onRemoveVariable = (variable: Variable) => { const varIndex = savedQuery.variables.map((v: Variable, index: number) => { if (v.name === variable.name) { return index; } return; }); if (typeof varIndex === 'number') { // NOTE: doing mutation vs filter to force re-render savedQuery.variables.splice(varIndex, 1); updateSavedQuery({ query: savedQuery, opts: options }); } }; return (