diff --git a/.changeset/dashboard-filter-reorder.md b/.changeset/dashboard-filter-reorder.md new file mode 100644 index 000000000..93e418884 --- /dev/null +++ b/.changeset/dashboard-filter-reorder.md @@ -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. diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 89c1e5146..90bbba65e 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -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, @@ -2132,6 +2133,18 @@ function DBDashboardPage({ }); }; + const handleReorderFilter = (fromIndex: number, toIndex: number) => { + if (!dashboard?.filters) return; + + setDashboard( + produce(dashboard, draft => { + if (draft.filters) { + draft.filters = arrayMove(draft.filters, fromIndex, toIndex); + } + }), + ); + }; + const [isLive, setIsLive] = useState(false); const [selectedRefreshInterval, setSelectedRefreshInterval] = useState< Granularity | undefined @@ -3654,6 +3667,7 @@ function DBDashboardPage({ filters={filters} onSaveFilter={handleSaveFilter} onRemoveFilter={handleRemoveFilter} + onReorderFilter={handleReorderFilter} isLoading={isSavingDashboard || isFetchingDashboard} showVariableOptions showRequiredFilterOptions diff --git a/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx b/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx index 6828e205e..9f2621ba1 100644 --- a/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx +++ b/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx @@ -18,6 +18,8 @@ import { import { IconAsterisk, IconBuildingBroadcastTower, + IconChevronDown, + IconChevronUp, IconDatabase, IconFilter, IconLabel, @@ -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; } @@ -150,6 +154,7 @@ export const DashboardFiltersList = ({ showVariableOptions, onEdit, onRemove, + onReorder, onClose, onAddNew, }: DashboardFiltersListProps) => { @@ -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) { @@ -206,6 +217,32 @@ export const DashboardFiltersList = ({ attributes={attributes} actions={ <> + {onReorder && ( + <> + 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`} + > + + + 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`} + > + + + + )} onEdit(filter)} className={styles.filterActionButton} diff --git a/packages/app/src/components/DashboardFiltersModal/__tests__/DashboardFiltersModal.test.tsx b/packages/app/src/components/DashboardFiltersModal/__tests__/DashboardFiltersModal.test.tsx index 91c0840b3..08eff932e 100644 --- a/packages/app/src/components/DashboardFiltersModal/__tests__/DashboardFiltersModal.test.tsx +++ b/packages/app/src/components/DashboardFiltersModal/__tests__/DashboardFiltersModal.test.tsx @@ -530,4 +530,100 @@ describe('DashboardFiltersModal', () => { ), ).toBeNull(); }); + + describe('reordering', () => { + const renderWithReorder = ( + filters: DashboardFilter[], + props: Partial> = {}, + ) => { + const onReorderFilter = jest.fn(); + renderWithMantine( + , + ); + 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(); + }); + }); }); diff --git a/packages/app/src/components/DashboardFiltersModal/index.tsx b/packages/app/src/components/DashboardFiltersModal/index.tsx index 96c27f9f9..8c3797502 100644 --- a/packages/app/src/components/DashboardFiltersModal/index.tsx +++ b/packages/app/src/components/DashboardFiltersModal/index.tsx @@ -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 = ({ @@ -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. @@ -101,6 +104,7 @@ const DashboardFiltersModal = ({ filters={filters} onEdit={startEditing} onRemove={onRemoveFilter} + onReorder={onReorderFilter} onClose={onClose} onAddNew={handleAddNewFilter} isLoading={isLoading} diff --git a/packages/app/styles/DashboardFiltersModal.module.scss b/packages/app/styles/DashboardFiltersModal.module.scss index 10ba0c9ea..f17295984 100644 --- a/packages/app/styles/DashboardFiltersModal.module.scss +++ b/packages/app/styles/DashboardFiltersModal.module.scss @@ -16,6 +16,11 @@ } } +.filterActionButtonDisabled { + opacity: 0.25; + pointer-events: none; +} + .deleteButton { &:hover { color: var(--color-text-danger); diff --git a/packages/app/tests/e2e/features/dashboard-filter-reorder.spec.ts b/packages/app/tests/e2e/features/dashboard-filter-reorder.spec.ts new file mode 100644 index 000000000..259eda2b4 --- /dev/null +++ b/packages/app/tests/e2e/features/dashboard-filter-reorder.spec.ts @@ -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']); + }); + }); + }, +); diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index 54a87a038..39afaeeb9 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -1187,6 +1187,43 @@ export class DashboardPage { 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 { + 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}`);