diff --git a/public/app/core/reducers/fn-slice.ts b/public/app/core/reducers/fn-slice.ts index 69d053f58acf9..2f2edfa6ac589 100644 --- a/public/app/core/reducers/fn-slice.ts +++ b/public/app/core/reducers/fn-slice.ts @@ -39,6 +39,14 @@ export interface FnState { */ enablePanelDelete: boolean; enablePanelLayoutEdit: boolean; + /** + * When true, legend colour pills open the series colour picker and the + * resulting dashboard is reported to the host as a `panelColorChanged` + * event so it can be persisted. Built-in dashboards leave this off, which + * hides the picker entirely rather than offering an edit the host cannot + * save. + */ + readonly enablePanelColorEdit: boolean; metadata: { teams: string[]; eventListener: ((event: { type: string; data: T }) => void) | null; @@ -71,6 +79,7 @@ export type FnPropMappedFromState = Extract< | 'enablePanelEdit' | 'enablePanelDelete' | 'enablePanelLayoutEdit' + | 'enablePanelColorEdit' | 'panelOptionsUpdate' | 'panelQueryPreviewUpdate' | 'refreshRevision' @@ -90,6 +99,7 @@ export const fnStateProps: FnStateProp[] = [ 'enablePanelEdit', 'enablePanelDelete', 'enablePanelLayoutEdit', + 'enablePanelColorEdit', 'hiddenVariables', 'pageTitle', 'panelOptionsUpdate', @@ -119,6 +129,7 @@ export const INITIAL_FN_STATE: FnState = { enablePanelEdit: false, enablePanelDelete: false, enablePanelLayoutEdit: false, + enablePanelColorEdit: false, metadata: { teams: [], eventListener: null, diff --git a/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx b/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx index 94f9aa52bc020..7665eeb8ed85a 100644 --- a/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx +++ b/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx @@ -39,6 +39,7 @@ import { profiler } from 'app/core/profiler'; import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel'; import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; import { applyFilterFromTable } from 'app/features/variables/adhoc/actions'; +import { FnLoggerService } from 'app/fn_logger'; import { onUpdatePanelSnapshotData } from 'app/plugins/datasource/grafana/utils'; import { changeSeriesColorConfigFactory } from 'app/plugins/panel/timeseries/overrides/colorSeriesConfigFactory'; import { dispatch } from 'app/store/store'; @@ -55,6 +56,11 @@ import { loadSnapshotData } from '../utils/loadSnapshotData'; import { PanelHeaderMenuWrapper } from './PanelHeader/PanelHeaderMenuWrapper'; import { PanelLoadTimeMonitor } from './PanelLoadTimeMonitor'; import { seriesVisibilityConfigFactory } from './SeriesVisibilityConfigFactory'; +import { + canEditSeriesColor, + FN_PANEL_COLOR_CHANGE_DEBOUNCE_MS, + FN_PANEL_COLOR_CHANGED_EVENT, +} from './fnSeriesColorEdit'; import { liveTimer } from './liveTimer'; import { PanelOptionsLogger } from './panelOptionsLogger'; @@ -74,6 +80,10 @@ export interface Props { timezone?: string; hideMenu?: boolean; isFnDashboard?: boolean; + /** Host opt-in for the legend series colour picker. */ + enablePanelColorEdit?: boolean; + /** Channel the host listens on for `panelColorChanged`. */ + panelColorListener?: (event: { type: string; data: T }) => void; } export interface State { @@ -116,7 +126,7 @@ export class PanelStateWrapperDisConnected extends PureComponent { eventBus, app: this.getPanelContextApp(), sync: this.getSync, - onSeriesColorChange: this.onSeriesColorChange, + onSeriesColorChange: this.getSeriesColorChangeHandler(props), onToggleSeriesVisibility: this.onSeriesVisibilityChange, onAnnotationCreate: this.onAnnotationCreate, onAnnotationUpdate: this.onAnnotationUpdate, @@ -172,10 +182,54 @@ export class PanelStateWrapperDisConnected extends PureComponent { return onUpdatePanelSnapshotData(this.props.panel, frames); }; + /** + * The legend colour pill is only offered when the host can persist the + * result. `usePanelContext` treats a missing `onSeriesColorChange` as + * "read-only" and renders a plain swatch, so withholding the handler hides + * the picker instead of opening a tray whose change would be lost. Built-in + * dashboards take that path: they neither opt in nor subscribe, and both are + * required so an opt-in without a listener cannot strand a colour either. + */ + getSeriesColorChangeHandler(props: Props): ((label: string, color: string) => void) | undefined { + return canEditSeriesColor(props) ? this.onSeriesColorChange : undefined; + } + onSeriesColorChange = (label: string, color: string) => { this.onFieldConfigChange(changeSeriesColorConfigFactory(label, color, this.props.panel.fieldConfig)); + this.reportFnColorChange(); }; + /** + * `updateFieldConfig` only mutates the in-memory `PanelModel`, so the new + * colour rendered but the host never learned of it and had nothing to save. + * Reporting the dashboard save model — the same payload + * `dashboardLayoutChanged` sends — lets the host persist it through the + * draft it already owns. + */ + emitFnColorChange = () => { + const { enablePanelColorEdit, panelColorListener, dashboard, panel } = this.props; + if (!enablePanelColorEdit || !panelColorListener) { + return; + } + + const dashboardJson = dashboard.getSaveModelClone(); + if (!dashboardJson) { + return; + } + + try { + panelColorListener({ type: FN_PANEL_COLOR_CHANGED_EVENT, data: dashboardJson }); + } catch (error) { + FnLoggerService.warn('FN panel colour listener failed', { error, panelId: panel.id }); + } + }; + + /** + * The spectrum tab reports a colour on every pointer move, so the report is + * debounced to keep one draft write per colour the user settles on. + */ + reportFnColorChange = debounce(this.emitFnColorChange, FN_PANEL_COLOR_CHANGE_DEBOUNCE_MS); + onSeriesVisibilityChange = (label: string, mode: SeriesVisibilityChangeMode) => { this.onFieldConfigChange( seriesVisibilityConfigFactory(label, mode, this.props.panel.fieldConfig, this.state.data.series) @@ -266,6 +320,8 @@ export class PanelStateWrapperDisConnected extends PureComponent { componentWillUnmount() { this.subs.unsubscribe(); liveTimer.remove(this); + // A pending colour report would otherwise read a torn-down dashboard. + this.reportFnColorChange.cancel(); } liveTimeChanged(liveTime: TimeRange) { @@ -286,12 +342,14 @@ export class PanelStateWrapperDisConnected extends PureComponent { const { context } = this.state; const app = this.getPanelContextApp(); + const onSeriesColorChange = this.getSeriesColorChangeHandler(this.props); - if (context.app !== app) { + if (context.app !== app || context.onSeriesColorChange !== onSeriesColorChange) { this.setState({ context: { ...context, app, + onSeriesColorChange, }, }); } @@ -650,7 +708,9 @@ function mapStateToProps() { */ enablePanelEdit: state.fnGlobalState.enablePanelEdit, enablePanelDelete: state.fnGlobalState.enablePanelDelete, + enablePanelColorEdit: state.fnGlobalState.enablePanelColorEdit, panelEditListener: state.fnGlobalState.metadata?.eventListener ?? undefined, + panelColorListener: state.fnGlobalState.metadata?.eventListener ?? undefined, }); } diff --git a/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts new file mode 100644 index 0000000000000..52e038daba983 --- /dev/null +++ b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts @@ -0,0 +1,26 @@ +import { canEditSeriesColor } from './fnSeriesColorEdit'; + +describe('canEditSeriesColor', () => { + it('keeps the picker for standalone Grafana', () => { + expect(canEditSeriesColor({ isFnDashboard: false })).toBe(true); + }); + + it('hides the picker on a built-in embedded dashboard', () => { + expect(canEditSeriesColor({ isFnDashboard: true })).toBe(false); + expect( + canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: false, panelColorListener: jest.fn() }) + ).toBe(false); + }); + + // Opting in without subscribing would open a tray whose colour is dropped, + // which is the bug this whole change removes. + it('hides the picker when the host opted in but is not listening', () => { + expect(canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: true })).toBe(false); + }); + + it('offers the picker once the host opts in and listens', () => { + expect(canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: true, panelColorListener: jest.fn() })).toBe( + true + ); + }); +}); diff --git a/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts new file mode 100644 index 0000000000000..411ceab7a303b --- /dev/null +++ b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts @@ -0,0 +1,32 @@ +/** Event the host listens on to persist a colour change made from a legend. */ +export const FN_PANEL_COLOR_CHANGED_EVENT = 'panelColorChanged'; + +/** + * How long to wait after the last colour change before reporting it. The + * spectrum tab of the picker fires `onChange` on every pointer move, so + * emitting each one would queue a draft write per pixel dragged. + */ +export const FN_PANEL_COLOR_CHANGE_DEBOUNCE_MS = 400; + +export interface FnSeriesColorEditAccess { + readonly isFnDashboard?: boolean; + readonly enablePanelColorEdit?: boolean; + /** Present only when the host subscribed to `panelColorChanged`. */ + readonly panelColorListener?: unknown; +} + +/** + * Whether the legend colour pill should open the series colour picker. + * + * Only a host that opted in *and* is listening can persist the result, so an + * embedded dashboard missing either (every built-in dashboard) must not offer + * the tray at all -- a picker whose change is dropped is the bug this guards. + * Standalone Grafana is unaffected and keeps the picker. + */ +export function canEditSeriesColor(access: FnSeriesColorEditAccess): boolean { + if (!access.isFnDashboard) { + return true; + } + + return access.enablePanelColorEdit === true && access.panelColorListener != null; +}