Skip to content

Commit 86844f8

Browse files
ziomeckaKatarzyna Ziomek-Zdanowicz
and
Katarzyna Ziomek-Zdanowicz
authored
8822 ci (#66)
* 8822 Remove dashes from dashboard filter labels * 8822 Empty commit --------- Co-authored-by: Katarzyna Ziomek-Zdanowicz <[email protected]>
1 parent 0b82d09 commit 86844f8

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

public/app/features/variables/pickers/PickerRenderer.tsx

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css } from '@emotion/css';
2+
import { isString, isFunction } from 'lodash';
23
import React, { CSSProperties, FunctionComponent, PropsWithChildren, ReactElement, useMemo } from 'react';
34
// eslint-disable-next-line no-restricted-imports
45
import { useSelector } from 'react-redux';
@@ -10,6 +11,7 @@ import type { StoreState } from 'app/types';
1011

1112
import { variableAdapters } from '../adapters';
1213
import { VariableHide, VariableModel } from '../types';
14+
import { FnLoggerService } from 'app/fn_logger';
1315

1416
interface Props {
1517
variable: VariableModel;
@@ -58,9 +60,14 @@ const COMMON_PICKER_LABEL_STYLE: CSSProperties = {
5860
};
5961

6062
function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | null {
61-
const labelOrName = useMemo(() => variable.label || variable.name, [variable]);
6263
const { FNDashboard, mode } = useSelector<StoreState, FnGlobalState>(({ fnGlobalState }) => fnGlobalState);
6364

65+
const labelOrName = useMemo(() => {
66+
const value = variable.label || variable.name;
67+
68+
return FNDashboard ? transformFnLabel(value) : value;
69+
}, [variable, FNDashboard]);
70+
6471
const fnLabelStyle = useMemo(
6572
() => ({
6673
...COMMON_PICKER_LABEL_STYLE,
@@ -77,9 +84,9 @@ function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | nul
7784
if (variable.hide !== VariableHide.dontHide) {
7885
return null;
7986
}
80-
const fnLabelOrName = FNDashboard ? labelOrName.replace('druid_adhoc_filters', 'ad-hoc') : labelOrName;
8187

8288
const elementId = `var-${variable.id}`;
89+
8390
if (variable.description) {
8491
return (
8592
<Tooltip content={variable.description} placement={'bottom'}>
@@ -89,19 +96,48 @@ function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | nul
8996
data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
9097
htmlFor={elementId}
9198
>
92-
{fnLabelOrName}
99+
{labelOrName}
93100
</label>
94101
</Tooltip>
95102
);
96103
}
104+
97105
return (
98106
<label
99107
className="gf-form-label gf-form-label--variable"
100108
style={fnLabelStyle}
101109
data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
102110
htmlFor={elementId}
103111
>
104-
{fnLabelOrName}
112+
{labelOrName}
105113
</label>
106114
);
107115
}
116+
117+
118+
119+
const TRANSFORMERS = new Map<string | RegExp, string | ((value: string) => string)>([
120+
['druid_adhoc_filters', "ad-hoc filter" ],
121+
[/\_/g, " "],
122+
['lowerCase', (value: string) => value.toLowerCase() ],
123+
])
124+
125+
function transformFnLabel(label: unknown, value = {current: label}) {
126+
if (!isString(label)) {
127+
FnLoggerService.warn(null, "Label is not a string.", {label})
128+
return label
129+
}
130+
131+
if (!label) {
132+
return label
133+
}
134+
135+
136+
TRANSFORMERS.forEach(( replaceValue: string | ((value: string) => string), searchValue: string|RegExp) => {
137+
value.current = isFunction(replaceValue) ? replaceValue(value.current) :`${ value.current}`.replace(searchValue, replaceValue)
138+
})
139+
140+
return value.current
141+
142+
143+
}

0 commit comments

Comments
 (0)