Skip to content

Commit e6320ae

Browse files
Merge branch 'main' into dependabot/npm_and_yarn/proxy-addr-2.0.8
2 parents c3eb9df + daccc74 commit e6320ae

15 files changed

Lines changed: 1404 additions & 422 deletions

‎.changeset/violet-onions-tickle.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/app": patch
3+
---
4+
5+
ui: Extract FilterPill, add filter editor components

‎packages/app/src/components/ActiveFilterPills.tsx‎

Lines changed: 33 additions & 264 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
22
import type { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types';
3-
import {
4-
ActionIcon,
5-
Autocomplete,
6-
Flex,
7-
FlexProps,
8-
Popover,
9-
Text,
10-
Tooltip,
11-
} from '@mantine/core';
12-
import { notifications } from '@mantine/notifications';
13-
import {
14-
IconCheck,
15-
IconCopy,
16-
IconFilter,
17-
IconFilterX,
18-
IconX,
19-
} from '@tabler/icons-react';
3+
import { Flex, FlexProps, Text } from '@mantine/core';
204

5+
import { FilterPill, FilterValueEditor } from '@/components/FilterPill';
216
import { useGetKeyValues } from '@/hooks/useMetadata';
227
import type { FilterStateHook } from '@/searchFilters';
238
import { useFormatTime } from '@/useFormatTime';
24-
import {
25-
CLIPBOARD_ERROR_MESSAGE,
26-
copyTextToClipboard,
27-
} from '@/utils/clipboard';
289

2910
const MAX_VISIBLE_PILLS = 8;
3011
// Cap the value list fetched for the in-pill value picker.
@@ -91,21 +72,7 @@ function flattenFilters(
9172
return pills;
9273
}
9374

94-
const pillStyle = {
95-
display: 'inline-flex',
96-
alignItems: 'center',
97-
gap: 4,
98-
padding: '1px 6px',
99-
borderRadius: 3,
100-
fontSize: 11,
101-
lineHeight: '18px',
102-
cursor: 'default',
103-
whiteSpace: 'nowrap' as const,
104-
maxWidth: 260,
105-
overflow: 'hidden',
106-
};
107-
108-
function FilterPill({
75+
function SearchFilterPill({
10976
pill,
11077
isInvalid,
11178
invalidReason,
@@ -123,33 +90,10 @@ function FilterPill({
12390
onReplaceValue: (value: string) => void;
12491
}) {
12592
const isExcluded = pill.type === 'excluded';
126-
const operator = isExcluded ? ' != ' : pill.type === 'range' ? ': ' : ' = ';
127-
12893
// A range pill has no single value to copy or flip, and an unapplied filter
129-
// (column missing on the active source) can only be removed. Both keep the
130-
// plain remove-only pill; only included/excluded pills open the action menu.
94+
// (column missing on the active source) can only be removed.
13195
const isEditable = pill.type !== 'range' && !isInvalid;
132-
const polarityLabel = isExcluded ? 'Include' : 'Exclude';
133-
13496
const [opened, setOpened] = useState(false);
135-
const [copied, setCopied] = useState(false);
136-
// Draft of the value being typed in the picker. Free text is allowed (the
137-
// field may not have this value in the sampled data yet), so we track what
138-
// the user types and only commit it on submit/blur. It starts empty (the
139-
// current value shows as a placeholder) so the full suggestion list is
140-
// visible on open instead of being filtered down to just the current value.
141-
const [draftValue, setDraftValue] = useState('');
142-
const copyTimerRef = useRef<ReturnType<typeof setTimeout>>(undefined);
143-
144-
useEffect(() => {
145-
return () => clearTimeout(copyTimerRef.current);
146-
}, []);
147-
148-
// Reset the draft each time the menu opens so reopening on a different
149-
// pill (or after a replace) starts from a clean, unfiltered list.
150-
useEffect(() => {
151-
setDraftValue('');
152-
}, [pill.value, opened]);
15397

15498
// The picker lists values to switch this pill to, so it must not be scoped
15599
// by the active query or by the pill's own filter. Reusing chartConfig
@@ -162,8 +106,6 @@ function FilterPill({
162106
[chartConfig],
163107
);
164108

165-
// Fetch the field's values for the in-pill value picker, only while the
166-
// menu is open (and never for range / not-applied pills).
167109
const { data: keyValues, isFetching: isFetchingValues } = useGetKeyValues(
168110
{
169111
chartConfig: valueChartConfig,
@@ -172,209 +114,36 @@ function FilterPill({
172114
},
173115
{ enabled: opened && isEditable },
174116
);
175-
const valueOptions = useMemo(
176-
() => Array.from(new Set([pill.value, ...(keyValues?.[0]?.value ?? [])])),
177-
[keyValues, pill.value],
178-
);
179-
180-
const label = pill.displayValue ?? pill.value;
181-
const tooltipLabel = isInvalid
182-
? (invalidReason ??
183-
`Filter not applied: "${pill.field}" isn't a column on the current source. It will reapply if you switch back.`)
184-
: `${pill.field}${operator}${label}`;
185-
186-
const showDangerAccent = isExcluded && !isInvalid;
187-
188-
// Commit the typed/picked value, but only when it actually differs from the
189-
// current one (avoids a redundant query on blur with no change).
190-
const commitValue = (value: string) => {
191-
const trimmed = value.trim();
192-
if (trimmed && trimmed !== pill.value) {
193-
onReplaceValue(trimmed);
194-
}
195-
setOpened(false);
196-
};
197-
198-
const handleCopy = async () => {
199-
const ok = await copyTextToClipboard(pill.value);
200-
if (!ok) {
201-
notifications.show({ color: 'red', message: CLIPBOARD_ERROR_MESSAGE });
202-
return;
203-
}
204-
setCopied(true);
205-
clearTimeout(copyTimerRef.current);
206-
copyTimerRef.current = setTimeout(() => setCopied(false), 1500);
207-
};
208-
209-
const pillWithTooltip = (
210-
<Tooltip
211-
label={tooltipLabel}
212-
openDelay={300}
213-
multiline
214-
maw={280}
215-
disabled={isEditable && opened}
216-
>
217-
<span
218-
data-testid={`active-filter-pill-${pill.field}`}
219-
data-invalid={isInvalid ? 'true' : undefined}
220-
onClick={isEditable ? () => setOpened(o => !o) : undefined}
221-
style={{
222-
...pillStyle,
223-
cursor: isEditable ? 'pointer' : 'default',
224-
backgroundColor: isInvalid
225-
? 'transparent'
226-
: isExcluded
227-
? 'var(--mantine-color-red-light)'
228-
: 'var(--color-bg-hover)',
229-
border: isInvalid
230-
? '1px dashed var(--color-border-emphasis)'
231-
: '1px solid transparent',
232-
opacity: isInvalid ? 0.75 : 1,
233-
}}
234-
>
235-
<Text
236-
span
237-
size="xxs"
238-
c="dimmed"
239-
fw={500}
240-
style={{
241-
flexShrink: 0,
242-
maxWidth: 100,
243-
textDecoration: isInvalid ? 'line-through' : undefined,
244-
}}
245-
truncate="start"
246-
>
247-
{pill.field}
248-
</Text>
249-
<Text
250-
span
251-
size="xxs"
252-
c="dimmed"
253-
style={{
254-
color: showDangerAccent
255-
? 'var(--mantine-color-red-light-color)'
256-
: undefined,
257-
textDecoration: isInvalid ? 'line-through' : undefined,
258-
}}
259-
>
260-
{operator}
261-
</Text>
262-
<Text
263-
span
264-
size="xxs"
265-
fw={500}
266-
truncate
267-
style={{ textDecoration: isInvalid ? 'line-through' : undefined }}
268-
>
269-
{label}
270-
</Text>
271-
<ActionIcon
272-
size={14}
273-
variant="transparent"
274-
color="gray"
275-
onClick={e => {
276-
// Keep the one-click remove without also toggling the action menu.
277-
e.stopPropagation();
278-
onRemove();
279-
}}
280-
style={{
281-
flexShrink: 0,
282-
marginLeft: 2,
283-
color: showDangerAccent
284-
? 'var(--mantine-color-red-light-color)'
285-
: undefined,
286-
}}
287-
aria-label="Remove filter"
288-
>
289-
<IconX size={9} />
290-
</ActionIcon>
291-
</span>
292-
</Tooltip>
293-
);
294-
295-
if (!isEditable) {
296-
return pillWithTooltip;
297-
}
117+
const valueOptions = useMemo(() => keyValues?.[0]?.value ?? [], [keyValues]);
298118

299119
return (
300-
<Popover
301-
position="bottom-start"
302-
withArrow
303-
shadow="md"
304-
radius="sm"
305-
opened={opened}
306-
onChange={setOpened}
307-
>
308-
<Popover.Target>{pillWithTooltip}</Popover.Target>
309-
<Popover.Dropdown p={6}>
310-
<Autocomplete
311-
size="xs"
312-
w={220}
313-
mb={6}
314-
data={valueOptions}
315-
value={draftValue}
316-
onChange={setDraftValue}
317-
// Picking a suggestion commits immediately.
318-
onOptionSubmit={commitValue}
319-
onKeyDown={e => {
320-
if (e.key !== 'Enter') {
321-
return;
322-
}
323-
// If the user is keyboard-navigating the dropdown, an option is
324-
// highlighted (Mantine marks it with [data-combobox-selected]).
325-
// Let the combobox handle Enter natively so it submits that option
326-
// via onOptionSubmit. Only commit free text when no option is
327-
// highlighted, so a typed value not in the list still applies.
328-
// Scope the lookup to this input's own listbox (via aria-controls)
329-
// rather than the whole document, so another open combobox on the
330-
// page can't make us swallow Enter here.
331-
const listId = e.currentTarget.getAttribute('aria-controls');
332-
const list = listId ? document.getElementById(listId) : null;
333-
const hasHighlightedOption = !!list?.querySelector(
334-
'[data-combobox-selected]',
335-
);
336-
if (!hasHighlightedOption) {
337-
e.preventDefault();
338-
commitValue(draftValue);
339-
}
340-
}}
341-
comboboxProps={{ withinPortal: false }}
342-
placeholder={isFetchingValues ? 'Loading values...' : pill.value}
343-
aria-label="Change filter value"
344-
/>
345-
<Flex gap={4} align="center">
346-
<Tooltip label={copied ? 'Copied' : 'Copy value'}>
347-
<ActionIcon
348-
size="sm"
349-
variant="subtle"
350-
color="gray"
351-
onClick={handleCopy}
352-
aria-label="Copy value"
353-
>
354-
{copied ? <IconCheck size={14} /> : <IconCopy size={14} />}
355-
</ActionIcon>
356-
</Tooltip>
357-
<Tooltip label={polarityLabel}>
358-
<ActionIcon
359-
size="sm"
360-
variant="subtle"
361-
color="gray"
362-
onClick={() => {
363-
onTogglePolarity();
364-
setOpened(false);
365-
}}
366-
aria-label={polarityLabel}
367-
>
368-
{isExcluded ? (
369-
<IconFilter size={14} />
370-
) : (
371-
<IconFilterX size={14} />
372-
)}
373-
</ActionIcon>
374-
</Tooltip>
375-
</Flex>
376-
</Popover.Dropdown>
377-
</Popover>
120+
<FilterPill
121+
field={pill.field}
122+
operator={isExcluded ? '!=' : pill.type === 'range' ? ':' : '='}
123+
value={pill.value}
124+
displayValue={pill.displayValue}
125+
isExcluded={isExcluded}
126+
isInvalid={isInvalid}
127+
invalidReason={invalidReason}
128+
onRemove={onRemove}
129+
onOpenedChange={setOpened}
130+
renderPopover={
131+
isEditable
132+
? close => (
133+
<FilterValueEditor
134+
value={pill.value}
135+
valueOptions={valueOptions}
136+
isLoadingValues={isFetchingValues}
137+
isExcluded={isExcluded}
138+
onReplaceValue={onReplaceValue}
139+
onTogglePolarity={onTogglePolarity}
140+
onDone={close}
141+
/>
142+
)
143+
: undefined
144+
}
145+
data-testid={`active-filter-pill-${pill.field}`}
146+
/>
378147
);
379148
}
380149

@@ -505,7 +274,7 @@ export const ActiveFilterPills = memo(function ActiveFilterPills({
505274
{visiblePills.map((pill, i) => {
506275
const isInvalid = invalidFields?.has(pill.field) ?? false;
507276
return (
508-
<FilterPill
277+
<SearchFilterPill
509278
key={`${pill.field}-${pill.type}-${pill.value}-${i}`}
510279
pill={pill}
511280
isInvalid={isInvalid}

0 commit comments

Comments
 (0)