Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-onions-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperdx/app": patch
---

ui: Extract FilterPill, add filter editor components
297 changes: 33 additions & 264 deletions packages/app/src/components/ActiveFilterPills.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types';
import {
ActionIcon,
Autocomplete,
Flex,
FlexProps,
Popover,
Text,
Tooltip,
} from '@mantine/core';
import { notifications } from '@mantine/notifications';
import {
IconCheck,
IconCopy,
IconFilter,
IconFilterX,
IconX,
} from '@tabler/icons-react';
import { Flex, FlexProps, Text } from '@mantine/core';

import { FilterPill, FilterValueEditor } from '@/components/FilterPill';
import { useGetKeyValues } from '@/hooks/useMetadata';
import type { FilterStateHook } from '@/searchFilters';
import { useFormatTime } from '@/useFormatTime';
import {
CLIPBOARD_ERROR_MESSAGE,
copyTextToClipboard,
} from '@/utils/clipboard';

const MAX_VISIBLE_PILLS = 8;
// Cap the value list fetched for the in-pill value picker.
Expand Down Expand Up @@ -91,21 +72,7 @@ function flattenFilters(
return pills;
}

const pillStyle = {
display: 'inline-flex',
alignItems: 'center',
gap: 4,
padding: '1px 6px',
borderRadius: 3,
fontSize: 11,
lineHeight: '18px',
cursor: 'default',
whiteSpace: 'nowrap' as const,
maxWidth: 260,
overflow: 'hidden',
};

