2025-08-27 20:10:47 +08:00
|
|
|
|
import { useId, useMemo } from 'react';
|
2025-04-23 17:10:09 +08:00
|
|
|
|
|
2025-06-12 17:03:52 +08:00
|
|
|
|
import { t } from '@grafana/i18n';
|
2025-04-23 17:10:09 +08:00
|
|
|
|
import { LocalValueVariable } from '@grafana/scenes';
|
|
|
|
|
import { Box, Stack } from '@grafana/ui';
|
|
|
|
|
import { OptionsPaneCategoryDescriptor } from 'app/features/dashboard/components/PanelEditor/OptionsPaneCategoryDescriptor';
|
|
|
|
|
import { OptionsPaneItemDescriptor } from 'app/features/dashboard/components/PanelEditor/OptionsPaneItemDescriptor';
|
|
|
|
|
|
|
|
|
|
import { EditableDashboardElement, EditableDashboardElementInfo } from '../../scene/types/EditableDashboardElement';
|
|
|
|
|
|
2025-09-09 17:31:06 +08:00
|
|
|
|
function useEditPaneOptions(this: LocalVariableEditableElement): OptionsPaneCategoryDescriptor[] {
|
|
|
|
|
const variable = this.variable;
|
|
|
|
|
const localVariableCategoryId = useId();
|
|
|
|
|
const localVariableId = useId();
|
|
|
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
|
const category = new OptionsPaneCategoryDescriptor({
|
|
|
|
|
title: '',
|
|
|
|
|
id: localVariableCategoryId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
category.addItem(
|
|
|
|
|
new OptionsPaneItemDescriptor({
|
|
|
|
|
title: '',
|
|
|
|
|
id: localVariableId,
|
|
|
|
|
skipField: true,
|
|
|
|
|
render: () => {
|
|
|
|
|
return (
|
|
|
|
|
<Box paddingBottom={1}>
|
|
|
|
|
<Stack>
|
|
|
|
|
<Stack>
|
|
|
|
|
<span>${variable.state.name}</span>
|
|
|
|
|
<span>=</span>
|
|
|
|
|
<span>{variable.getValueText()}</span>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [category];
|
|
|
|
|
}, [localVariableCategoryId, localVariableId, variable]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-23 17:10:09 +08:00
|
|
|
|
export class LocalVariableEditableElement implements EditableDashboardElement {
|
|
|
|
|
public readonly isEditableDashboardElement = true;
|
|
|
|
|
|
|
|
|
|
public constructor(public variable: LocalValueVariable) {}
|
|
|
|
|
|
|
|
|
|
public getEditableElementInfo(): EditableDashboardElementInfo {
|
|
|
|
|
return {
|
|
|
|
|
typeName: t('dashboard.edit-pane.elements.local-variable', 'Local variable'),
|
|
|
|
|
icon: 'dollar-alt',
|
|
|
|
|
instanceName: ` $${this.variable.state.name} = ${this.variable.getValueText!()}`,
|
|
|
|
|
isHidden: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 17:31:06 +08:00
|
|
|
|
public useEditPaneOptions = useEditPaneOptions.bind(this);
|
2025-04-23 17:10:09 +08:00
|
|
|
|
}
|