diff --git a/.changeset/split-value-button.md b/.changeset/split-value-button.md new file mode 100644 index 0000000000..4c6d3339f6 --- /dev/null +++ b/.changeset/split-value-button.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +Show a Group by field count as a number on a narrow screen diff --git a/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx b/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx index 6794ad77fb..d90cf8d5d6 100644 --- a/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx +++ b/packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx @@ -6,12 +6,13 @@ import { Group, Popover, SegmentedControl, - Text, TextInput, - UnstyledButton, } from '@mantine/core'; -import { IconChevronDown } from '@tabler/icons-react'; +import { + fieldListSummary, + SplitValueButton, +} from '@/components/SplitValueButton/SplitValueButton'; import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; import { GroupByFieldList } from './GroupByFieldList'; @@ -23,16 +24,8 @@ import { } from './searchGroupBy'; import { useGroupByKeyOptions } from './useGroupByKeyOptions'; -import classes from './SearchGroupBySelect.module.scss'; - type Mode = 'fields' | 'sql'; -function triggerLabel(fields: string[]) { - if (fields.length === 0) return 'none'; - if (fields.length === 1) return fields[0]; - return `${fields.length} fields`; -} - export function SearchGroupBySelect({ source, value, @@ -112,8 +105,6 @@ export function SearchGroupBySelect({ prev.includes(field) ? prev.filter(f => f !== field) : [...prev, field], ); - const label = triggerLabel(appliedFields); - return ( -
- - Group by - - - handleOpenChange(!opened)} - data-testid="search-group-by-button" - aria-label={`Group by ${label}`} - > - - {label} - - - - -
+ + handleOpenChange(!opened)} + data-testid="search-group-by-button" + /> + = { + title: 'Components/SplitValueButton', + component: SplitValueButton, + parameters: { + layout: 'padded', + docs: { + description: { + component: + 'The shared trigger for Group by, Columns, and Sort. Use it when a search control shows the current value of a clause and opens an editor. Do not use it for an action such as Save, Run, or Apply.', + }, + }, + }, +}; + +export default meta; +type Story = StoryObj; + +const noop = () => {}; + +export const WhenToUse: Story = { + render: () => ( + + + Use this button for a search clause whose value is one field, or a count + of fields. The name stays fixed. The value is what you press. + + + + Group by splits the histogram. One series for each value of the field. + + + + + + Columns chooses which columns the results table shows, and their + order. + + + + + + Sort sets the order of the result rows. One field includes its + direction. + + + + + + On a narrow screen a count is only the number. One field keeps its + name, and an empty clause stays none. + + + + + + + + + ), +}; diff --git a/packages/app/src/components/SplitValueButton/SplitValueButton.tsx b/packages/app/src/components/SplitValueButton/SplitValueButton.tsx new file mode 100644 index 0000000000..5815600b58 --- /dev/null +++ b/packages/app/src/components/SplitValueButton/SplitValueButton.tsx @@ -0,0 +1,91 @@ +import { ComponentPropsWithoutRef, Ref } from 'react'; +import cx from 'classnames'; +import { Text, UnstyledButton } from '@mantine/core'; +import { IconChevronDown } from '@tabler/icons-react'; + +import { useCompactViewport } from '@/components/CompactViewport/useCompactViewport'; + +import classes from './SplitValueButton.module.scss'; + +/** One field by name, several as a count, or none. */ +export function fieldListSummary(fields: string[]): string { + if (fields.length === 0) return 'none'; + if (fields.length === 1) return fields[0]; + return `${fields.length} fields`; +} + +type SplitValueButtonProps = { + /** Fixed name, such as Group by, Columns, or Sort. */ + name: string; + /** Desktop value: a field name, "3 fields", or "none". */ + summary: string; + /** How many fields the summary counts. Drives the narrow-screen number. */ + count: number; + /** The clause is still the source default, so the value is quiet. */ + dimmed?: boolean; + /** Full clause, shown on hover when the value is only a summary. */ + title?: string; + onClick: () => void; + /** Story and test override. Otherwise follows the compact viewport. */ + compact?: boolean; + 'data-testid': string; + ref?: Ref; +} & Omit, 'onClick' | 'title'>; + +/** + * Split button for a search clause: a fixed name, then the current value. + * + * Use it for Group by, Columns, and Sort. The name says which clause it is. + * The value is one field, or "3 fields" when there are several. On a narrow + * screen that count is only the number, so the row still fits. + * + * Group by splits the histogram, one series per value. Columns chooses and + * orders the results table. Sort sets the row order, and a single field + * includes its direction. + * + * Do not use it for an action such as Save, Run, or Apply. + */ +export function SplitValueButton({ + name, + summary, + count, + dimmed, + title, + onClick, + compact, + 'data-testid': testId, + className, + ref, + ...others +}: SplitValueButtonProps) { + const viewportCompact = useCompactViewport(); + const isCompact = compact ?? viewportCompact; + const shown = isCompact && count > 1 ? String(count) : summary; + + return ( +
+ + {name} + + + + {shown} + + + +
+ ); +} diff --git a/packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx b/packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx new file mode 100644 index 0000000000..9430b1f9fd --- /dev/null +++ b/packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx @@ -0,0 +1,53 @@ +import { screen } from '@testing-library/react'; + +import { SplitValueButton } from '@/components/SplitValueButton/SplitValueButton'; + +describe('SplitValueButton', () => { + it('shows a field count as words on a wide screen', () => { + renderWithMantine( + {}} + data-testid="columns" + />, + ); + expect(screen.getByTestId('columns')).toHaveTextContent('3 fields'); + expect(screen.getByTestId('columns')).toHaveAccessibleName( + 'Columns 3 fields', + ); + }); + + it('shows only the number on a narrow screen', () => { + renderWithMantine( + {}} + data-testid="columns" + />, + ); + expect(screen.getByTestId('columns')).toHaveTextContent(/^3$/); + expect(screen.getByTestId('columns')).toHaveAccessibleName( + 'Columns 3 fields', + ); + }); + + it('keeps a single field name on a narrow screen', () => { + renderWithMantine( + {}} + data-testid="group-by" + />, + ); + expect(screen.getByTestId('group-by')).toHaveTextContent('SeverityText'); + }); +});