From dcb8d9ab83f565d4802104098c780a827cb303b4 Mon Sep 17 00:00:00 2001 From: Zakk Date: Thu, 1 Oct 2026 01:00:19 +1000 Subject: [PATCH] feat(rules): open the routing list on the simple view The routing list now opens on the simple view whenever it is available, for template and custom routing alike; view=advanced still opens the table. A link that points at one rule (rule, edit, add or held) lands on the table, and closing the editor such a link opened keeps the table instead of falling back to the simple view. --- e2e/activity.spec.ts | 2 +- e2e/architecture.spec.ts | 6 +++--- e2e/builder.spec.ts | 2 +- e2e/config.spec.ts | 4 ++-- e2e/connection-rule.spec.ts | 16 ++++++++-------- e2e/dns-quick-rule.spec.ts | 2 +- e2e/keyboard.spec.ts | 2 +- e2e/phone-tables.spec.ts | 4 ++-- e2e/rtl.spec.ts | 2 +- e2e/rule-editor.spec.ts | 10 +++++----- e2e/rules-templates.spec.ts | 21 +++++++++++++++++++++ e2e/rules.spec.ts | 10 +++++----- e2e/tap-reveal.spec.ts | 4 ++-- src/features/rules/template.test.ts | 10 +++++++++- src/features/rules/template.ts | 11 +++++++++++ src/features/rules/useRuleList.ts | 3 ++- src/features/rules/useRuleTemplates.ts | 8 ++++---- 17 files changed, 79 insertions(+), 38 deletions(-) diff --git a/e2e/activity.spec.ts b/e2e/activity.spec.ts index 0a9d105b..2256088d 100644 --- a/e2e/activity.spec.ts +++ b/e2e/activity.spec.ts @@ -660,7 +660,7 @@ test('the demo switches the outbound mode and back through its own configuration await expect(page.locator('.rp-toast.positive')).toContainText('reloaded: Direct'); await expect(page.locator('.rp-toast.negative')).toHaveCount(0); // The catch-all is a real rule of the new generation, right after the must rules. - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const rules = page.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]'); await expect(rules.nth(2)).toContainText('l4proto(tcp, udp)'); await expect(rules.nth(2)).toContainText('direct'); diff --git a/e2e/architecture.spec.ts b/e2e/architecture.spec.ts index b7444b82..1e3400b8 100644 --- a/e2e/architecture.spec.ts +++ b/e2e/architecture.spec.ts @@ -192,7 +192,7 @@ test('Add refuses changed rule generations while its dialog is open', async ({pa writes++; await route.fulfill({json: await api.validateConfig(route.request().postDataJSON())}); }); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await page.getByRole('button', {name: 'Add rule', exact: true}).click(); const dialog = page.getByRole('dialog'); await dialog.getByRole('radio', {name: 'Expression', exact: true}).click(); @@ -217,7 +217,7 @@ test('a source shifted since the rule list was read offers no rule edits', async main.content = main.content!.replace('routing {', 'routing {\n dport(65535) -> direct'); main.content_sha256 = await sha256(main.content); await page.route('**/api/v1/config', route => route.fulfill({json: config})); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const panel = page.getByRole('tabpanel', {name: 'Routing rules'}); await expect(panel.getByRole('row').nth(1)).toBeVisible(); await expect(page.getByRole('button', {name: 'Remove rule', exact: true, disabled: false})).toHaveCount(0); @@ -748,7 +748,7 @@ test('redacted rule labels edit accepted source and freeze the draft through val await route.fulfill({json: await api.operation(new URL(route.request().url()).pathname.split('/').pop()!)}); }); const original = (await api.config()).sources.find(source => source.kind === 'main')!.content; - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await page.getByRole('button', {name: 'Add rule', exact: true}).click(); const dialog = page.getByRole('dialog', {name: 'Add rule', exact: true}); await dialog.getByRole('textbox', {name: 'Values', exact: true}).fill('accepted.example'); diff --git a/e2e/builder.spec.ts b/e2e/builder.spec.ts index c618fcce..f46dbebd 100644 --- a/e2e/builder.spec.ts +++ b/e2e/builder.spec.ts @@ -52,7 +52,7 @@ test('editing only filters preserves the native policy spelling', async ({page}) }); test('a rule condition is composed from a kind and values, or typed as an expression', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await page.getByRole('button', {name: 'Add rule', exact: true}).click(); const dialog = page.getByRole('dialog'); await dialog.getByRole('button', {name: /Match by$/}).click(); diff --git a/e2e/config.spec.ts b/e2e/config.spec.ts index 13d88a88..5156386d 100644 --- a/e2e/config.spec.ts +++ b/e2e/config.spec.ts @@ -483,7 +483,7 @@ test('incomplete sources cannot be transformed by rule edits or quick setup', as await page.getByRole('tab', {name: 'Quick setup'}).click(); await page.getByLabel('Subscription URL', {exact: true}).fill('https://example.org/new'); await expect(page.getByRole('button', {name: 'Apply', exact: true})).toBeDisabled(); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await page.getByRole('button', {name: 'Remove rule', exact: true, disabled: false}).first().click(); await page.getByRole('alertdialog').getByRole('button', {name: 'Remove rule', exact: true}).click(); await expect(page.locator('.rp-toast.negative')).toContainText('incomplete'); @@ -603,7 +603,7 @@ test('rule writes require a stable source ID even when the display path matches' const rules = await api.rules(); for (const rule of rules.rules) if (rule.source) rule.source.source_id = 'unknown-source'; await page.route('**/api/v1/rules', route => route.fulfill({json: rules})); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const add = page.getByRole('button', {name: 'Add rule', exact: true}); await expect(add).toBeDisabled(); // The button is disabled while the configuration loads too, but gains its reason, a line under the toolbar, only once diff --git a/e2e/connection-rule.spec.ts b/e2e/connection-rule.spec.ts index 992f4c2f..38eb0756 100644 --- a/e2e/connection-rule.spec.ts +++ b/e2e/connection-rule.spec.ts @@ -164,7 +164,7 @@ test('held rules wait for one apply from the top bar, which writes them in one r // Refresh stays a plain re-read beside it. await expect(top(page).getByRole('button', {name: 'Refresh', exact: true}).locator('.rp-held-count')).toHaveCount(0); // The rule list shows what is held, and a held rule can be discarded there. - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const held = page.getByRole('region', {name: 'Pending: 2'}); await expect(held).toContainText('domain(full: api.telegram.org) -> proxy'); await expect(held).toContainText('domain(full: cdn.bilibili.com) -> direct'); @@ -190,7 +190,7 @@ test('a refused apply keeps the held rules and shows the diagnostics in the rule diagnostics: [{level: 'error', source_id: 'src-main', line: 44, column: 3, span: null, code: 'unknown-outbound', message: 'no group proxy'}] }); await hold(page, '1'); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await top(page).getByRole('button', {name: 'Apply (1)', exact: true}).click(); await expect(page.locator('.rp-toast.negative', {hasText: 'Validation found 1 error; nothing written'})).toBeVisible(); const held = page.getByRole('region', {name: 'Pending: 1'}); @@ -261,7 +261,7 @@ test('reload with rules held reloads only and keeps them held', async ({page}) = await expect(page.locator('.rp-toast.positive', {hasText: 'Reload'})).toBeVisible(); expect(requests.filter(request => request.method() !== 'GET').map(request => new URL(request.url()).pathname)).toEqual([expect.stringMatching(/reload$/)]); await expect(top(page).getByRole('button', {name: 'Apply (1)', exact: true}).locator('.rp-held-count')).toHaveText('1'); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await expect(page.getByRole('region', {name: 'Pending: 1'})).toContainText('domain(full: api.telegram.org) -> proxy'); }); @@ -349,7 +349,7 @@ test('a matched rule gone after a reload is not retargeted until the dialog says await expect(top(page).locator('.rp-held-count')).toHaveCount(0); await dialog.getByRole('button', {name: 'Hold', exact: true}).click(); await expect(dialog).toHaveCount(0); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await expect(page.getByRole('region', {name: 'Pending: 1'})).toContainText('Last, before the fallback'); }); @@ -380,7 +380,7 @@ test('an apply that fails in a later file keeps what it could not write and says const inInclude = [...connections.tcp, ...connections.udp].find(row => row.rule_id === 'r7')!; await hold(page, '1'); await hold(page, inInclude.id); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const held = page.getByRole('region', {name: 'Pending: 2'}); await expect(held).toContainText('Writes 2 files'); await top(page).getByRole('button', {name: 'Apply (2); writes 2 files', exact: true}).click(); @@ -439,7 +439,7 @@ test('closing the dialog while it reads the configuration writes nothing', async slow = false; read.open(); // The rule list is read again after the close; a write would come before that settles. - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await expect(page.getByRole('tabpanel', {name: 'Routing rules'})).toContainText('domain(geosite: telegram)'); await page.waitForTimeout(500); expect(requests.filter(request => request.method() !== 'GET')).toHaveLength(0); @@ -508,7 +508,7 @@ test('held rules cannot be discarded while they are applied', async ({page}) => }; await hold(page, '1'); await hold(page, '2'); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const held = page.getByRole('region', {name: 'Pending: 2'}); const discard = held.getByRole('button', {name: 'Discard held rule', exact: true}); await expect(discard).toHaveCount(2); @@ -699,7 +699,7 @@ test('the matched rule is edited from a connection: only its outbound changes, o test('a routing rule edits its outbound from its own row, and a read-only file disables that with the reason', async ({page}) => { const {api, handlers, requests} = await mockBackend(page); const before = (await api.config()).sources.find(source => source.id === 'src-main')!.content!; - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const row = page.getByRole('row', {name: /domain\(geosite: telegram\)/}); await row.getByRole('button', {name: 'Edit outbound settings', exact: true}).click(); const dialog = page.getByRole('dialog', {name: 'Edit outbound settings'}); diff --git a/e2e/dns-quick-rule.spec.ts b/e2e/dns-quick-rule.spec.ts index d6a2453f..80732166 100644 --- a/e2e/dns-quick-rule.spec.ts +++ b/e2e/dns-quick-rule.spec.ts @@ -312,7 +312,7 @@ test('an apply that fails in a later file keeps the rule it could not write and '/api/v1/config/sources/src-main', '/api/v1/config/sources/src-rules' ]); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await expect(page.getByRole('region', {name: 'Pending: 1'})).toContainText('rules.dae line 7: Backend message: no group proxy'); await page.goto('/#/rules?tab=dns'); await expect(page.getByRole('region', {name: /^Pending/})).toHaveCount(0); diff --git a/e2e/keyboard.spec.ts b/e2e/keyboard.spec.ts index 9e9ec058..0dabb2c7 100644 --- a/e2e/keyboard.spec.ts +++ b/e2e/keyboard.spec.ts @@ -216,7 +216,7 @@ test('Escape on a menu or dialog opened from a table row returns focus to its tr await expect(page.getByRole('menu')).toBeHidden(); await expect(menu).toBeFocused(); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const edit = await reach(page.getByRole('row', {name: /domain\(geosite: telegram\)/}), 'Edit outbound settings'); await page.keyboard.press('Enter'); await expect(page.getByRole('dialog', {name: 'Edit outbound settings'})).toBeVisible(); diff --git a/e2e/phone-tables.spec.ts b/e2e/phone-tables.spec.ts index 1bf68fa3..0d37626f 100644 --- a/e2e/phone-tables.spec.ts +++ b/e2e/phone-tables.spec.ts @@ -20,7 +20,7 @@ const columnNames = (grid: Locator) => for (const [width, url, heading, columns] of [ [320, '/#/nodes?tab=list', 'Nodes', ['Source', 'Kind', 'Nodes', 'Usage', 'Updated', 'Auto-refresh', 'Expires', 'State', 'Actions']], [360, '/#/nodes?tab=list', 'Nodes', ['Source', 'Kind', 'Nodes', 'Usage', 'Updated', 'Auto-refresh', 'Expires', 'State', 'Actions']], - [320, '/#/rules?tab=list', 'Routing rules', ['#', 'Expression', 'Outbound', 'Where', 'Hits', 'Actions']], + [320, '/#/rules?tab=list&view=advanced', 'Routing rules', ['#', 'Expression', 'Outbound', 'Where', 'Hits', 'Actions']], [360, '/#/rules?by=client&tab=list', 'Routing rules', ['#', 'Expression', 'Outbound', 'Where', 'Hits', 'Actions']] ] as const) { test.describe(`${width}px`, () => { @@ -60,7 +60,7 @@ test.describe('column resizer touch target', () => { test.use({viewport: {width: 1280, height: 900}, hasTouch: true}); test('the resizer hit area is at least 24px wide under a coarse pointer, with the line still at the column edge', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const header = page.getByRole('tabpanel', {name: 'Routing rules'}).locator('[role=columnheader]').first(); await expect(header).toBeVisible(); const resizer = header.locator('.rp-resizer'); diff --git a/e2e/rtl.spec.ts b/e2e/rtl.spec.ts index 586596c5..12b4c7e3 100644 --- a/e2e/rtl.spec.ts +++ b/e2e/rtl.spec.ts @@ -108,7 +108,7 @@ test.describe('right to left at 1280px', () => { }); test('rules keep rule text left to right', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const cells = page.locator('.rp-table :is([role=rowheader], [role=gridcell])'); await expect(cells.first()).toBeVisible(); await expectRtlLayout(page); diff --git a/e2e/rule-editor.spec.ts b/e2e/rule-editor.spec.ts index 06907709..40ee59d9 100644 --- a/e2e/rule-editor.spec.ts +++ b/e2e/rule-editor.spec.ts @@ -6,7 +6,7 @@ const rows = (page: import('@playwright/test').Page) => test('the add-rule switch explains the must keyword in each locale', async ({page}) => { await page.setViewportSize({width: 1440, height: 900}); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); for (const lang of ['zh-TW', 'en']) { for (const scheme of ['light', 'dark']) { await setAppearance(page, lang, scheme); @@ -27,7 +27,7 @@ test('the add-rule switch explains the must keyword in each locale', async ({pag test('the insert position is searchable by the rule it goes before', async ({page}) => { await page.setViewportSize({width: 1440, height: 900}); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); await page.getByRole('button', {name: 'Add rule', exact: true}).first().click(); const dialog = page.getByRole('dialog'); await dialog.getByRole('button', {name: /Insert/}).click(); @@ -45,7 +45,7 @@ test('the insert position is searchable by the rule it goes before', async ({pag }); test('the rule list shows the dictionary in evaluation order with its source lines', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const list = rows(page); await expect(list).toHaveCount(9); await expect(list.first()).toContainText('pname(NetworkManager, systemd-resolved)'); @@ -58,7 +58,7 @@ test('the rule list shows the dictionary in evaluation order with its source lin }); test('a rule from an include file says why it cannot be changed here and opens its file at the line', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const row = rows(page).nth(6); await expect(row).toContainText('rules.dae:6'); const reason = @@ -81,7 +81,7 @@ test('a rule from an include file says why it cannot be changed here and opens i }); test('a rule is added before the fallback and removed again through validate, save and reload', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const list = rows(page); await expect(list).toHaveCount(9); await page.getByRole('button', {name: 'Add rule', exact: true}).click(); diff --git a/e2e/rules-templates.spec.ts b/e2e/rules-templates.spec.ts index d2bd26fd..4c198f3d 100644 --- a/e2e/rules-templates.spec.ts +++ b/e2e/rules-templates.spec.ts @@ -112,6 +112,27 @@ test('a template replaces the routing of the one file that holds it', async ({pa await expect(applyButton(page)).toBeDisabled(); }); +test('the routing list opens on the simple view for template and custom routing, and a link to a rule on the table', async ({page}) => { + const {api, write} = await backend(page); + const table = page.getByRole('tabpanel', {name: 'Routing rules'}).locator('.rp-table'); + await write(oneFile); + await page.goto('/#/rules?tab=list'); + await expect(modes(page)).toBeVisible(); + await expect(modes(page).getByRole('radio', {checked: true})).toHaveCount(0); + await expect(table).toHaveCount(0); + const defined = allGroupNames((await api.config()).sources); + await write(text => writeTemplate(text, 'bypass', defined)); + await page.goto('/#/rules'); + await expect(modes(page).getByRole('radio', {name: 'Bypass mainland China'})).toBeChecked(); + // A link to one rule, as search and Connections make, lands on the table with the rule selected. + const rule = (await api.rules()).rules[0].rule_id; + await page.goto(`/#/rules?tab=list&rule=${rule}`); + await expect(table).toBeVisible(); + await expect(modes(page)).toHaveCount(0); + await page.goto('/#/rules?view=advanced'); + await expect(table).toBeVisible(); +}); + test('the detected mode is selected, and the arrow keys move the selection through the visible modes', async ({page}) => { const {api, write} = await backend(page); const defined = allGroupNames((await api.config()).sources); diff --git a/e2e/rules.spec.ts b/e2e/rules.spec.ts index f819e619..d5094a4b 100644 --- a/e2e/rules.spec.ts +++ b/e2e/rules.spec.ts @@ -15,7 +15,7 @@ test('the rule list filters by source without accumulating polls, sorted in conf await page.route('**/api/v1/version', async route => route.fulfill({json: await api.version()})); await page.route('**/api/v1/runtime/settings', async route => route.fulfill({json: await api.runtimeSettings()})); await page.route('**/api/v1/flows?*', route => route.fulfill({json: flows})); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const panel = page.getByRole('tabpanel', {name: 'Routing rules'}); const rows = panel.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]'); await expect(rows.first()).toBeVisible(); @@ -67,7 +67,7 @@ test('the rule list keeps exact loss counts and replaces an empty snapshot', asy await page.route('**/api/v1/version', async route => route.fulfill({json: await api.version()})); await page.route('**/api/v1/runtime/settings', async route => route.fulfill({json: await api.runtimeSettings()})); await page.route('**/api/v1/flows?*', route => route.fulfill({json: snapshot})); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const panel = page.getByRole('tabpanel', {name: 'Routing rules'}); const rows = panel.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]'); await expect(rows).toHaveCount(15); @@ -150,7 +150,7 @@ test('without a rule dictionary, Retry refetches the flows the distribution is b }; // Frozen timers: only the retry, not the next poll, can bring the flows back. await page.clock.install(); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const panel = page.getByRole('tabpanel', {name: 'Routing rules'}); const alert = panel.getByRole('alert').filter({hasText: 'Flows unavailable'}); await expect(alert).toBeVisible(); @@ -164,7 +164,7 @@ test('Cancel on a rule write that has landed reads the sources again, so the nex const {api} = await mockBackend(page); // The page's polls of the accepted write never answer; only the backend finishes it. await page.route('**/api/v1/operations/**', () => {}); - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const add = page.getByRole('button', {name: 'Add rule', exact: true}); const dialog = page.getByRole('dialog'); const write = async (value: string) => { @@ -185,7 +185,7 @@ test('Cancel on a rule write that has landed reads the sources again, so the nex }); test('the rule list fits its rows instead of holding a page of empty space', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const table = page.locator('.rp-table', {has: page.getByRole('grid', {name: 'Routing rules'})}); await expect(table.locator('[role=row][data-key]').first()).toBeVisible(); const rows = await table.locator('[role=row][data-key]').count(); diff --git a/e2e/tap-reveal.spec.ts b/e2e/tap-reveal.spec.ts index a5903000..38433cc1 100644 --- a/e2e/tap-reveal.spec.ts +++ b/e2e/tap-reveal.spec.ts @@ -9,7 +9,7 @@ test.describe('on a phone', () => { test.use({viewport: {width: 360, height: 780}, hasTouch: true, isMobile: true}); test('a tap on a truncated rule cell shows it whole until a tap elsewhere', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const cell = firstCut(page); const full = (await cell.textContent())!.trim(); await expect(page.getByRole('tooltip')).toHaveCount(0); @@ -43,7 +43,7 @@ test.describe('on a phone', () => { }); test('a tap in a table whose row press does something else shows the tip and still presses the row', async ({page}) => { - await page.goto('/#/rules?tab=list'); + await page.goto('/#/rules?tab=list&view=advanced'); const cell = firstCut(page); const row = cell.locator('xpath=ancestor::*[@role="row"][1]'); await cell.tap(); diff --git a/src/features/rules/template.test.ts b/src/features/rules/template.test.ts index 648c9ec3..43200b35 100644 --- a/src/features/rules/template.test.ts +++ b/src/features/rules/template.test.ts @@ -2,7 +2,7 @@ import {expect, it} from 'vitest'; import type {ConfigSource} from '../../api/model'; import {translate, type Translator} from '../../i18n'; import {writeTemplate} from '../../dae/setup'; -import {currentTemplate, refusalReason, routingSources, templateImpact, templatesView, templateTarget} from './template'; +import {currentTemplate, refusalReason, routingSources, ruleViewMode, templateImpact, templatesView, templateTarget} from './template'; const t: Translator = (key, params, pluralParam, precision) => translate('en', key, params, pluralParam, precision); const source = (id: string, content: string, kind: ConfigSource['kind'] = 'include'): ConfigSource => ({ @@ -102,3 +102,11 @@ it('reads a quoted group as its own group when listing what a template creates a }); expect(templateImpact('bypass', main, [main], []).reused).toEqual([{name: "'proxy'", pinned: true}]); }); + +it('opens the routing list on the simple view unless the link names a view or points at a rule', () => { + expect(ruleViewMode('')).toBe('simple'); + expect(ruleViewMode('tab=list')).toBe('simple'); + expect(ruleViewMode('view=advanced')).toBe('advanced'); + for (const query of ['tab=list&rule=r1', 'edit=r1', 'add=domain%3Aexample.org', 'held=1']) expect(ruleViewMode(query)).toBe('advanced'); + expect(ruleViewMode('rule=r1&view=simple')).toBe('simple'); +}); diff --git a/src/features/rules/template.ts b/src/features/rules/template.ts index 682a1509..3588d4d9 100644 --- a/src/features/rules/template.ts +++ b/src/features/rules/template.ts @@ -23,6 +23,17 @@ export function templateChoice(id: RuleTemplate, t: Translator): TemplateChoice return {id, name: t(name), help: t(help, {groups: templates[id].groups.map(group => group.name).join(', ')})}; } +export type RuleViewMode = 'simple' | 'advanced'; +// The params that point at one rule: a rule to select, a rule to edit, a condition to add, or held rules to review. +const ruleTargets = ['rule', 'edit', 'add', 'held']; +// The routing list's view: the one `view` names, else the rule table for a link to a rule and the simple view otherwise. +export function ruleViewMode(query: string): RuleViewMode { + const params = new URLSearchParams(query); + const asked = params.get('view'); + if (asked === 'simple' || asked === 'advanced') return asked; + return ruleTargets.some(key => params.has(key)) ? 'advanced' : 'simple'; +} + // The files a person writes whose top level holds a routing block; generated and subscription files hold none. export function routingSources(sources: ConfigSource[]): ConfigSource[] { return sources.filter( diff --git a/src/features/rules/useRuleList.ts b/src/features/rules/useRuleList.ts index 2d398847..9ce3e177 100644 --- a/src/features/rules/useRuleList.ts +++ b/src/features/rules/useRuleList.ts @@ -112,7 +112,8 @@ export function useRuleList({go, query}: PageProps): RuleListModel { anchor: ruleAnchor, kinds: conditionKinds, onClose: () => { - if (seed || edit) go('rules', within(query, {add: null, edit: null})); + // The link opened the rule table; dropping its params keeps the table rather than falling back to the simple view. + if (seed || edit) go('rules', within(query, {add: null, edit: null, view: params.get('view') ?? 'advanced'})); }, link: edit ? {key: `edit:${edit}`, open: edited ? {kind: 'edit', rule: edited, outbound: edited.outbound ?? '', must: edited.must} : null} diff --git a/src/features/rules/useRuleTemplates.ts b/src/features/rules/useRuleTemplates.ts index 997cfef2..02fb74a3 100644 --- a/src/features/rules/useRuleTemplates.ts +++ b/src/features/rules/useRuleTemplates.ts @@ -15,16 +15,17 @@ import {within} from '../../shell/route'; import {lineDiff} from './diff'; import { refusalReason, + ruleViewMode, templateChoice, templateImpact, templatesView, templateTarget, + type RuleViewMode, type TemplateChoice, type TemplateImpact, type TemplatesView } from './template'; -export type RuleViewMode = 'simple' | 'advanced'; // A template being confirmed, over the file as it was read when the dialog opened: that text is what the write's // If-Match names, so a change on disk since is refused rather than overwritten. type Pending = {choice: TemplateChoice; source: ConfigSource; after: string; impact: TemplateImpact; diff: DiffRow[]}; @@ -49,7 +50,7 @@ export type RuleTemplatesModel = TemplatesView & { applying: boolean; }; // The routing list's simple view: the template the routing holds, in plain words, and the templates to replace it with. -// It opens on the simple view when the routing holds a template and on the rule table otherwise. +// It opens on the simple view, template or custom, and on the rule table for a link to a rule. export function useRuleTemplates({go, query}: PageProps): RuleTemplatesModel { const t = useT(); const resources = useCapabilities().data?.resources; @@ -74,14 +75,13 @@ export function useRuleTemplates({go, query}: PageProps): RuleTemplatesModel { holdsCredentials: source => engine.holdsCredentials(source), denied }); - const asked = new URLSearchParams(query).get('view'); const setMode = (mode: string) => go('rules', within(query, {view: mode})); const selected = picked ?? view.current?.id ?? null; const canApply = !!selected && selected !== view.current?.id && !target.refusal && !!target.source && isComplete(target.source) === true && !editor.busy; return { ...view, available: readable && !!config.data, - mode: asked === 'simple' || asked === 'advanced' ? asked : view.current ? 'simple' : 'advanced', + mode: ruleViewMode(query), setMode, file: target.source && fileName(target.source), selected,