From d3970eb987e1ff8f9ebe48fc3bfe6df2b6f87cd2 Mon Sep 17 00:00:00 2001 From: Zakk Date: Tue, 29 Sep 2026 17:58:23 +1000 Subject: [PATCH] feat(ui): move connection detail actions behind a More menu Each panel shows one primary action. The connection detail keeps Add rule and moves show rule, edit rule, view flow, trace, only this client and close connection into a trailing More menu, close last. MoreMenu is the shared wrapper; a disabled item carries its reason as its description. --- e2e/architecture.spec.ts | 4 +- e2e/connection-rule.spec.ts | 60 +++++++++---------- e2e/connections.spec.ts | 33 ++++++++--- e2e/fixtures.ts | 11 ++++ e2e/flows.spec.ts | 4 +- e2e/trace-links.spec.ts | 4 +- src/features/connections/Connections.tsx | 44 ++++++++------ src/ui/ActionGroup.test.tsx | 40 +++++++++++++ src/ui/ActionGroup.tsx | 75 +++++++++++++++++------- src/ui/styles/buttons-menus.css | 7 +++ src/ui/ui.tsx | 2 +- 11 files changed, 197 insertions(+), 87 deletions(-) create mode 100644 src/ui/ActionGroup.test.tsx diff --git a/e2e/architecture.spec.ts b/e2e/architecture.spec.ts index 86bd8c6c..b032c92e 100644 --- a/e2e/architecture.spec.ts +++ b/e2e/architecture.spec.ts @@ -2,7 +2,7 @@ import {test as httpTest, type Locator, type Page} from '@playwright/test'; import {createMockApi} from '../src/api/mock'; import {sha256} from '../src/api/hash'; import {ApiError} from '../src/api/error'; -import {expect, test} from './fixtures'; +import {expect, test, moreAction} from './fixtures'; async function backend(page: Page) { const api = createMockApi(); @@ -153,7 +153,7 @@ for (const all of [false, true]) { if (all) { await page.getByRole('button', {name: 'Close all', exact: true}).click(); await page.getByRole('alertdialog').getByRole('button', {name: 'Close all', exact: true}).click(); - } else await page.getByRole('button', {name: 'Close connection', exact: true}).click(); + } else await moreAction(page.locator('.rp-panel, .rp-drawer'), 'Close connection'); await request; await expect(page).toHaveURL(/connections\?id=1$/); // A pending confirmation covers the page; its Cancel abandons the bulk close. diff --git a/e2e/connection-rule.spec.ts b/e2e/connection-rule.spec.ts index 7fc63707..71192e52 100644 --- a/e2e/connection-rule.spec.ts +++ b/e2e/connection-rule.spec.ts @@ -1,4 +1,4 @@ -import {detail, expect, expectLoadFailures, mockBackend, test} from './fixtures'; +import {detail, expect, expectLoadFailures, mockBackend, test, moreAction} from './fixtures'; import {createMockApi} from '../src/api/mock'; import {ApiError} from '../src/api/error'; import {sha256} from '../src/api/hash'; @@ -68,7 +68,7 @@ test('a refused write shows its diagnostics in the dialog and writes nothing', a test('show matched rule opens the rule list on that rule', async ({page}) => { await mockBackend(page); await page.goto('/#/connections?id=1'); - await detail(page).getByRole('button', {name: 'Show matched rule', exact: true}).click(); + await moreAction(detail(page), 'Show matched rule'); await expect(page).toHaveURL(/#\/rules\?tab=list&rule=r5$/); const selected = page.getByRole('tabpanel', {name: 'Routing rules'}).locator('[role=row][aria-selected=true]'); await expect(selected).toHaveCount(1); @@ -83,7 +83,9 @@ test('a connection with no recorded rule adds before the fallback and says earli }; await page.goto('/#/connections?id=1'); await expect(detail(page).getByRole('heading', {name: 'api.telegram.org'})).toBeVisible(); - await expect(detail(page).getByRole('button', {name: 'Show matched rule', exact: true})).toHaveCount(0); + await detail(page).getByRole('button', {name: 'More actions', exact: true}).click(); + await expect(page.getByRole('menuitem', {name: 'Show matched rule', exact: true})).toHaveCount(0); + await page.keyboard.press('Escape'); await detail(page).getByRole('button', {name: 'Add rule', exact: true}).click(); const dialog = page.getByRole('dialog', {name: 'Add rule'}); await expect(dialog.getByRole('button', {name: /Insert$/})).toContainText('Last, before the fallback'); @@ -100,41 +102,35 @@ test('without a writable configuration showing the matched rule only reads', asy await page.goto('/#/connections?id=1'); await expect(detail(page).getByRole('heading', {name: 'api.telegram.org'})).toBeVisible(); await expect(detail(page).getByRole('link', {name: /in the rule list$/})).toBeVisible(); - await detail(page).getByRole('button', {name: 'Show matched rule', exact: true}).click(); + await moreAction(detail(page), 'Show matched rule'); await expect(page).toHaveURL(/#\/rules\?tab=list&rule=r5$/); }); for (const width of [360, 768, 1440]) - test(`the panel actions share one size, line up and keep even gaps at ${width}px`, async ({page}) => { + test(`the panel shows one primary action beside a More menu of the same size at ${width}px`, async ({page}) => { await mockBackend(page); await page.setViewportSize({width, height: 900}); await page.goto('/#/connections?id=1'); - const close = detail(page).getByRole('button', {name: 'Close connection', exact: true}); - await expect(close).toBeVisible(); - const boxes = await detail(page) - .getByRole('button', { - name: /^(Add rule|Show matched rule|Edit matched rule's outbound settings|View flow|Trace this connection|Only this device|Close connection)$/ - }) - .evaluateAll(buttons => buttons.map(button => button.getBoundingClientRect().toJSON() as DOMRect)); - expect(boxes).toHaveLength(7); - // One button style: a quiet button beside a filled one reads as a misaligned label. - const fills = await detail(page) - .getByRole('button', {name: /^(Add rule|Show matched rule|Edit matched rule's outbound settings|View flow|Trace this connection|Only this device)$/}) - .evaluateAll(buttons => buttons.map(button => getComputedStyle(button).backgroundColor)); - expect(new Set(fills).size).toBe(1); - expect(new Set(boxes.map(box => box.height)).size).toBe(1); - const lines: DOMRect[][] = []; - for (const box of boxes) { - const line = lines.find(line => Math.abs(line[0].top - box.top) <= 1); - if (line) line.push(box); - else lines.push([box]); - } - const gaps = lines.flatMap(line => line.slice(1).map((box, i) => box.left - line[i].right)); - const leading = lines.slice(1).map((line, i) => line[0].top - lines[i][0].bottom); - for (const gap of [...gaps, ...leading]) expect(Math.abs(gap - gaps[0])).toBeLessThanOrEqual(1); - expect(lines.every(line => Math.abs(line[0].left - boxes[0].left) <= 1)).toBe(true); - // The destructive action ends the group on a line of its own. - expect(lines.at(-1)).toEqual([boxes[6]]); + const add = detail(page).getByRole('button', {name: 'Add rule', exact: true}); + const more = detail(page).getByRole('button', {name: 'More actions', exact: true}); + await expect(add).toBeVisible(); + await expect(more).toBeVisible(); + await expect( + detail(page).getByRole('button', {name: /^(Show matched rule|View flow|Trace this connection|Only this device|Close connection)$/}) + ).toHaveCount(0); + const [primary, trigger] = [(await add.boundingBox())!, (await more.boundingBox())!]; + expect(trigger.height).toBe(primary.height); + expect(Math.abs(trigger.y - primary.y)).toBeLessThanOrEqual(1); + await more.click(); + // The destructive action ends the menu. + await expect(page.getByRole('menu', {name: 'More actions'}).getByRole('menuitem')).toHaveText([ + 'Show matched rule', + "Edit matched rule's outbound settings", + 'View flow', + 'Trace this connection', + 'Only this device', + 'Close connection' + ]); }); const top = (page: import('@playwright/test').Page) => page.locator('.rp-top'); @@ -628,7 +624,7 @@ test('the matched rule is edited from a connection: only its outbound changes, o const {api, requests} = await mockBackend(page); const before = (await api.config()).sources.find(source => source.id === 'src-main')!.content!; await page.goto('/#/connections?id=1'); - await detail(page).getByRole('button', {name: "Edit matched rule's outbound settings", exact: true}).click(); + await moreAction(detail(page), "Edit matched rule's outbound settings"); await expect(page).toHaveURL(/#\/rules\?.*edit=/); const dialog = page.getByRole('dialog', {name: 'Edit outbound settings'}); await expect(dialog.locator('.rp-code')).toContainText('domain(geosite: telegram)'); diff --git a/e2e/connections.spec.ts b/e2e/connections.spec.ts index c028a04b..268c98dd 100644 --- a/e2e/connections.spec.ts +++ b/e2e/connections.spec.ts @@ -1,5 +1,5 @@ import type {Locator} from '@playwright/test'; -import {expect, expectLoadFailures, mockBackend, test} from './fixtures'; +import {expect, expectLoadFailures, mockBackend, test, moreAction, moreItem} from './fixtures'; import {createMockApi} from '../src/api/mock'; // The flat list exercises the virtualizer; grouping (the default) gets its own test below. @@ -381,7 +381,7 @@ test.describe('default view', () => { await page.setViewportSize({width: 1440, height: 900}); await grid.locator('[role=row][aria-level="2"]').first().click(); await expect(page.locator('.rp-panel').getByRole('heading')).toBeVisible(); - await page.locator('.rp-panel').getByRole('button', {name: 'Only this device', exact: true}).click(); + await moreAction(page.locator('.rp-panel'), 'Only this device'); await expect(page).toHaveURL(/src=10\.0\.0\.\d+/); await expect(groups).toHaveCount(1); }); @@ -391,7 +391,7 @@ test('closing a connection removes it from the list and clears the selection', a await page.goto('/#/connections?id=c-0001'); const panel = page.locator('.rp-panel'); await expect(panel.getByRole('heading', {name: 'api.telegram.org'})).toBeVisible(); - await panel.getByRole('button', {name: 'Close connection', exact: true}).click(); + await moreAction(panel, 'Close connection'); await expect(page.locator('.rp-toast.positive')).toContainText('Closed api.telegram.org'); await expect(page).toHaveURL(/#\/connections\?tab=list$/); await expect(page.locator('.rp-table [data-key="c-0001"]')).toHaveCount(0); @@ -399,10 +399,9 @@ test('closing a connection removes it from the list and clears the selection', a await page.goto('/#/connections?id=c-0002'); const reason = 'The backend has no userspace transfer to interrupt, so this connection cannot be closed. Connections the kernel forwards directly are of this kind.'; - const close = panel.getByRole('button', {name: 'Close connection', exact: true}); + const close = await moreItem(panel, 'Close connection'); await expect(close).toBeDisabled(); - await expect(close).toHaveAccessibleDescription(reason); - await expect(panel.getByText(reason)).toBeVisible(); + await expect(close).toContainText(reason); await expect(page.locator('.rp-table [data-key="c-0002"]')).toHaveCount(1); }); @@ -419,7 +418,7 @@ test('a connection opened while another closes stays selected', async ({page}) = await page.goto(`/#/connections?id=${encodeURIComponent(closed.id)}`); const panel = page.locator('.rp-panel'); await expect(panel.getByRole('heading')).toBeVisible(); - await panel.getByRole('button', {name: 'Close connection', exact: true}).click(); + await moreAction(panel, 'Close connection'); await page.locator(`.rp-table [data-key="${opened.id}"]`).click(); await expect(page).toHaveURL(new RegExp(`[?&]id=${encodeURIComponent(opened.id)}(&|$)`)); release(); @@ -442,7 +441,25 @@ test('phone details retain routing diagnostics and omit unsupported flow actions 'href', '#/rules?tab=list&rule=' + encodeURIComponent(row.rule_id!) ); - await expect(drawer.getByRole('button', {name: 'View flow', exact: true})).toHaveCount(0); + await drawer.getByRole('button', {name: 'More actions', exact: true}).click(); + const menu = page.getByRole('menu', {name: 'More actions'}); + await expect(menu.getByRole('menuitem', {name: 'Trace this connection', exact: true})).toBeVisible(); + await expect(menu.getByRole('menuitem', {name: 'View flow', exact: true})).toHaveCount(0); +}); + +test('the connection More menu opens with Enter and gives focus back on Escape', async ({page}) => { + await page.setViewportSize({width: 1440, height: 900}); + await page.goto('/#/connections?id=c-0001'); + const panel = page.locator('.rp-panel'); + const more = panel.getByRole('button', {name: 'More actions', exact: true}); + await more.focus(); + await page.keyboard.press('Enter'); + const menu = page.getByRole('menu', {name: 'More actions'}); + await expect(menu).toBeVisible(); + await expect(menu.getByRole('menuitem').first()).toBeFocused(); + await page.keyboard.press('Escape'); + await expect(menu).toHaveCount(0); + await expect(more).toBeFocused(); }); test('the connection list exports the filtered rows as CSV', async ({page}) => { diff --git a/e2e/fixtures.ts b/e2e/fixtures.ts index 6bee1dc8..93eab8c9 100644 --- a/e2e/fixtures.ts +++ b/e2e/fixtures.ts @@ -110,6 +110,17 @@ export async function scrollTableToEnd(grid: Locator) { } export const detail = (page: Page) => page.locator('.rp-panel, .rp-drawer'); +// A panel's secondary actions sit in its trailing More menu. Opens it and returns the item; the menu itself is +// portalled out of the panel. +export async function moreItem(scope: Locator, name: string | RegExp, menu = 'More actions') { + await scope.getByRole('button', {name: menu, exact: true}).click(); + return scope + .page() + .getByRole('menu', {name: menu}) + .getByRole('menuitem', {name, exact: typeof name === 'string'}); +} +// Opens a panel's More menu and chooses an item. +export const moreAction = async (scope: Locator, name: string | RegExp, menu?: string) => (await moreItem(scope, name, menu)).click(); // The language and scheme the page takes from its next load. export const setAppearance = (page: Page, lang: string, scheme: string) => page.evaluate( diff --git a/e2e/flows.spec.ts b/e2e/flows.spec.ts index 9ac7ffaf..bfa38f12 100644 --- a/e2e/flows.spec.ts +++ b/e2e/flows.spec.ts @@ -1,4 +1,4 @@ -import {expect, faults, mockBackend, setAppearance, test} from './fixtures'; +import {expect, faults, mockBackend, setAppearance, test, moreAction} from './fixtures'; test.use({viewport: {width: 1440, height: 900}}); @@ -22,7 +22,7 @@ test.describe(() => { await panel.getByRole('link', {name: 'View connection', exact: true}).click(); await expect(page).toHaveURL(/#\/connections\?id=1$/); await expect(page.locator('.rp-panel').getByRole('heading', {name: 'api.telegram.org'})).toBeVisible(); - await page.locator('.rp-panel').getByRole('button', {name: 'View flow', exact: true}).click(); + await moreAction(page.locator('.rp-panel'), 'View flow'); await expect(page).toHaveURL(/#\/flows\?tab=records&id=flow-1$/); await page.locator('.rp-panel').getByRole('button', {name: 'Close', exact: true}).click(); await expect(page).toHaveURL(/#\/flows\?tab=records$/); diff --git a/e2e/trace-links.spec.ts b/e2e/trace-links.spec.ts index c38a52a1..6db7c9b6 100644 --- a/e2e/trace-links.spec.ts +++ b/e2e/trace-links.spec.ts @@ -1,4 +1,4 @@ -import {detail, expect, test} from './fixtures'; +import {detail, expect, test, moreAction} from './fixtures'; test.use({viewport: {width: 1440, height: 900}}); @@ -18,7 +18,7 @@ test('a trace link fills in the form without running it', async ({page}) => { test('a connection opens the trace of its target and source', async ({page}) => { await page.goto('/#/connections?tab=list&id=1'); - await detail(page).getByRole('button', {name: 'Trace this connection', exact: true}).click(); + await moreAction(detail(page), 'Trace this connection'); await expect(page).toHaveURL(/#\/rules\?tab=trace&network=tcp&domain=api\.telegram\.org&dst_ip=149\.154\.167\.220&dst_port=443&src_ip=10\.0\.0\.12$/); await expect(page.getByLabel('Domain', {exact: true})).toHaveValue('api.telegram.org'); await expect(page.getByLabel('Destination IP', {exact: true})).toHaveValue('149.154.167.220'); diff --git a/src/features/connections/Connections.tsx b/src/features/connections/Connections.tsx index d86b26e5..d2ea4f83 100644 --- a/src/features/connections/Connections.tsx +++ b/src/features/connections/Connections.tsx @@ -1,5 +1,4 @@ import { - ActionHelp, Badge, Button, ConfirmButton, @@ -14,7 +13,8 @@ import { TextField, ErrorMessage, Tabs, - TextTooltip + TextTooltip, + MoreMenu } from '../../ui/ui'; import Download from '../../ui/icons/Download'; import {Traffic} from './Traffic'; @@ -111,23 +111,31 @@ export function Connections(props: PageProps) { {cur.status} - {/* One button style for the actions; Close connection, the destructive one, always takes the last line. */} + {/* One primary action; the rest go into the trailing More menu, Close connection, the destructive one, last. */}
-
- {vm.ruleAction.canAdd && } - {vm.ruleAction.canShow && } - {vm.ruleAction.canEdit && } - {vm.canViewFlow && } - {vm.canTrace && } - {cur.source && } -
- {vm.canClose && cur.closable && ( - - - - )} + {vm.ruleAction.canAdd && } + vm.showRule()}] : []), + ...(vm.ruleAction.canEdit ? [{id: 'edit-rule', label: t('conn.editRule'), onAction: () => vm.showRule(true)}] : []), + ...(vm.canViewFlow ? [{id: 'view-flow', label: t('conn.viewFlow'), onAction: vm.showFlow}] : []), + ...(vm.canTrace ? [{id: 'trace', label: t('conn.trace'), onAction: vm.traceConnection}] : []), + ...(cur.source ? [{id: 'only-client', label: t('conn.onlyThisClient'), onAction: vm.onlyClient}] : []), + ...(vm.canClose && cur.closable + ? [ + { + id: 'close', + label: t('conn.close'), + negative: true, + isPending: vm.close.pending, + isDisabled: vm.close.disabled || !!cur.closeReason, + reason: cur.closeReason ?? undefined, + onAction: vm.close.run + } + ] + : []) + ]} + />
{}; + +it('lists the actions in order with the destructive one marked', () => { + const markup = renderToStaticMarkup( + + ); + expect(markup).toContain('aria-label="More actions"'); + expect(markup.indexOf('Trace')).toBeLessThan(markup.indexOf('Close')); + expect(markup).toMatch(/class="rp-item plain negative"[^>]*>.*Close/); + expect(markup).not.toContain('aria-disabled'); +}); + +it('describes a disabled item with its reason, and an enabled one with none', () => { + const markup = renderToStaticMarkup( + + ); + // The item names every slot it may describe itself with; the reason's is among them. + const reason = markup.match(/id="([^"]+)" slot="description">The file is read-only]*data-key="edit"`)); + expect(markup).toContain('aria-disabled="true"'); + expect(markup).not.toContain('unused while enabled'); +}); diff --git a/src/ui/ActionGroup.tsx b/src/ui/ActionGroup.tsx index 5c314c06..9047d0a1 100644 --- a/src/ui/ActionGroup.tsx +++ b/src/ui/ActionGroup.tsx @@ -1,11 +1,23 @@ import type {ReactNode} from 'react'; -import {Menu, MenuItem} from 'react-aria-components'; +import {Menu, MenuItem, Text} from 'react-aria-components'; import MoreVertical from './icons/MoreVertical'; import {useT} from '../i18n'; import {Button} from './Button'; import {MenuButton} from './Select'; +import {cx} from './cx'; -export type Action = {id: string; label: string; icon?: ReactNode; onAction: () => void; isDisabled?: boolean; isPending?: boolean}; +export type Action = { + id: string; + label: string; + icon?: ReactNode; + onAction: () => void; + isDisabled?: boolean; + isPending?: boolean; + // Why a disabled action cannot run. In a menu it is the item's description, in view under its name. + reason?: string; + // A destructive action, which the caller puts last. + negative?: boolean; +}; const ActionButton = ({action}: {action: Action}) => (