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/split-value-button.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

Show a Group by field count as a number on a narrow screen
50 changes: 14 additions & 36 deletions packages/app/src/components/SearchGroupBy/SearchGroupBySelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -112,8 +105,6 @@ export function SearchGroupBySelect({
prev.includes(field) ? prev.filter(f => f !== field) : [...prev, field],
);

const label = triggerLabel(appliedFields);

return (
<Popover
opened={opened}
Expand All @@ -123,29 +114,16 @@ export function SearchGroupBySelect({
shadow="md"
width={420}
>
<div className={classes.control}>
<Text size="xs" fw={500} className={classes.label}>
Group by
</Text>
<Popover.Target>
<UnstyledButton
className={classes.target}
onClick={() => handleOpenChange(!opened)}
data-testid="search-group-by-button"
aria-label={`Group by ${label}`}
>
<Text
size="xs"
fw={600}
className={classes.value}
c={value == null ? 'dimmed' : undefined}
>
{label}
</Text>
<IconChevronDown size={14} />
</UnstyledButton>
</Popover.Target>
</div>
<Popover.Target>
<SplitValueButton
name="Group by"
summary={fieldListSummary(appliedFields)}
count={appliedFields.length}
dimmed={value == null}
onClick={() => handleOpenChange(!opened)}
data-testid="search-group-by-button"
/>
</Popover.Target>
<Popover.Dropdown p="xs" data-testid="search-group-by-dropdown">
<SegmentedControl
fullWidth
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// The label sits in a filled addon so it reads as the frame's name rather than
// something to press; only the value side is the button.
.control {
display: flex;
display: inline-flex;
align-items: center;
align-self: flex-start;
flex-shrink: 0;
width: fit-content;
max-width: 100%;
overflow: hidden;
background: var(--color-bg-surface);
border: 1px solid var(--color-border);
Expand All @@ -23,6 +26,7 @@
.target {
display: flex;
align-items: center;
flex: none;
gap: 6px;
height: 28px;
padding-inline: 8px;
Expand All @@ -35,6 +39,7 @@

// Long expressions truncate rather than pushing the row's other controls off.
.value {
flex: none;
overflow: hidden;
max-width: 220px;
text-overflow: ellipsis;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Group, Stack, Text } from '@mantine/core';
import type { Meta, StoryObj } from '@storybook/nextjs';

import { SplitValueButton } from './SplitValueButton';

const meta: Meta<typeof SplitValueButton> = {
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<typeof SplitValueButton>;

const noop = () => {};

export const WhenToUse: Story = {
render: () => (
<Stack gap="lg" maw={560}>
<Text size="sm">
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.
</Text>
<Stack gap="xs">
<Text size="xs" c="dimmed">
Group by splits the histogram. One series for each value of the field.
</Text>
<SplitValueButton
name="Group by"
summary="SeverityText"
count={1}
compact={false}
onClick={noop}
data-testid="story-group-by"
/>
</Stack>
<Stack gap="xs">
<Text size="xs" c="dimmed">
Columns chooses which columns the results table shows, and their
order.
</Text>
<SplitValueButton
name="Columns"
summary="4 fields"
count={4}
compact={false}
onClick={noop}
data-testid="story-columns"
/>
</Stack>
<Stack gap="xs">
<Text size="xs" c="dimmed">
Sort sets the order of the result rows. One field includes its
direction.
</Text>
<SplitValueButton
name="Sort"
summary="Timestamp desc"
count={1}
compact={false}
onClick={noop}
data-testid="story-sort"
/>
</Stack>
<Stack gap="xs">
<Text size="xs" c="dimmed">
On a narrow screen a count is only the number. One field keeps its
name, and an empty clause stays none.
</Text>
<Group gap="sm">
<SplitValueButton
name="Group by"
summary="SeverityText"
count={1}
compact
onClick={noop}
data-testid="story-group-by-compact"
/>
<SplitValueButton
name="Columns"
summary="4 fields"
count={4}
compact
onClick={noop}
data-testid="story-columns-compact"
/>
<SplitValueButton
name="Sort"
summary="none"
count={0}
dimmed
compact
onClick={noop}
data-testid="story-sort-compact"
/>
</Group>
</Stack>
</Stack>
),
};
Original file line number Diff line number Diff line change
@@ -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<HTMLButtonElement>;
} & Omit<ComponentPropsWithoutRef<'button'>, '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 (
<div className={classes.control}>
<Text size="xs" fw={500} className={classes.label}>
{name}
</Text>
<UnstyledButton
ref={ref}
{...others}
className={cx(classes.target, className)}
onClick={onClick}
title={title}
aria-label={`${name} ${summary}`}
data-testid={testId}
>
<Text
size="xs"
fw={600}
className={classes.value}
c={dimmed ? 'dimmed' : undefined}
>
{shown}
</Text>
<IconChevronDown size={14} />
</UnstyledButton>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -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(
<SplitValueButton
name="Columns"
summary="3 fields"
count={3}
compact={false}
onClick={() => {}}
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(
<SplitValueButton
name="Columns"
summary="3 fields"
count={3}
compact
onClick={() => {}}
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(
<SplitValueButton
name="Group by"
summary="SeverityText"
count={1}
compact
onClick={() => {}}
data-testid="group-by"
/>,
);
expect(screen.getByTestId('group-by')).toHaveTextContent('SeverityText');
});
});
Loading