-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathGroupByVariable.tsx
107 lines (98 loc) · 2.49 KB
/
GroupByVariable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, { useEffect, useState } from 'react';
import { ControlsLayout, GroupByVariable as GroupByVariableObject } from '@grafana/scenes';
import { useSceneContext } from '../hooks/hooks';
import { VariableProps } from './types';
import { AdHocVariableFilter, MetricFindValue } from '@grafana/data';
import { getTagKeysProvider } from '@grafana/scenes/src/variables/groupby/GroupByVariable';
import { DataSourceRef } from '@grafana/schema';
export interface GroupByVariableProps extends VariableProps {
datasource: DataSourceRef | null;
children: React.ReactNode;
defaultOptions?: MetricFindValue[];
baseFilters?: AdHocVariableFilter[];
readOnly?: boolean;
layout?: ControlsLayout;
applyMode?: 'auto' | 'manual';
tagKeyRegexFilter?: RegExp;
getTagKeysProvider?: getTagKeysProvider;
allowCustomValue?: boolean;
}
export function GroupByVariable({
name,
label,
hide,
initialValue,
datasource,
defaultOptions,
baseFilters,
readOnly,
layout,
applyMode = 'auto',
tagKeyRegexFilter,
getTagKeysProvider,
allowCustomValue,
children,
}: GroupByVariableProps): React.ReactNode {
const scene = useSceneContext();
const [variableAdded, setVariableAdded] = useState<boolean>();
let variable: GroupByVariableObject | undefined = scene.findVariable(name);
if (initialValue && !Array.isArray(initialValue)) {
initialValue = [initialValue];
}
if (!variable) {
variable = new GroupByVariableObject({
name,
label,
datasource,
value: initialValue,
hide,
defaultOptions,
baseFilters,
readOnly,
layout,
applyMode,
tagKeyRegexFilter,
getTagKeysProvider,
allowCustomValue,
});
}
useEffect(() => {
const removeFn = scene.addVariable(variable);
setVariableAdded(true);
return removeFn;
}, [variable, scene, name]);
useEffect(() => {
variable?.setState({
label,
datasource,
hide,
defaultOptions,
baseFilters,
readOnly,
layout,
applyMode,
tagKeyRegexFilter,
getTagKeysProvider,
allowCustomValue,
});
}, [
allowCustomValue,
applyMode,
baseFilters,
datasource,
defaultOptions,
getTagKeysProvider,
hide,
initialValue,
label,
layout,
readOnly,
tagKeyRegexFilter,
variable,
]);
// Need to block child rendering until the variable is added so that child components like RVariableSelect find the variable
if (!variableAdded) {
return null;
}
return children;
}