From d1431ff0a373e85bf16b745dfd0cd85334edd693 Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Thu, 24 Sep 2026 17:10:37 -0400 Subject: [PATCH 1/2] fix(mfe): report legend colour changes so the host can save them Clicking a legend colour pill called `onSeriesColorChange`, which wrote the new colour into the in-memory `PanelModel` through `updateFieldConfig` and re-rendered. Nothing told the host a change had happened, so the colour was visible but unsaveable: the draft never learned about it and publishing dropped it. Report the dashboard save model as a `panelColorChanged` event, matching the payload `dashboardLayoutChanged` already sends, so the host persists the colour through the draft it owns. The spectrum tab fires a colour on every pointer move, so the report is debounced and cancelled on unmount. Gate the picker on a new `enablePanelColorEdit` flag. `usePanelContext` renders a plain swatch when `onSeriesColorChange` is absent, so built-in dashboards -- which cannot save -- no longer open a tray at all instead of offering an edit that is silently discarded. Standalone Grafana is unaffected and keeps the picker. --- public/app/core/reducers/fn-slice.ts | 11 ++++ .../dashboard/dashgrid/PanelStateWrapper.tsx | 63 ++++++++++++++++++- .../dashgrid/fnSeriesColorEdit.test.ts | 16 +++++ .../dashboard/dashgrid/fnSeriesColorEdit.ts | 29 +++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts create mode 100644 public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts diff --git a/public/app/core/reducers/fn-slice.ts b/public/app/core/reducers/fn-slice.ts index 69d053f58acf9..7345289747e17 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 `fieldConfig` is reported to the host as a + * `panelFieldConfigChanged` 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. + */ + 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..f5b110dd44ea7 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,53 @@ 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 (`enablePanelColorEdit` off) take that path. + */ + 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 +319,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 +341,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 +707,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..3a64b921af34a --- /dev/null +++ b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts @@ -0,0 +1,16 @@ +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 })).toBe(false); + }); + + it('offers the picker once the host opts in', () => { + expect(canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: true })).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..c4ee3b3b0a553 --- /dev/null +++ b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts @@ -0,0 +1,29 @@ +/** 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; +} + +/** + * Whether the legend colour pill should open the series colour picker. + * + * Only a host that opted in can persist the result, so a dashboard embedded + * without `enablePanelColorEdit` (every built-in dashboard) must not offer the + * tray at all. Standalone Grafana is unaffected and keeps the picker. + */ +export function canEditSeriesColor(access: FnSeriesColorEditAccess): boolean { + if (!access.isFnDashboard) { + return true; + } + + return access.enablePanelColorEdit === true; +} From 1cc06b3babe6d73468071221373b889ffd8a9467 Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Thu, 24 Sep 2026 17:26:25 -0400 Subject: [PATCH 2/2] fix(mfe): require a host listener before offering colour edits An Fn dashboard that set enablePanelColorEdit without subscribing to the event still got the picker, and emitFnColorChange then dropped the colour -- the same unsaveable edit this change removes. Require both the opt-in and a listener; standalone Grafana stays eligible without either. Also mark FnState.enablePanelColorEdit readonly per the **/*.ts rule and correct a doc comment naming the pre-rename event. --- public/app/core/reducers/fn-slice.ts | 10 +++++----- .../dashboard/dashgrid/PanelStateWrapper.tsx | 3 ++- .../dashboard/dashgrid/fnSeriesColorEdit.test.ts | 16 +++++++++++++--- .../dashboard/dashgrid/fnSeriesColorEdit.ts | 11 +++++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/public/app/core/reducers/fn-slice.ts b/public/app/core/reducers/fn-slice.ts index 7345289747e17..2f2edfa6ac589 100644 --- a/public/app/core/reducers/fn-slice.ts +++ b/public/app/core/reducers/fn-slice.ts @@ -41,12 +41,12 @@ export interface FnState { enablePanelLayoutEdit: boolean; /** * When true, legend colour pills open the series colour picker and the - * resulting `fieldConfig` is reported to the host as a - * `panelFieldConfigChanged` 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. + * 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. */ - enablePanelColorEdit: boolean; + readonly enablePanelColorEdit: boolean; metadata: { teams: string[]; eventListener: ((event: { type: string; data: T }) => void) | null; diff --git a/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx b/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx index f5b110dd44ea7..7665eeb8ed85a 100644 --- a/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx +++ b/public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx @@ -187,7 +187,8 @@ export class PanelStateWrapperDisConnected extends PureComponent { * 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 (`enablePanelColorEdit` off) take that path. + * 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; diff --git a/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts index 3a64b921af34a..52e038daba983 100644 --- a/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts +++ b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts @@ -7,10 +7,20 @@ describe('canEditSeriesColor', () => { it('hides the picker on a built-in embedded dashboard', () => { expect(canEditSeriesColor({ isFnDashboard: true })).toBe(false); - expect(canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: false })).toBe(false); + expect( + canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: false, panelColorListener: jest.fn() }) + ).toBe(false); }); - it('offers the picker once the host opts in', () => { - expect(canEditSeriesColor({ isFnDashboard: true, enablePanelColorEdit: true })).toBe(true); + // 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 index c4ee3b3b0a553..411ceab7a303b 100644 --- a/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts +++ b/public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts @@ -11,19 +11,22 @@ 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 can persist the result, so a dashboard embedded - * without `enablePanelColorEdit` (every built-in dashboard) must not offer the - * tray at all. Standalone Grafana is unaffected and keeps the 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; + return access.enablePanelColorEdit === true && access.panelColorListener != null; }