Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/dashboard-live-refresh-interval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/app': patch
---

feat: Pick the dashboard live mode refresh interval, from 30s up to the time
range's default
44 changes: 25 additions & 19 deletions packages/app/src/DBDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ import {
type DragHandleProps,
} from '@/components/DashboardDndContext';
import { DashboardKioskHeader } from '@/components/DashboardKioskHeader';
import { DashboardLiveButton } from '@/components/DashboardLiveButton';
import DashboardTableOfContents from '@/components/DashboardTableOfContents';
import EditTimeChartForm from '@/components/DBEditTimeChartForm';
import DBNumberChart from '@/components/DBNumberChart';
Expand Down Expand Up @@ -2131,6 +2132,9 @@ function DBDashboardPage({
};

const [isLive, setIsLive] = useState(false);
const [selectedRefreshInterval, setSelectedRefreshInterval] = useState<
Granularity | undefined
>();

const { control, setValue, getValues, handleSubmit } = useForm<{
granularity: SQLInterval | 'auto';
Expand Down Expand Up @@ -2173,12 +2177,24 @@ function DBDashboardPage({
isRefreshEnabled,
manualRefreshCooloff,
refresh,
refreshInterval,
refreshIntervalOptions,
isAutoRefreshInterval,
} = useDashboardRefresh({
searchedTimeRange,
onTimeRangeSelect,
isLive: isLive || isKioskMode,
selectedRefreshInterval,
});

const onRefreshIntervalChange = useCallback(
(interval: Granularity | undefined) => {
setSelectedRefreshInterval(interval);
setIsLive(true);
},
[],
);

const onSubmit = useCallback(() => {
onSearch(displayedTimeInputValue);
handleSubmit(data => {
Expand Down Expand Up @@ -3290,25 +3306,15 @@ function DBDashboardPage({
}}
/>
<GranularityPickerControlled control={control} name="granularity" />
<Tooltip
withArrow
label={
isRefreshEnabled
? `Auto-refreshing with ${granularityOverride} interval`
: 'Enable auto-refresh'
}
fz="xs"
color="gray"
>
<Button
onClick={() => setIsLive(prev => !prev)}
size="sm"
variant={isLive ? 'primary' : 'secondary'}
title={isLive ? 'Disable auto-refresh' : 'Enable auto-refresh'}
>
Live
</Button>
</Tooltip>
<DashboardLiveButton
isLive={isLive}
onToggleLive={() => setIsLive(prev => !prev)}
refreshInterval={refreshInterval}
isAutoRefreshInterval={isAutoRefreshInterval}
defaultRefreshInterval={granularityOverride}
refreshIntervalOptions={refreshIntervalOptions}
onRefreshIntervalChange={onRefreshIntervalChange}
/>
<Tooltip withArrow label="Refresh dashboard" fz="xs" color="gray">
<ActionIcon
onClick={refresh}
Expand Down
107 changes: 107 additions & 0 deletions packages/app/src/components/DashboardLiveButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Granularity } from '@hyperdx/common-utils/dist/core/utils';
import { mapGranularityToExternalFormat } from '@hyperdx/common-utils/dist/externalSource';
import { Box, Button, Menu, Tooltip } from '@mantine/core';
import { IconCheck, IconChevronDown } from '@tabler/icons-react';

function IntervalItem({
label,
checked,
onClick,
}: {
label: string;
checked: boolean;
onClick: () => void;
}) {
return (
<Menu.Item

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.

🟡 P2 — Screen readers can't tell which refresh interval is selected

The selected item is marked only by a visual IconCheck and a data-checked attribute, and assistive tech reads neither. Give each Menu.Item role="menuitemradio" and aria-checked={checked}, and have the tests assert aria-checked instead of data-checked.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tried this: Mantine's Menu.Item sets role: "menuitem" after spreading props, so role="menuitemradio" is overridden, and aria-checked isn't valid on a plain menuitem. Fixing it means replacing Menu.Item with a custom item, which is more than this PR should take on; leaving it for a maintainer to decide.

data-checked={checked}
onClick={onClick}
leftSection={checked ? <IconCheck size={14} /> : <Box w={14} />}
>
{label}
</Menu.Item>
);
}

export function DashboardLiveButton({
isLive,
onToggleLive,
refreshInterval,
isAutoRefreshInterval,
defaultRefreshInterval,
refreshIntervalOptions,
onRefreshIntervalChange,
}: {
isLive: boolean;
onToggleLive: () => void;
refreshInterval: Granularity;
isAutoRefreshInterval: boolean;
defaultRefreshInterval: Granularity;
refreshIntervalOptions: Granularity[];
/** Undefined selects auto, which follows the date range's default. */
onRefreshIntervalChange: (interval: Granularity | undefined) => void;
}) {
const variant = isLive ? 'primary' : 'secondary';
const intervalLabel = mapGranularityToExternalFormat(refreshInterval);
const autoLabel = `Auto (${mapGranularityToExternalFormat(defaultRefreshInterval)})`;
const triggerLabel = isAutoRefreshInterval ? autoLabel : intervalLabel;

return (
<Button.Group>
<Tooltip
withArrow
label={
isLive
? `Auto-refreshing every ${intervalLabel}`
: 'Enable auto-refresh'
}
fz="xs"
color="gray"
>
<Button
onClick={onToggleLive}
size="sm"
variant={variant}
title={isLive ? 'Disable auto-refresh' : 'Enable auto-refresh'}
data-testid="dashboard-live-button"
>
Live
</Button>
</Tooltip>
<Menu position="bottom-end" withinPortal>
<Menu.Target>
<Button
size="sm"
variant={variant}
px="xs"
rightSection={<IconChevronDown size={14} />}
aria-label={`Refresh interval ${triggerLabel}`}
data-testid="dashboard-refresh-interval-button"
style={{
borderInlineStart: '1px solid var(--color-border)',
}}
>
{triggerLabel}
</Button>
</Menu.Target>
<Menu.Dropdown data-testid="dashboard-refresh-interval-menu">
<Menu.Label>Refresh every</Menu.Label>
<IntervalItem
label={autoLabel}
checked={isAutoRefreshInterval}
onClick={() => onRefreshIntervalChange(undefined)}
/>
{refreshIntervalOptions.length > 0 && <Menu.Divider />}
{refreshIntervalOptions.map(interval => (
<IntervalItem
key={interval}
label={mapGranularityToExternalFormat(interval)}
checked={!isAutoRefreshInterval && interval === refreshInterval}
onClick={() => onRefreshIntervalChange(interval)}
/>
))}
</Menu.Dropdown>
</Menu>
</Button.Group>
);
}
112 changes: 112 additions & 0 deletions packages/app/src/components/__tests__/DashboardLiveButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React from 'react';
import { Granularity } from '@hyperdx/common-utils/dist/core/utils';
import { fireEvent, screen } from '@testing-library/react';

import { DashboardLiveButton } from '@/components/DashboardLiveButton';

const OPTIONS = [
Granularity.ThirtySecond,
Granularity.OneMinute,
Granularity.FiveMinute,
];

function renderButton(
props: Partial<React.ComponentProps<typeof DashboardLiveButton>> = {},
) {
const onToggleLive = jest.fn();
const onRefreshIntervalChange = jest.fn();
renderWithMantine(
<DashboardLiveButton
isLive={false}
onToggleLive={onToggleLive}
refreshInterval={Granularity.FiveMinute}
isAutoRefreshInterval
defaultRefreshInterval={Granularity.FiveMinute}
refreshIntervalOptions={OPTIONS}
onRefreshIntervalChange={onRefreshIntervalChange}
{...props}
/>,
);
return { onToggleLive, onRefreshIntervalChange };
}

async function openMenu() {
fireEvent.click(screen.getByTestId('dashboard-refresh-interval-button'));
await screen.findByTestId('dashboard-refresh-interval-menu');
}

describe('DashboardLiveButton', () => {
it('toggles live mode', () => {
const { onToggleLive } = renderButton();

fireEvent.click(screen.getByTestId('dashboard-live-button'));

expect(onToggleLive).toHaveBeenCalledTimes(1);
});

it('shows auto as the first, checked item', async () => {
renderButton();

expect(
screen.getByTestId('dashboard-refresh-interval-button'),
).toHaveTextContent('Auto (5m)');

await openMenu();
const items = screen.getAllByRole('menuitem', { hidden: true });
expect(items.map(item => item.textContent)).toEqual([
'Auto (5m)',
'30s',
'1m',
'5m',
]);
expect(items[0]).toHaveAttribute('data-checked', 'true');
expect(items[3]).toHaveAttribute('data-checked', 'false');
});

it('checks a fixed interval equal to auto without checking auto', async () => {
renderButton({ isAutoRefreshInterval: false });

expect(
screen.getByTestId('dashboard-refresh-interval-button'),
).toHaveTextContent(/^5m$/);

await openMenu();
expect(
screen.getByRole('menuitem', { name: 'Auto (5m)', hidden: true }),
).toHaveAttribute('data-checked', 'false');
expect(
screen.getByRole('menuitem', { name: '5m', hidden: true }),
).toHaveAttribute('data-checked', 'true');
});

it('selects a fixed interval or auto', async () => {
const { onRefreshIntervalChange } = renderButton();

await openMenu();
fireEvent.click(screen.getByRole('menuitem', { name: '1m', hidden: true }));
expect(onRefreshIntervalChange).toHaveBeenLastCalledWith(
Granularity.OneMinute,
);

await openMenu();
fireEvent.click(
screen.getByRole('menuitem', { name: 'Auto (5m)', hidden: true }),
);
expect(onRefreshIntervalChange).toHaveBeenLastCalledWith(undefined);
});

it('offers only auto when there are no fixed intervals', async () => {
renderButton({
refreshInterval: Granularity.FifteenSecond,
defaultRefreshInterval: Granularity.FifteenSecond,
refreshIntervalOptions: [],
});

await openMenu();
expect(
screen
.getAllByRole('menuitem', { hidden: true })
.map(item => item.textContent),
).toEqual(['Auto (15s)']);
});
});
Loading
Loading