1import { sharedSingleStatPanelChangedHandler, BigValueGraphMode, BigValueColorMode } from '@grafana/ui';
2import { FieldColorModeId, FieldConfigSource, PanelModel } from '@grafana/data';
3import { StatPanelOptions } from './types';
4import { BigValueTextMode } from '@grafana/ui/src/components/BigValue/BigValue';
5
6// This is called when the panel changes from another panel
7export const statPanelChangedHandler = (
8  panel: PanelModel<Partial<StatPanelOptions>> | any,
9  prevPluginId: string,
10  prevOptions: any
11) => {
12  // This handles most config changes
13  const options = sharedSingleStatPanelChangedHandler(panel, prevPluginId, prevOptions) as StatPanelOptions;
14
15  // Changing from angular singlestat
16  if (prevOptions.angular && (prevPluginId === 'singlestat' || prevPluginId === 'grafana-singlestat-panel')) {
17    const oldOptions = prevOptions.angular;
18
19    options.graphMode = BigValueGraphMode.None;
20    if (oldOptions.sparkline && oldOptions.sparkline.show) {
21      options.graphMode = BigValueGraphMode.Area;
22    }
23
24    if (oldOptions.colorBackground) {
25      options.colorMode = BigValueColorMode.Background;
26    } else if (oldOptions.colorValue) {
27      options.colorMode = BigValueColorMode.Value;
28    } else {
29      options.colorMode = BigValueColorMode.None;
30      if (oldOptions.sparkline?.lineColor && options.graphMode === BigValueGraphMode.Area) {
31        const cfg: FieldConfigSource = panel.fieldConfig ?? {};
32        cfg.defaults.color = {
33          mode: FieldColorModeId.Fixed,
34          fixedColor: oldOptions.sparkline.lineColor,
35        };
36        panel.fieldConfig = cfg;
37      }
38    }
39
40    if (oldOptions.valueName === 'name') {
41      options.textMode = BigValueTextMode.Name;
42    }
43  }
44
45  return options;
46};
47