function FilterPill({
function SearchFilterPill({
pill,
isInvalid,
invalidReason,
Expand All @@ -123,33 +90,10 @@ function FilterPill({
onReplaceValue: (value: string) => void;
}) {
const isExcluded = pill.type === 'excluded';
const operator = isExcluded ? ' != ' : pill.type === 'range' ? ': ' : ' = ';

// A range pill has no single value to copy or flip, and an unapplied filter
// (column missing on the active source) can only be removed. Both keep the
// plain remove-only pill; only included/excluded pills open the action menu.
// (column missing on the active source) can only be removed.
const isEditable = pill.type !== 'range' && !isInvalid;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — !isInvalid check repeats a guard that FilterPill already applies

FilterPill already sets isEditable = renderPopover != null && !isInvalid (FilterPill/FilterPill.tsx:968). In SearchFilterPill, isEditable only needs to be pill.type !== 'range' for deciding whether to pass renderPopover. Keep !isInvalid only in the enabled condition of useGetKeyValues, so the invalid-pill rule lives in one place.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

const polarityLabel = isExcluded ? 'Include' : 'Exclude';

const [opened, setOpened] = useState(false);
const [copied, setCopied] = useState(false);
// Draft of the value being typed in the picker. Free text is allowed (the
// field may not have this value in the sampled data yet), so we track what
// the user types and only commit it on submit/blur. It starts empty (the
// current value shows as a placeholder) so the full suggestion list is
// visible on open instead of being filtered down to just the current value.
const [draftValue, setDraftValue] = useState('');
const copyTimerRef = useRef<ReturnType<typeof setTimeout>>(undefined);

useEffect(() => {
return () => clearTimeout(copyTimerRef.current);
}, []);

// Reset the draft each time the menu opens so reopening on a different
// pill (or after a replace) starts from a clean, unfiltered list.
useEffect(() => {
setDraftValue('');
}, [pill.value, opened]);

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

// Fetch the field's values for the in-pill value picker, only while the
// menu is open (and never for range / not-applied pills).
const { data: keyValues, isFetching: isFetchingValues } = useGetKeyValues(
{
chartConfig: valueChartConfig,
Expand All @@ -172,209 +114,36 @@ function FilterPill({
},
{ enabled: opened && isEditable },
);
const valueOptions = useMemo(
() => Array.from(new Set([pill.value, ...(keyValues?.[0]?.value ?? [])])),
[keyValues, pill.value],
);

const label = pill.displayValue ?? pill.value;
const tooltipLabel = isInvalid
? (invalidReason ??
`Filter not applied: "${pill.field}" isn't a column on the current source. It will reapply if you switch back.`)
: `${pill.field}${operator}${label}`;

const showDangerAccent = isExcluded && !isInvalid;

// Commit the typed/picked value, but only when it actually differs from the
// current one (avoids a redundant query on blur with no change).
const commitValue = (value: string) => {
const trimmed = value.trim();
if (trimmed && trimmed !== pill.value) {
onReplaceValue(trimmed);
}
setOpened(false);
};

const handleCopy = async () => {
const ok = await copyTextToClipboard(pill.value);
if (!ok) {
notifications.show({ color: 'red', message: CLIPBOARD_ERROR_MESSAGE });
return;
}
setCopied(true);
clearTimeout(copyTimerRef.current);
copyTimerRef.current = setTimeout(() => setCopied(false), 1500);
};

const pillWithTooltip = (
<Tooltip
label={tooltipLabel}
openDelay={300}
multiline
maw={280}
disabled={isEditable && opened}
>
<span
data-testid={`active-filter-pill-${pill.field}`}
data-invalid={isInvalid ? 'true' : undefined}
onClick={isEditable ? () => setOpened(o => !o) : undefined}
style={{
...pillStyle,
cursor: isEditable ? 'pointer' : 'default',
backgroundColor: isInvalid
? 'transparent'
: isExcluded
? 'var(--mantine-color-red-light)'
: 'var(--color-bg-hover)',
border: isInvalid
? '1px dashed var(--color-border-emphasis)'
: '1px solid transparent',
opacity: isInvalid ? 0.75 : 1,
}}
>
<Text
span
size="xxs"
c="dimmed"
fw={500}
style={{
flexShrink: 0,
maxWidth: 100,
textDecoration: isInvalid ? 'line-through' : undefined,
}}
truncate="start"
>
{pill.field}
</Text>
<Text
span
size="xxs"
c="dimmed"
style={{
color: showDangerAccent
? 'var(--mantine-color-red-light-color)'
: undefined,
textDecoration: isInvalid ? 'line-through' : undefined,
}}
>
{operator}
</Text>
<Text
span
size="xxs"
fw={500}
truncate
style={{ textDecoration: isInvalid ? 'line-through' : undefined }}
>
{label}
</Text>
<ActionIcon
size={14}
variant="transparent"
color="gray"
onClick={e => {
// Keep the one-click remove without also toggling the action menu.
e.stopPropagation();
onRemove();
}}
style={{
flexShrink: 0,
marginLeft: 2,
color: showDangerAccent
? 'var(--mantine-color-red-light-color)'
: undefined,
}}
aria-label="Remove filter"
>
<IconX size={9} />
</ActionIcon>
</span>
</Tooltip>
);

if (!isEditable) {
return pillWithTooltip;
}
const valueOptions = useMemo(() => keyValues?.[0]?.value ?? [], [keyValues]);

return (
<Popover
position="bottom-start"
withArrow
shadow="md"
radius="sm"
opened={opened}
onChange={setOpened}
>
<Popover.Target>{pillWithTooltip}</Popover.Target>
<Popover.Dropdown p={6}>
<Autocomplete
size="xs"
w={220}
mb={6}
data={valueOptions}
value={draftValue}
onChange={setDraftValue}
// Picking a suggestion commits immediately.
onOptionSubmit={commitValue}
onKeyDown={e => {
if (e.key !== 'Enter') {
return;
}
// If the user is keyboard-navigating the dropdown, an option is
// highlighted (Mantine marks it with [data-combobox-selected]).
// Let the combobox handle Enter natively so it submits that option
// via onOptionSubmit. Only commit free text when no option is
// highlighted, so a typed value not in the list still applies.
// Scope the lookup to this input's own listbox (via aria-controls)
// rather than the whole document, so another open combobox on the
// page can't make us swallow Enter here.
const listId = e.currentTarget.getAttribute('aria-controls');
const list = listId ? document.getElementById(listId) : null;
const hasHighlightedOption = !!list?.querySelector(
'[data-combobox-selected]',
);
if (!hasHighlightedOption) {
e.preventDefault();
commitValue(draftValue);
}
}}
comboboxProps={{ withinPortal: false }}
placeholder={isFetchingValues ? 'Loading values...' : pill.value}
aria-label="Change filter value"
/>
<Flex gap={4} align="center">
<Tooltip label={copied ? 'Copied' : 'Copy value'}>
<ActionIcon
size="sm"
variant="subtle"
color="gray"
onClick={handleCopy}
aria-label="Copy value"
>
{copied ? <IconCheck size={14} /> : <IconCopy size={14} />}
</ActionIcon>
</Tooltip>
<Tooltip label={polarityLabel}>
<ActionIcon
size="sm"
variant="subtle"
color="gray"
onClick={() => {
onTogglePolarity();
setOpened(false);
}}
aria-label={polarityLabel}
>
{isExcluded ? (
<IconFilter size={14} />
) : (
<IconFilterX size={14} />
)}
</ActionIcon>
</Tooltip>
</Flex>
</Popover.Dropdown>
</Popover>
<FilterPill
field={pill.field}
operator={isExcluded ? '!=' : pill.type === 'range' ? ':' : '='}
value={pill.value}
displayValue={pill.displayValue}
isExcluded={isExcluded}
isInvalid={isInvalid}
invalidReason={invalidReason}
onRemove={onRemove}
onOpenedChange={setOpened}
renderPopover={
isEditable
? close => (
<FilterValueEditor
value={pill.value}
valueOptions={valueOptions}
isLoadingValues={isFetchingValues}
isExcluded={isExcluded}
onReplaceValue={onReplaceValue}
onTogglePolarity={onTogglePolarity}
onDone={close}
/>
)
: undefined
}
data-testid={`active-filter-pill-${pill.field}`}
/>
);
}

Expand Down Expand Up @@ -505,7 +274,7 @@ export const ActiveFilterPills = memo(function ActiveFilterPills({
{visiblePills.map((pill, i) => {
const isInvalid = invalidFields?.has(pill.field) ?? false;
return (
<FilterPill
<SearchFilterPill
key={`${pill.field}-${pill.type}-${pill.value}-${i}`}
pill={pill}
isInvalid={isInvalid}
Expand Down
Loading
Loading