{"version":3,"file":"PromQueryLegendEditor.mjs","sources":["../../../../src/querybuilder/components/PromQueryLegendEditor.tsx"],"sourcesContent":["// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryLegendEditor.tsx\nimport { useRef } from 'react';\nimport * as React from 'react';\n\nimport { type SelectableValue } from '@grafana/data';\nimport { selectors } from '@grafana/e2e-selectors';\nimport { t } from '@grafana/i18n';\nimport { EditorField } from '@grafana/plugin-ui';\nimport { AutoSizeInput, Select } from '@grafana/ui';\n\nimport { LegendFormatMode } from '../../types';\n\ninterface PromQueryLegendEditorProps {\n  legendFormat: string | undefined;\n  onChange: (legendFormat: string) => void;\n  onRunQuery: () => void;\n}\n\nconst getLegendModeOptions = () => [\n  {\n    label: t('grafana-prometheus.prom-query-legend-editor.get-legend-mode-options.label-auto', 'Auto'),\n    value: LegendFormatMode.Auto,\n    description: t(\n      'grafana-prometheus.prom-query-legend-editor.get-legend-mode-options.description-auto',\n      'Only includes unique labels'\n    ),\n  },\n  {\n    label: t('grafana-prometheus.prom-query-legend-editor.get-legend-mode-options.label-verbose', 'Verbose'),\n    value: LegendFormatMode.Verbose,\n    description: t(\n      'grafana-prometheus.prom-query-legend-editor.get-legend-mode-options.description-verbose',\n      'All label names and values'\n    ),\n  },\n  {\n    label: t('grafana-prometheus.prom-query-legend-editor.get-legend-mode-options.label-custom', 'Custom'),\n    value: LegendFormatMode.Custom,\n    description: t(\n      'grafana-prometheus.prom-query-legend-editor.get-legend-mode-options.description-custom',\n      'Provide a naming template'\n    ),\n  },\n];\n\n/**\n * Tests for this component are on the parent level (PromQueryBuilderOptions).\n */\nexport const PromQueryLegendEditor = React.memo<PromQueryLegendEditorProps>(\n  ({ legendFormat, onChange, onRunQuery }) => {\n    const mode = getLegendMode(legendFormat);\n    const inputRef = useRef<HTMLInputElement | null>(null);\n    const legendModeOptions = getLegendModeOptions();\n\n    const onLegendFormatChanged = (evt: React.FormEvent<HTMLInputElement>) => {\n      let newFormat = evt.currentTarget.value;\n      if (newFormat.length === 0) {\n        newFormat = LegendFormatMode.Auto;\n      }\n\n      if (newFormat !== legendFormat) {\n        onChange(newFormat);\n        onRunQuery();\n      }\n    };\n\n    const onLegendModeChanged = (value: SelectableValue<LegendFormatMode>) => {\n      switch (value.value!) {\n        case LegendFormatMode.Auto:\n          onChange(LegendFormatMode.Auto);\n          break;\n        case LegendFormatMode.Custom:\n          onChange('{{label_name}}');\n          setTimeout(() => {\n            inputRef.current?.focus();\n            inputRef.current?.setSelectionRange(2, 12, 'forward');\n          }, 10);\n          break;\n        case LegendFormatMode.Verbose:\n          onChange('');\n          break;\n      }\n      onRunQuery();\n    };\n\n    return (\n      <EditorField\n        label={t('grafana-prometheus.querybuilder.prom-query-legend-editor.label-legend', 'Legend')}\n        tooltip={t(\n          'grafana-prometheus.querybuilder.prom-query-legend-editor.tooltip-legend',\n          'Series name override or template. Ex. {{templateExample}} will be replaced with label value for {{labelName}}.',\n          { templateExample: '{{hostname}}', labelName: 'hostname' }\n        )}\n        data-testid={selectors.components.DataSource.Prometheus.queryEditor.legend}\n      >\n        <>\n          {mode === LegendFormatMode.Custom && (\n            <AutoSizeInput\n              id=\"legendFormat\"\n              minWidth={22}\n              // eslint-disable-next-line @grafana/i18n/no-untranslated-strings\n              placeholder=\"auto\"\n              defaultValue={legendFormat}\n              onCommitChange={onLegendFormatChanged}\n              ref={inputRef}\n            />\n          )}\n          {mode !== LegendFormatMode.Custom && (\n            <Select\n              inputId=\"legend.mode\"\n              isSearchable={false}\n              placeholder={t(\n                'grafana-prometheus.querybuilder.prom-query-legend-editor.placeholder-select-legend-mode',\n                'Select legend mode'\n              )}\n              options={legendModeOptions}\n              width={22}\n              onChange={onLegendModeChanged}\n              value={legendModeOptions.find((x) => x.value === mode)}\n              aria-label={t(\n                'grafana-prometheus.querybuilder.prom-query-legend-editor.aria-label-legend',\n                'Legend combobox'\n              )}\n            />\n          )}\n        </>\n      </EditorField>\n    );\n  }\n);\n\nPromQueryLegendEditor.displayName = 'PromQueryLegendEditor';\n\nfunction getLegendMode(legendFormat: string | undefined) {\n  // This special value means the new smart minimal series naming\n  if (legendFormat === LegendFormatMode.Auto) {\n    return LegendFormatMode.Auto;\n  }\n\n  // Missing or empty legend format is the old verbose behavior\n  if (legendFormat == null || legendFormat === '') {\n    return LegendFormatMode.Verbose;\n  }\n\n  return LegendFormatMode.Custom;\n}\n\nexport function getLegendModeLabel(legendFormat: string | undefined) {\n  const legendModeOptions = getLegendModeOptions();\n  const mode = getLegendMode(legendFormat);\n  if (mode !== LegendFormatMode.Custom) {\n    return legendModeOptions.find((x) => x.value === mode)?.label;\n  }\n  return legendFormat;\n}\n"],"names":[],"mappings":";;;;;;;;;;AAkBA,MAAM,uBAAuB,MAAM;AAAA,EACjC;AAAA,IACE,KAAA,EAAO,CAAA,CAAE,gFAAA,EAAkF,MAAM,CAAA;AAAA,IACjG,OAAO,gBAAA,CAAiB,IAAA;AAAA,IACxB,WAAA,EAAa,CAAA;AAAA,MACX,sFAAA;AAAA,MACA;AAAA;AACF,GACF;AAAA,EACA;AAAA,IACE,KAAA,EAAO,CAAA,CAAE,mFAAA,EAAqF,SAAS,CAAA;AAAA,IACvG,OAAO,gBAAA,CAAiB,OAAA;AAAA,IACxB,WAAA,EAAa,CAAA;AAAA,MACX,yFAAA;AAAA,MACA;AAAA;AACF,GACF;AAAA,EACA;AAAA,IACE,KAAA,EAAO,CAAA,CAAE,kFAAA,EAAoF,QAAQ,CAAA;AAAA,IACrG,OAAO,gBAAA,CAAiB,MAAA;AAAA,IACxB,WAAA,EAAa,CAAA;AAAA,MACX,wFAAA;AAAA,MACA;AAAA;AACF;AAEJ,CAAA;AAKO,MAAM,wBAAwB,KAAA,CAAM,IAAA;AAAA,EACzC,CAAC,EAAE,YAAA,EAAc,QAAA,EAAU,YAAW,KAAM;AAC1C,IAAA,MAAM,IAAA,GAAO,cAAc,YAAY,CAAA;AACvC,IAAA,MAAM,QAAA,GA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