-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathDashboardFilters.tsx
More file actions
154 lines (144 loc) · 4.99 KB
/
Copy pathDashboardFilters.tsx
File metadata and controls
154 lines (144 loc) · 4.99 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useState } from 'react';
import { FilterState } from '@hyperdx/common-utils/dist/filters';
import { DashboardFilter } from '@hyperdx/common-utils/dist/types';
import { Group, Stack, Text, Tooltip } from '@mantine/core';
import { IconAlertTriangle, IconHelp, IconRefresh } from '@tabler/icons-react';
import { FilterLinkToggle } from './components/FilterLinkToggle';
import { VirtualMultiSelect } from './components/VirtualMultiSelect/VirtualMultiSelect';
import { useDashboardFilterValues } from './hooks/useDashboardFilterValues';
interface DashboardFilterSelectProps {
filter: DashboardFilter;
onChange: (values: string[]) => void;
value: string[];
values?: string[];
isLoading?: boolean;
isError?: boolean;
}
const getAppliesToTooltip = (filter: DashboardFilter) => {
const count = filter.appliesToSourceIds?.length ?? 0;
if (count === 0) return 'Applies to all sources';
return `Applies to ${count} source${count === 1 ? '' : 's'}`;
};
const DashboardFilterSelect = ({
filter,
onChange,
value,
values,
isLoading,
isError,
}: DashboardFilterSelectProps) => {
const sortedValues = values?.toSorted() || [];
const tooltipText = getAppliesToTooltip(filter);
return (
<Stack gap={2}>
<Group gap={4} align="center" wrap="nowrap">
<Text size="xs" c="dimmed">
{filter.name}
</Text>
<Tooltip label={tooltipText} withinPortal>
<IconHelp
size={12}
color="var(--color-text-muted)"
data-testid={`dashboard-filter-help-${filter.name}`}
/>
</Tooltip>
{isError && (
<Tooltip
label="Filter values query failed. The filter's query may be invalid."
withinPortal
>
<IconAlertTriangle
size={12}
color="var(--color-text-danger)"
data-testid={`dashboard-filter-error-${filter.name}`}
/>
</Tooltip>
)}
</Group>
<div style={{ width: 250 }}>
<VirtualMultiSelect
placeholder={value.length === 0 ? filter.name : undefined}
values={value}
data={sortedValues}
// Surface loading as a dropdown hint rather than disabling the control,
// so a completed/empty/failed query stays interactive and the user can
// still clear or adjust the selection.
loading={isLoading}
onChange={onChange}
data-testid={`dashboard-filter-select-${filter.name}`}
/>
</div>
</Stack>
);
};
interface DashboardFilterProps {
filters: DashboardFilter[];
filterValues: FilterState;
onSetFilterValue: (expression: string, values: string[]) => void;
dateRange: [Date, Date];
}
const DashboardFilters = ({
filters,
dateRange,
filterValues,
onSetFilterValue,
}: DashboardFilterProps) => {
// "Link" mode (opt-in, off by default): each dropdown's values are narrowed by
// the others' selections. Off by default because contingent value lookups
// can't use the cheap per-key rollups and are more expensive at scale. When
// on, all of a source's facets are computed in a single groupUniqArrayIf scan.
const [linked, setLinked] = useState(false);
const {
data: filterValuesById,
erroredFilterIds,
isFetching,
} = useDashboardFilterValues({
filters,
dateRange,
// Only narrow by sibling selections when linked.
filterValues: linked ? filterValues : {},
});
return (
<Group align="start">
{Object.values(filters).map(filter => {
const queriedFilterValues = filterValuesById?.get(filter.id);
const included = filterValues[filter.expression]?.included;
const selectedValues = included
? Array.from(included).map(v => v.toString())
: [];
// Fall back to the hook-level fetching state only until this filter's
// query has produced an entry; once it has (even with empty values),
// honor its own loading flag.
const isLoadingValues = queriedFilterValues
? queriedFilterValues.isLoading
: isFetching;
return (
<DashboardFilterSelect
key={filter.id}
filter={filter}
isLoading={isLoadingValues}
isError={erroredFilterIds?.has(filter.id) ?? false}
onChange={values => onSetFilterValue(filter.expression, values)}
values={queriedFilterValues?.values}
value={selectedValues}
/>
);
})}
{filters.length >= 2 && (
<Stack gap={2} justify="flex-end">
{/* Spacer to align the toggle with the inputs (filters have a label row above). */}
<Text size="xs" c="transparent" aria-hidden>
</Text>
<FilterLinkToggle
linked={linked}
onChange={setLinked}
data-testid="dashboard-filters-link-toggle"
/>
</Stack>
)}
{isFetching && <IconRefresh className="spin-animate" size={12} />}
</Group>
);
};
export default DashboardFilters;