1
1
import { css } from '@emotion/css' ;
2
+ import { isString , isFunction } from 'lodash' ;
2
3
import React , { CSSProperties , FunctionComponent , PropsWithChildren , ReactElement , useMemo } from 'react' ;
3
4
// eslint-disable-next-line no-restricted-imports
4
5
import { useSelector } from 'react-redux' ;
@@ -10,6 +11,7 @@ import type { StoreState } from 'app/types';
10
11
11
12
import { variableAdapters } from '../adapters' ;
12
13
import { VariableHide , VariableModel } from '../types' ;
14
+ import { FnLoggerService } from 'app/fn_logger' ;
13
15
14
16
interface Props {
15
17
variable : VariableModel ;
@@ -58,9 +60,14 @@ const COMMON_PICKER_LABEL_STYLE: CSSProperties = {
58
60
} ;
59
61
60
62
function PickerLabel ( { variable } : PropsWithChildren < Props > ) : ReactElement | null {
61
- const labelOrName = useMemo ( ( ) => variable . label || variable . name , [ variable ] ) ;
62
63
const { FNDashboard, mode } = useSelector < StoreState , FnGlobalState > ( ( { fnGlobalState } ) => fnGlobalState ) ;
63
64
65
+ const labelOrName = useMemo ( ( ) => {
66
+ const value = variable . label || variable . name ;
67
+
68
+ return FNDashboard ? transformFnLabel ( value ) : value ;
69
+ } , [ variable , FNDashboard ] ) ;
70
+
64
71
const fnLabelStyle = useMemo (
65
72
( ) => ( {
66
73
...COMMON_PICKER_LABEL_STYLE ,
@@ -77,9 +84,9 @@ function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | nul
77
84
if ( variable . hide !== VariableHide . dontHide ) {
78
85
return null ;
79
86
}
80
- const fnLabelOrName = FNDashboard ? labelOrName . replace ( 'druid_adhoc_filters' , 'ad-hoc' ) : labelOrName ;
81
87
82
88
const elementId = `var-${ variable . id } ` ;
89
+
83
90
if ( variable . description ) {
84
91
return (
85
92
< Tooltip content = { variable . description } placement = { 'bottom' } >
@@ -89,19 +96,48 @@ function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | nul
89
96
data-testid = { selectors . pages . Dashboard . SubMenu . submenuItemLabels ( labelOrName ) }
90
97
htmlFor = { elementId }
91
98
>
92
- { fnLabelOrName }
99
+ { labelOrName }
93
100
</ label >
94
101
</ Tooltip >
95
102
) ;
96
103
}
104
+
97
105
return (
98
106
< label
99
107
className = "gf-form-label gf-form-label--variable"
100
108
style = { fnLabelStyle }
101
109
data-testid = { selectors . pages . Dashboard . SubMenu . submenuItemLabels ( labelOrName ) }
102
110
htmlFor = { elementId }
103
111
>
104
- { fnLabelOrName }
112
+ { labelOrName }
105
113
</ label >
106
114
) ;
107
115
}
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