Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions public/app/core/reducers/fn-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (<T>(event: { type: string; data: T }) => void) | null;
Expand Down Expand Up @@ -71,6 +79,7 @@ export type FnPropMappedFromState = Extract<
| 'enablePanelEdit'
| 'enablePanelDelete'
| 'enablePanelLayoutEdit'
| 'enablePanelColorEdit'
| 'panelOptionsUpdate'
| 'panelQueryPreviewUpdate'
| 'refreshRevision'
Expand All @@ -90,6 +99,7 @@ export const fnStateProps: FnStateProp[] = [
'enablePanelEdit',
'enablePanelDelete',
'enablePanelLayoutEdit',
'enablePanelColorEdit',
'hiddenVariables',
'pageTitle',
'panelOptionsUpdate',
Expand Down Expand Up @@ -119,6 +129,7 @@ export const INITIAL_FN_STATE: FnState = {
enablePanelEdit: false,
enablePanelDelete: false,
enablePanelLayoutEdit: false,
enablePanelColorEdit: false,
metadata: {
teams: [],
eventListener: null,
Expand Down
64 changes: 62 additions & 2 deletions public/app/features/dashboard/dashgrid/PanelStateWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand All @@ -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?: <T>(event: { type: string; data: T }) => void;
}

export interface State {
Expand Down Expand Up @@ -116,7 +126,7 @@ export class PanelStateWrapperDisConnected extends PureComponent<Props, State> {
eventBus,
app: this.getPanelContextApp(),
sync: this.getSync,
onSeriesColorChange: this.onSeriesColorChange,
onSeriesColorChange: this.getSeriesColorChangeHandler(props),
onToggleSeriesVisibility: this.onSeriesVisibilityChange,
onAnnotationCreate: this.onAnnotationCreate,
onAnnotationUpdate: this.onAnnotationUpdate,
Expand Down Expand Up @@ -172,10 +182,54 @@ export class PanelStateWrapperDisConnected extends PureComponent<Props, State> {
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;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

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)
Expand Down Expand Up @@ -266,6 +320,8 @@ export class PanelStateWrapperDisConnected extends PureComponent<Props, State> {
componentWillUnmount() {
this.subs.unsubscribe();
liveTimer.remove(this);
// A pending colour report would otherwise read a torn-down dashboard.
this.reportFnColorChange.cancel();
}

liveTimeChanged(liveTime: TimeRange) {
Expand All @@ -286,12 +342,14 @@ export class PanelStateWrapperDisConnected extends PureComponent<Props, State> {
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,
},
});
}
Expand Down Expand Up @@ -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,
});
}

Expand Down
26 changes: 26 additions & 0 deletions public/app/features/dashboard/dashgrid/fnSeriesColorEdit.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
});
32 changes: 32 additions & 0 deletions public/app/features/dashboard/dashgrid/fnSeriesColorEdit.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading