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
7 changes: 7 additions & 0 deletions .changeset/dashboard-filter-reorder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/app': patch
---

feat: let users reorder dashboard filters and variables. The edit filters modal
now shows up/down controls on each filter, and that order drives both the modal
list and the filter bar.
14 changes: 14 additions & 0 deletions packages/app/src/DBDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { ErrorBoundary } from 'react-error-boundary';
import RGL from 'react-grid-layout';
import { useForm, useWatch } from 'react-hook-form';
import { arrayMove } from '@dnd-kit/sortable';
import {
TableConnection,
tcFromSource,
Expand Down Expand Up @@ -2132,6 +2133,18 @@ function DBDashboardPage({
});
};

const handleReorderFilter = (fromIndex: number, toIndex: number) => {

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 — Reorder is computed from stale dashboard data, so quick repeated clicks overwrite each other on saved dashboards

On a saved dashboard, setDashboard sends a PATCH and only updates the list when the ['dashboards'] refetch comes back (packages/app/src/dashboard.ts:159-187, 279-319). Nothing updates the list in the meantime, so the rows keep their old order and the chevrons stay clickable. Each click builds a full filters array from the old order and replaces the saved one. Example: starting from [A,B,C], clicking 'down' on A twice quickly sends [B,A,C] both times, so A moves one place instead of two. Clicking A down and then C up quickly sends [A,C,B] last, which throws away A's move. The PATCHes can also arrive out of order. The E2E spec works around this by polling between moves. Fix: disable the reorder buttons while isLoading (which already reflects isSavingDashboard || isFetchingDashboard), or update the ['dashboards'] cache optimistically in onMutate.

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.

Fixed in 79a5540. The reorder chevrons are now disabled while a save is in flight (isLoading = isSavingDashboard || isFetchingDashboard), so a second click can't fire until the list reflects the first move's new order. Added a unit test for the disabled-while-saving state. I went with disabling rather than an optimistic onMutate to keep the change out of dashboard.ts, which this PR doesn't otherwise touch.

if (!dashboard?.filters) return;

setDashboard(
produce(dashboard, draft => {
if (draft.filters) {
draft.filters = arrayMove(draft.filters, fromIndex, toIndex);
}
}),
);
};
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

const [isLive, setIsLive] = useState(false);
const [selectedRefreshInterval, setSelectedRefreshInterval] = useState<
Granularity | undefined
Expand Down Expand Up @@ -3654,6 +3667,7 @@ function DBDashboardPage({
filters={filters}
onSaveFilter={handleSaveFilter}
onRemoveFilter={handleRemoveFilter}
onReorderFilter={handleReorderFilter}
isLoading={isSavingDashboard || isFetchingDashboard}
showVariableOptions
showRequiredFilterOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
import {
IconAsterisk,
IconBuildingBroadcastTower,
IconChevronDown,
IconChevronUp,
IconDatabase,
IconFilter,
IconLabel,
Expand Down Expand Up @@ -50,6 +52,8 @@ interface DashboardFiltersListProps {
showVariableOptions: boolean;
onEdit: (filter: DashboardFilter) => void;
onRemove: (id: string) => void;
/** Moves the filter at `fromIndex` to `toIndex`. Omit to hide reorder controls. */
onReorder?: (fromIndex: number, toIndex: number) => void;
onClose: () => void;
onAddNew: () => void;
}
Expand Down Expand Up @@ -150,6 +154,7 @@ export const DashboardFiltersList = ({
showVariableOptions,
onEdit,
onRemove,
onReorder,
onClose,
onAddNew,
}: DashboardFiltersListProps) => {
Expand All @@ -169,8 +174,14 @@ export const DashboardFiltersList = ({
gap="xs"
data-testid="dashboard-filters-list"
>
{filters.map(filter => {
{filters.map((filter, index) => {
const attributes = [getValuesAttribute(filter, sources, connections)];
// Each reorder saves and the list only reflects the new order once
// the save round-trips, so lock the controls while a save is in
// flight — otherwise a quick second click would reorder against the
// stale order and clobber the first move.
const upDisabled = index === 0 || !!isLoading;
const downDisabled = index === filters.length - 1 || !!isLoading;

const broadcastTarget = getBroadcastTargetDisplay(filter, sources);
if (!hideAppliesTo && broadcastTarget != null) {
Expand Down Expand Up @@ -206,6 +217,32 @@ export const DashboardFiltersList = ({
attributes={attributes}
actions={
<>
{onReorder && (
<>
<UnstyledButton
onClick={() => onReorder(index, index - 1)}
disabled={upDisabled}
className={`${styles.filterActionButton} ${
upDisabled ? styles.filterActionButtonDisabled : ''
}`}
data-testid={`move-filter-up-button-${filter.name}`}
aria-label={`Move ${filter.name} up`}
>
<IconChevronUp size={16} />
</UnstyledButton>
<UnstyledButton
onClick={() => onReorder(index, index + 1)}
disabled={downDisabled}
className={`${styles.filterActionButton} ${
downDisabled ? styles.filterActionButtonDisabled : ''
}`}
data-testid={`move-filter-down-button-${filter.name}`}
aria-label={`Move ${filter.name} down`}
>
<IconChevronDown size={16} />
</UnstyledButton>
</>
)}
<UnstyledButton
onClick={() => onEdit(filter)}
className={styles.filterActionButton}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,100 @@ describe('DashboardFiltersModal', () => {
),
).toBeNull();
});

describe('reordering', () => {
const renderWithReorder = (
filters: DashboardFilter[],
props: Partial<React.ComponentProps<typeof DashboardFiltersModal>> = {},
) => {
const onReorderFilter = jest.fn();
renderWithMantine(
<DashboardFiltersModal
opened
filters={filters}
showVariableOptions
showRequiredFilterOptions
onClose={jest.fn()}
onSaveFilter={jest.fn()}
onRemoveFilter={jest.fn()}
onReorderFilter={onReorderFilter}
{...props}
/>,
);
return { onReorderFilter, user: userEvent.setup() };
};

it('moves a filter down by its index', async () => {
const { onReorderFilter, user } = renderWithReorder([
EXISTING_FILTER,
EXISTING_STATIC_FILTER,
]);

await user.click(
screen.getByTestId(`move-filter-down-button-${EXISTING_FILTER.name}`),
);

expect(onReorderFilter).toHaveBeenCalledWith(0, 1);
});

it('moves a filter up by its index', async () => {
const { onReorderFilter, user } = renderWithReorder([
EXISTING_FILTER,
EXISTING_STATIC_FILTER,
]);

await user.click(
screen.getByTestId(
`move-filter-up-button-${EXISTING_STATIC_FILTER.name}`,
),
);

expect(onReorderFilter).toHaveBeenCalledWith(1, 0);
});

it('disables up on the first filter and down on the last', () => {
renderWithReorder([EXISTING_FILTER, EXISTING_STATIC_FILTER]);

expect(
screen.getByTestId(`move-filter-up-button-${EXISTING_FILTER.name}`),
).toBeDisabled();
expect(
screen.getByTestId(
`move-filter-down-button-${EXISTING_STATIC_FILTER.name}`,
),
).toBeDisabled();
expect(
screen.getByTestId(`move-filter-down-button-${EXISTING_FILTER.name}`),
).toBeEnabled();
});

// A reorder saves and the list only reflects it once the save round-trips,
// so the controls lock while a save is in flight to stop a second click
// reordering against the stale order.
it('disables every reorder control while a save is in flight', () => {
renderWithReorder([EXISTING_FILTER, EXISTING_STATIC_FILTER], {
isLoading: true,
});

expect(
screen.getByTestId(`move-filter-down-button-${EXISTING_FILTER.name}`),
).toBeDisabled();
expect(
screen.getByTestId(
`move-filter-up-button-${EXISTING_STATIC_FILTER.name}`,
),
).toBeDisabled();
});

it('hides reorder controls without an onReorderFilter handler', () => {
renderModal({ filters: [EXISTING_FILTER] });

expect(
screen.queryByTestId(`move-filter-up-button-${EXISTING_FILTER.name}`),
).toBeNull();
expect(
screen.queryByTestId(`move-filter-down-button-${EXISTING_FILTER.name}`),
).toBeNull();
});
});
});
4 changes: 4 additions & 0 deletions packages/app/src/components/DashboardFiltersModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface DashboardFiltersEditModalProps {
onClose: () => void;
onSaveFilter: (filter: DashboardFilter) => void;
onRemoveFilter: (id: string) => void;
/** Moves the filter at `fromIndex` to `toIndex`. Omit to hide reorder controls. */
onReorderFilter?: (fromIndex: number, toIndex: number) => void;
}

const DashboardFiltersModal = ({
Expand All @@ -43,6 +45,7 @@ const DashboardFiltersModal = ({
onClose,
onSaveFilter,
onRemoveFilter,
onReorderFilter,
}: DashboardFiltersEditModalProps) => {
// Undefined when not editing, set to the filter being edited when editing.
// The filter is undefined when creating a new one.
Expand Down Expand Up @@ -101,6 +104,7 @@ const DashboardFiltersModal = ({
filters={filters}
onEdit={startEditing}
onRemove={onRemoveFilter}
onReorder={onReorderFilter}
onClose={onClose}
onAddNew={handleAddNewFilter}
isLoading={isLoading}
Expand Down
5 changes: 5 additions & 0 deletions packages/app/styles/DashboardFiltersModal.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
}
}

.filterActionButtonDisabled {
opacity: 0.25;
pointer-events: none;
}

.deleteButton {
&:hover {
color: var(--color-text-danger);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Filters are stored as an ordered list, and that order drives both the Edit
* Filters modal's list and the filter bar on the dashboard. The modal's up/down
* controls on each row let a user change that order, and the change persists.
*/
import { DashboardPage } from '../page-objects/DashboardPage';
import { expect, test } from '../utils/base-test';

const FILTERS = ['Alpha', 'Bravo', 'Charlie'] as const;

test.describe(
'Dashboard filter reordering',
{ tag: ['@dashboard', '@full-stack'] },
() => {
let dashboardPage: DashboardPage;

test.beforeEach(async ({ page }) => {
dashboardPage = new DashboardPage(page);
await dashboardPage.goto();
});

const createThreeFilters = async () => {
await dashboardPage.createNewDashboard();
await dashboardPage.openEditFiltersModal();
for (const [i, name] of FILTERS.entries()) {
await dashboardPage.addStaticListFilterToDashboard(name, ['x', 'y'], {
variableName: `v${i}`,
});
}
};

test('reorders filters with the up/down controls and persists the order', async ({
page,
}) => {
test.setTimeout(150000);

await test.step('Create three filters in a known order', async () => {
await createThreeFilters();
await expect
.poll(() => dashboardPage.getFilterListItemNames(), {
timeout: 15000,
})
.toEqual([...FILTERS]);
});

await test.step('The ends are pinned: up on the first, down on the last', async () => {
await expect(
dashboardPage.getMoveFilterUpButton('Alpha'),
).toBeDisabled();
await expect(
dashboardPage.getMoveFilterDownButton('Charlie'),
).toBeDisabled();
await expect(
dashboardPage.getMoveFilterDownButton('Alpha'),
).toBeEnabled();
});

// Each move persists through the dashboard update mutation, so poll until
// the reordered list lands before issuing the next move.
await test.step('Moving the first filter down swaps it with the second', async () => {
await dashboardPage.moveFilterDown('Alpha');
await expect
.poll(() => dashboardPage.getFilterListItemNames(), {
timeout: 15000,
})
.toEqual(['Bravo', 'Alpha', 'Charlie']);
});

await test.step('Moving the last filter up swaps it with the one above', async () => {
await dashboardPage.moveFilterUp('Charlie');
await expect
.poll(() => dashboardPage.getFilterListItemNames(), {
timeout: 15000,
})
.toEqual(['Bravo', 'Charlie', 'Alpha']);
});

await test.step('The new order survives a reload', async () => {
await dashboardPage.closeFiltersModal();
await page.reload();
await dashboardPage.waitForLoaded();
await dashboardPage.openEditFiltersModal();
await expect
.poll(() => dashboardPage.getFilterListItemNames(), {
timeout: 15000,
})
.toEqual(['Bravo', 'Charlie', 'Alpha']);
});
});
},
);
37 changes: 37 additions & 0 deletions packages/app/tests/e2e/page-objects/DashboardPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
// The document title comes from the saved dashboard, so it only changes
// once the rename has persisted.
const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), {

Check failure on line 289 in packages/app/tests/e2e/page-objects/DashboardPage.ts

View workflow job for this annotation

GitHub Actions / e2e-tests / E2E Tests - Shard 2

[chromium] › tests/e2e/features/dashboard.spec.ts:48:7 › Dashboard › should persist dashboard across page reloads @dashboard

5) [chromium] › tests/e2e/features/dashboard.spec.ts:48:7 › Dashboard › should persist dashboard across page reloads @dashboard › Create and name a new dashboard Error: expect(page).toHaveTitle(expected) failed Expected pattern: /^Test Dashboard 1796144433611 - / Received string: "My New Dashboard - ClickStack" Timeout: 10000ms Call log: - Expect "toHaveTitle" with timeout 10000ms 14 × unexpected value "My New Dashboard - ClickStack" at page-objects/DashboardPage.ts:289 287 | // once the rename has persisted. 288 | const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); > 289 | await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), { | ^ 290 | timeout: 10000, 291 | }); 292 | } at DashboardPage.renameDashboard (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:289:29) at DashboardPage.editDashboardName (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:271:5) at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard.spec.ts:57:7 at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard.spec.ts:51:5

Check failure on line 289 in packages/app/tests/e2e/page-objects/DashboardPage.ts

View workflow job for this annotation

GitHub Actions / e2e-tests / E2E Tests - Shard 2

[chromium] › tests/e2e/features/dashboard-template-import.spec.ts:447:7 › Dashboard Template Import › should map tile onClick dashboard to the selected dashboard id on import @dashboard @full-stack

4) [chromium] › tests/e2e/features/dashboard-template-import.spec.ts:447:7 › Dashboard Template Import › should map tile onClick dashboard to the selected dashboard id on import @dashboard @full-stack › Create a target dashboard for the onClick link Error: expect(page).toHaveTitle(expected) failed Expected pattern: /^E2E OnClick Target 1796144416277 - / Received string: "My New Dashboard - ClickStack" Timeout: 10000ms Call log: - Expect "toHaveTitle" with timeout 10000ms 14 × unexpected value "My New Dashboard - ClickStack" at page-objects/DashboardPage.ts:289 287 | // once the rename has persisted. 288 | const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); > 289 | await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), { | ^ 290 | timeout: 10000, 291 | }); 292 | } at DashboardPage.renameDashboard (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:289:29) at DashboardPage.editDashboardName (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:271:5) at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard-template-import.spec.ts:462:9 at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard-template-import.spec.ts:459:7

Check failure on line 289 in packages/app/tests/e2e/page-objects/DashboardPage.ts

View workflow job for this annotation

GitHub Actions / e2e-tests / E2E Tests - Shard 2

[chromium] › tests/e2e/features/dashboard-table-linking.spec.ts:676:9 › Dashboard Table Linking › Dashboard mode: a variable-enabled target filter still receives an expression-keyed link @dashboard @full-stack

3) [chromium] › tests/e2e/features/dashboard-table-linking.spec.ts:676:9 › Dashboard Table Linking › Dashboard mode: a variable-enabled target filter still receives an expression-keyed link @dashboard @full-stack › Create the target dashboard with a variable-enabled ServiceName filter Error: expect(page).toHaveTitle(expected) failed Expected pattern: /^E2E Variable Filter Target 1796144396687 - / Received string: "My New Dashboard - ClickStack" Timeout: 10000ms Call log: - Expect "toHaveTitle" with timeout 10000ms 14 × unexpected value "My New Dashboard - ClickStack" at page-objects/DashboardPage.ts:289 287 | // once the rename has persisted. 288 | const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); > 289 | await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), { | ^ 290 | timeout: 10000, 291 | }); 292 | } at DashboardPage.renameDashboard (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:289:29) at DashboardPage.editDashboardName (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:271:5) at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard-table-linking.spec.ts:689:9 at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard-table-linking.spec.ts:687:7

Check failure on line 289 in packages/app/tests/e2e/page-objects/DashboardPage.ts

View workflow job for this annotation

GitHub Actions / e2e-tests / E2E Tests - Shard 2

[chromium] › tests/e2e/features/dashboard-table-linking.spec.ts:583:9 › Dashboard Table Linking › Dashboard mode: filter templates render into the destination dashboard URL filters param @dashboard @full-stack

2) [chromium] › tests/e2e/features/dashboard-table-linking.spec.ts:583:9 › Dashboard Table Linking › Dashboard mode: filter templates render into the destination dashboard URL filters param @dashboard @full-stack › Create the target dashboard with a declared filter matching ServiceName Error: expect(page).toHaveTitle(expected) failed Expected pattern: /^E2E Filter Target 1796144393212 - / Received string: "My New Dashboard - ClickStack" Timeout: 10000ms Call log: - Expect "toHaveTitle" with timeout 10000ms 14 × unexpected value "My New Dashboard - ClickStack" at page-objects/DashboardPage.ts:289 287 | // once the rename has persisted. 288 | const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); > 289 | await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), { | ^ 290 | timeout: 10000, 291 | }); 292 | } at DashboardPage.renameDashboard (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:289:29) at DashboardPage.editDashboardName (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:271:5) at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard-table-linking.spec.ts:592:9 at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/dashboard-table-linking.spec.ts:590:7

Check failure on line 289 in packages/app/tests/e2e/page-objects/DashboardPage.ts

View workflow job for this annotation

GitHub Actions / e2e-tests / E2E Tests - Shard 3

[chromium] › tests/e2e/features/heatmap.spec.ts:62:7 › Heatmap › dashboard heatmap tile renders and persists across reload @full-stack @dashboard

1) [chromium] › tests/e2e/features/heatmap.spec.ts:62:7 › Heatmap › dashboard heatmap tile renders and persists across reload @full-stack @dashboard › Create a dashboard Error: expect(page).toHaveTitle(expected) failed Expected pattern: /^E2E Heatmap Dashboard 1796144458459 - / Received string: "My New Dashboard - ClickStack" Timeout: 10000ms Call log: - Expect "toHaveTitle" with timeout 10000ms 14 × unexpected value "My New Dashboard - ClickStack" at page-objects/DashboardPage.ts:289 287 | // once the rename has persisted. 288 | const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); > 289 | await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), { | ^ 290 | timeout: 10000, 291 | }); 292 | } at DashboardPage.renameDashboard (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:289:29) at DashboardPage.editDashboardName (/home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/page-objects/DashboardPage.ts:271:5) at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/heatmap.spec.ts:76:9 at /home/runner/work/ClickStack/ClickStack/packages/app/tests/e2e/features/heatmap.spec.ts:73:7
timeout: 10000,
});
}
Expand Down Expand Up @@ -1187,6 +1187,43 @@
return this.page.getByTestId(`dashboard-filter-item-${name}`);
}

/**
* The display names of the filters in the Edit Filters modal's list, in the
* order they are rendered. Used to assert reordering.
*/
async getFilterListItemNames(): Promise<string[]> {
const items = this.page
.getByTestId('dashboard-filters-list')
.locator('[data-testid^="dashboard-filter-item-"]');
const count = await items.count();
const names: string[] = [];
for (let i = 0; i < count; i++) {
const testId = await items.nth(i).getAttribute('data-testid');
names.push((testId ?? '').replace('dashboard-filter-item-', ''));
}
return names;
}

/** Move a filter up one position in the Edit Filters modal's list. */
async moveFilterUp(name: string) {
await this.page.getByTestId(`move-filter-up-button-${name}`).click();
}

/** Move a filter down one position in the Edit Filters modal's list. */
async moveFilterDown(name: string) {
await this.page.getByTestId(`move-filter-down-button-${name}`).click();
}

/** The up control for a filter row, for asserting its disabled state. */
getMoveFilterUpButton(name: string): Locator {
return this.page.getByTestId(`move-filter-up-button-${name}`);
}

/** The down control for a filter row, for asserting its disabled state. */
getMoveFilterDownButton(name: string): Locator {
return this.page.getByTestId(`move-filter-down-button-${name}`);
}

/** The filters list's summary of which sources a filter broadcasts to. */
getFilterBroadcastTarget(name: string) {
return this.page.getByTestId(`dashboard-filter-applies-to-${name}`);
Expand Down
Loading