Skip to content

Commit a1d0b1e

Browse files
fix(console): a settings action whose handler declares confirmText asks before invoking (#30470)
<!-- claude-projects-attribution: {"github_login":"os-sales"} --> _Requested by **Sales** · [project thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuXpPPZiBKyj1HzrdQ6La3ZH)_ domain:ui#6 · os-sales · session_01L6bhgUR8Nu4M7tbtgES7q2 Fixes #30419 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:设置页的操作按钮(`action_button`)若在 manifest 里声明了 `confirmText`,点击后先弹出确认框显示该文字;取消则什么都不执行,确认才执行一次。未声明的按钮行为不变。 - 为什么改:spec 早已声明「执行前确认」(`SpecifierHandlerSchema` 的 `http` / `action` 两臂),但控制台从不读取,作者标记为需确认的危险操作一点就跑(声明 ≠ 兑现)。 - 风险与回滚:仅改控制台一个渲染分支,复用现有 `AlertDialog` 原语与已有 `actionConfirm.*` 文案键,不加新组件、不加新 locale 键;回滚即 revert 本 PR。 - 先看:`ui/apps/console/src/pages/settings/SettingsField.tsx` 的 `action_button` 分支(`declaredConfirm` 一段);其次新测试 `SettingsField.actionConfirm.test.tsx`。 - 两分钟验证:`pnpm --dir ui exec vitest run apps/console/src/pages/settings/__tests__/SettingsField.actionConfirm.test.tsx`,预期 11 passed。 - 需要你做的:无,席位落地。 ## What changes - `SettingsField`'s `action_button` row reads the handler's declared `confirmText` (the spec's `SpecifierHandlerSchema` `http` / `action` arms: "Confirm dialog text before invoking (omit = no confirm)"). - Declared (judged `confirmText != null`, the same test the spec's action resolver applies when it decides an action opens a dialog): the button becomes the trigger of the console's existing `AlertDialog` primitive from `@object-ui/components` (the one the Approvals Inbox confirms with). The dialog body is the text resolved the way this file resolves the row's other manifest copy: `labels.actionConfirm(actionId, resolveLabel(confirmText))`, i.e. the already-existing, until now unread `settings.NAMESPACE.actions.ID.confirmText` slot with the manifest literal as fallback. Title / Cancel / Continue reuse the shared `actionConfirm.*` keys `ActionConfirmDialog` uses, so no locale key is added. Cancel invokes nothing; Continue invokes `onAction` once. - Not declared, and a `navigate` handler (which has no such key): the button is rendered exactly as before, `onClick={onAction}`, no dialog. - Outside the source-badge / reset-to-default region; no `packages/**` change; no change to the `types.ts` mirror. ## Tests New `ui/apps/console/src/pages/settings/__tests__/SettingsField.actionConfirm.test.tsx` (11 tests): - for an `http` and an `action` handler with `confirmText`: click opens an alertdialog carrying the text and invokes nothing; Cancel closes it and invokes nothing; Continue invokes exactly once; - the `actions.ID.confirmText` translation is what the dialog shows, and the helper is asked with the manifest literal as fallback; - no `confirmText` (`http`, `action`), a `navigate` handler, and no handler: click invokes once, no dialog. ## Acceptance notes - The settings mirror (`types.ts`) types `confirmText` as a plain string while the spec declares an `I18nLabel` (string or inline locale map). The same mirror gap already holds for every specifier `label` here (`resolveLabel` reads a string or a `defaultValue`/`key` object, not an inline locale map). This PR keeps that resolution unchanged; because the dialog opens on the declaration, not on the resolved text, an inline-map `confirmText` still asks (with the generic title) rather than running unasked. Not filed: observation, no reach measured. ## Verification All readings on `objectstack-ai/objectstack`, branch head `470a4eb5` (tests run at `21978cf3`; the only later commit adds the changeset). <details><summary>Positive control: the declared-confirm tests fail on main's SettingsField</summary> `git restore --source=677ac3bb -- ui/apps/console/src/pages/settings/SettingsField.tsx` (trap-restored from HEAD, hash re-compared after), then `pnpm --dir ui exec vitest run --reporter=verbose apps/console/src/pages/settings/__tests__/SettingsField.actionConfirm.test.tsx`: ``` Tests 7 failed | 4 passed (11) os-verify-lock: VERDICT command-exit 1 ``` The 7 failures are exactly the declared-confirm cases (http x3, action x3, translation); the 4 undeclared cases pass on main, as they must. </details> <details><summary>With the fix</summary> ``` pnpm --dir ui exec vitest run --reporter=verbose apps/console/src/pages/settings/__tests__/SettingsField.actionConfirm.test.tsx Test Files 1 passed (1) Tests 11 passed (11) os-verify-lock: VERDICT command-exit 0 pnpm --dir ui exec vitest run apps/console/src/pages/settings/__tests__/ Test Files 32 passed (32) Tests 262 passed (262) os-verify-lock: VERDICT command-exit 0 ``` (The settings directory run includes `settingsPage.noBareLiterals-12821.test.ts`, which reads `SettingsField.tsx` for bare rendered literals.) </details> <details><summary>Lint</summary> `cd ui && pnpm exec eslint apps/console/src/pages/settings/SettingsField.tsx apps/console/src/pages/settings/__tests__/SettingsField.actionConfirm.test.tsx` → exit 0, `0 errors, 2 warnings` (both pre-existing `no-explicit-any` lines outside this diff). `pnpm --dir ui run check:new-line-citations` → `VERDICT new-cross-file-line-citations: 0 new citation(s)`. </details> <details><summary>Derived gates (`node scripts/pm/dispatch-gates.mjs --commands`, 34 commands)</summary> - `node scripts/check-adr-0087-registration.mjs --base origin/main` → exit 0 - `node scripts/check-adr-0087-registration.mjs --self-test` → exit 0 - `node scripts/check-changeset-no-major.mjs --base origin/main` → exit 0 - `node scripts/check-closing-keyword-parity.mjs` → exit 0 - `node scripts/check-closing-keyword-parity.mjs --self-test` → exit 0 - `node scripts/check-comment-mask-corpus.mjs` → exit 0 - `node scripts/check-empty-changeset.mjs --base origin/main` → exit 0 - `node scripts/check-empty-changeset.mjs --self-test` → exit 0 - `node scripts/check-sdui-manifest.mjs` → exit 0 - `node scripts/check-sdui-manifest.mjs --self-test` → exit 0 - `node scripts/pm/release-rehearsal-clone.mjs --self-test` → exit 0 - `node scripts/release-pending-publish.mjs --self-test` → exit 0 - `pnpm --filter @objectstack/spec run check:duration-unit-keys` → exit 0 - `pnpm check:changeset-gate-self-tests` → exit 0 - `pnpm check:closed-ceremony-text` → exit 0 - `pnpm check:cross-package-test-inputs` → exit 0 - `pnpm check:dispatcher-error-vocabulary` → exit 0 - `pnpm check:driver-memory-census` → exit 0 - `pnpm check:engine-double-contract` → exit 0 - `pnpm check:gitlink-declared` → exit 0 - `pnpm check:nul-bytes` → exit 0 - `pnpm check:published-files` → exit 0 - `pnpm check:query-options-erasure` → exit 0 - `pnpm check:refd-timer-probe` → exit 0 - `pnpm check:registry-log-declared` → exit 0 - `pnpm check:rest-log-spy-declared` → exit 0 - `pnpm check:test-source-alias` → exit 0 - `pnpm check:tier-file-adoption` → exit 0 - `pnpm check:type-check-coverage` → exit 0 - `pnpm check:type-check-debt` → exit 3 - `pnpm check:undeclared-dep-imports` → exit 0 - `pnpm check:watch-hint-literal` → exit 0 - `pnpm check:where-matcher` → exit 0 - `pnpm check:changeset-fixed` → exit 0 `pnpm check:type-check-debt` exit 3 is the gate's PREREQUISITE NOT MET (it refused to measure), read as NOT MEASURED, not a finding; this diff touches no package ledger. `pnpm --dir ui exec vitest run scripts/` not run locally (scripts tree untouched); CI runs it. </details> <details><summary>NOT MEASURED</summary> - Console type-check (`pnpm --filter @object-ui/console run type-check`): NOT MEASURED, reason: it needs the `@object-ui/*` dist closure built; the shared verify lock queue-timed out twice (exit 99, 9 min each) behind another run, and an unlocked attempt without that build answers only TS2307 missing-module noise. CI's `Type Check UI` measures it. </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent cd7f785 commit a1d0b1e

3 files changed

Lines changed: 177 additions & 7 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/console': patch
3+
---
4+
5+
fix(console): a settings action whose handler declares `confirmText` asks before it runs
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The settings manifest's action handler (`SpecifierHandlerSchema`, its `http` and `action` arms) declares `confirmText`: "Confirm dialog text before invoking (omit = no confirm)". The Console's `action_button` row never read it, so an action its manifest marked as needing a confirmation ran on the first click. Now a declared `confirmText` opens a confirmation dialog carrying that text (translated through `settings.NAMESPACE.actions.ID.confirmText` when the locale has it); Cancel runs nothing and Continue runs the action once. A handler without `confirmText`, and a `navigate` handler, runs on click exactly as before.

‎ui/apps/console/src/pages/settings/SettingsField.tsx‎

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ import {
2727
Separator,
2828
Badge,
2929
Button,
30+
AlertDialog,
31+
AlertDialogAction,
32+
AlertDialogCancel,
33+
AlertDialogContent,
34+
AlertDialogDescription,
35+
AlertDialogFooter,
36+
AlertDialogHeader,
37+
AlertDialogTitle,
38+
AlertDialogTrigger,
3039
cn,
3140
getLazyIcon,
3241
} from '@object-ui/components';
@@ -485,20 +494,57 @@ export function SettingsField(props: SettingsFieldProps) {
485494
// One label per action row (objectui#12610): the button carries it. The
486495
// row's left side holds the manifest's one-line `description` when it
487496
// declares one, and nothing otherwise — the label is never drawn twice.
497+
//
498+
// A handler that declares `confirmText` (the spec's `SpecifierHandlerSchema`
499+
// `http` / `action` arms: "omit = no confirm") asks before it invokes:
500+
// cancel invokes nothing. Judged on the DECLARATION, as the spec's action
501+
// resolver judges `confirmText != null`; the text resolves through the
502+
// `actions.<id>.confirmText` slot with the manifest literal as fallback.
503+
const declaredConfirm =
504+
spec.handler && spec.handler.kind !== 'navigate' ? spec.handler.confirmText : undefined;
505+
const confirmText = declaredConfirm != null
506+
? (labels
507+
? labels.actionConfirm(actionId, resolveLabel(declaredConfirm))
508+
: resolveLabel(declaredConfirm))
509+
: undefined;
510+
const actionButton = (
511+
<Button
512+
size="sm"
513+
variant="secondary"
514+
onClick={declaredConfirm != null ? undefined : onAction}
515+
disabled={saving}
516+
>
517+
{Icon ? (
518+
// eslint-disable-next-line react-hooks/static-components -- getLazyIcon returns a module-cached stable component per name, not one created during render
519+
<Icon className="h-4 w-4 mr-1.5" />
520+
) : null}
521+
{actionLabel}
522+
</Button>
523+
);
488524
return (
489525
<div
490526
className={`flex items-center gap-4 py-3 ${spec.description ? 'justify-between' : 'justify-end'}`}
491527
>
492528
{spec.description ? (
493529
<p className="text-sm text-muted-foreground">{spec.description}</p>
494530
) : null}
495-
<Button size="sm" variant="secondary" onClick={onAction} disabled={saving}>
496-
{Icon ? (
497-
// eslint-disable-next-line react-hooks/static-components -- getLazyIcon returns a module-cached stable component per name, not one created during render
498-
<Icon className="h-4 w-4 mr-1.5" />
499-
) : null}
500-
{actionLabel}
501-
</Button>
531+
{declaredConfirm != null ? (
532+
<AlertDialog>
533+
<AlertDialogTrigger asChild>{actionButton}</AlertDialogTrigger>
534+
<AlertDialogContent>
535+
<AlertDialogHeader>
536+
<AlertDialogTitle>{t('actionConfirm.title')}</AlertDialogTitle>
537+
<AlertDialogDescription>{confirmText}</AlertDialogDescription>
538+
</AlertDialogHeader>
539+
<AlertDialogFooter>
540+
<AlertDialogCancel>{t('actionConfirm.cancel')}</AlertDialogCancel>
541+
<AlertDialogAction onClick={onAction}>{t('actionConfirm.confirm')}</AlertDialogAction>
542+
</AlertDialogFooter>
543+
</AlertDialogContent>
544+
</AlertDialog>
545+
) : (
546+
actionButton
547+
)}
502548
</div>
503549
);
504550
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* An `action_button` whose handler declares `confirmText` asks before it
11+
* invokes.
12+
*
13+
* The spec's `SpecifierHandlerSchema` declares `confirmText` on its `http` and
14+
* `action` arms ("Confirm dialog text before invoking (omit = no confirm)").
15+
* The pins:
16+
*
17+
* - declared → the click opens a confirmation carrying that text; Cancel
18+
* invokes nothing; Continue invokes exactly once;
19+
* - the text resolves through `settings.NAMESPACE.actions.ID.confirmText`
20+
* when the page's label helpers carry a translation for it;
21+
* - undeclared (and a `navigate` handler, which has no such key) → the click
22+
* invokes at once, with no dialog, as before.
23+
*/
24+
25+
import type { ReactElement } from 'react';
26+
import { describe, it, expect, vi, afterEach } from 'vitest';
27+
import { render, screen, cleanup, fireEvent } from '@testing-library/react';
28+
import '@testing-library/jest-dom';
29+
import { I18nProvider } from '@object-ui/i18n';
30+
31+
import { SettingsField } from '../SettingsField';
32+
import type { Specifier } from '../types';
33+
import type { SettingsLabelHelpers } from '../useSettingsLabel';
34+
35+
afterEach(() => {
36+
cleanup();
37+
});
38+
39+
function withI18n(ui: ReactElement) {
40+
return <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}>{ui}</I18nProvider>;
41+
}
42+
43+
const CONFIRM = 'Rebuild the whole index? Searches are slower until it finishes.';
44+
45+
function actionSpec(handler: Specifier['handler']): Specifier {
46+
return { type: 'action_button', id: 'rebuild_index', label: 'Rebuild index', handler };
47+
}
48+
49+
function renderAction(spec: Specifier, labels?: SettingsLabelHelpers) {
50+
const onAction = vi.fn();
51+
render(withI18n(<SettingsField spec={spec} value={undefined} onChange={() => {}} onAction={onAction} labels={labels} />));
52+
return onAction;
53+
}
54+
55+
describe('SettingsField action_button — a declared confirmText asks before invoking', () => {
56+
for (const handler of [
57+
{ kind: 'http', method: 'POST', url: '/api/v1/search/rebuild', confirmText: CONFIRM },
58+
{ kind: 'action', name: 'rebuild_index', confirmText: CONFIRM },
59+
] as const) {
60+
describe(`a ${handler.kind} handler`, () => {
61+
it('opens a confirmation carrying the declared text and invokes nothing on click', () => {
62+
const onAction = renderAction(actionSpec(handler));
63+
fireEvent.click(screen.getByRole('button', { name: 'Rebuild index' }));
64+
const dialog = screen.getByRole('alertdialog');
65+
expect(dialog).toHaveTextContent(CONFIRM);
66+
expect(onAction).not.toHaveBeenCalled();
67+
});
68+
69+
it('Cancel closes the confirmation and invokes nothing', () => {
70+
const onAction = renderAction(actionSpec(handler));
71+
fireEvent.click(screen.getByRole('button', { name: 'Rebuild index' }));
72+
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
73+
expect(screen.queryByRole('alertdialog')).toBeNull();
74+
expect(onAction).not.toHaveBeenCalled();
75+
});
76+
77+
it('Continue invokes the action exactly once', () => {
78+
const onAction = renderAction(actionSpec(handler));
79+
fireEvent.click(screen.getByRole('button', { name: 'Rebuild index' }));
80+
fireEvent.click(screen.getByRole('button', { name: 'Continue' }));
81+
expect(onAction).toHaveBeenCalledTimes(1);
82+
expect(screen.queryByRole('alertdialog')).toBeNull();
83+
});
84+
});
85+
}
86+
87+
it('shows the translation the actions.ID.confirmText slot carries', () => {
88+
const actionConfirm = vi.fn((_id: string, _fallback: string | undefined) => 'Translated confirmation');
89+
const labels = {
90+
actionLabel: (_id: string, fallback: string) => fallback,
91+
actionConfirm,
92+
} as unknown as SettingsLabelHelpers;
93+
renderAction(actionSpec({ kind: 'action', name: 'rebuild_index', confirmText: CONFIRM }), labels);
94+
fireEvent.click(screen.getByRole('button', { name: 'Rebuild index' }));
95+
expect(screen.getByRole('alertdialog')).toHaveTextContent('Translated confirmation');
96+
expect(actionConfirm).toHaveBeenCalledWith('rebuild_index', CONFIRM);
97+
});
98+
});
99+
100+
describe('SettingsField action_button — no confirmText invokes on click', () => {
101+
for (const handler of [
102+
{ kind: 'http', method: 'POST', url: '/api/v1/search/rebuild' },
103+
{ kind: 'action', name: 'rebuild_index' },
104+
{ kind: 'navigate', url: '/system/search' },
105+
undefined,
106+
] as const) {
107+
it(`${handler ? `a ${handler.kind} handler` : 'no handler'} invokes at once with no dialog`, () => {
108+
const onAction = renderAction(actionSpec(handler));
109+
fireEvent.click(screen.getByRole('button', { name: 'Rebuild index' }));
110+
expect(onAction).toHaveBeenCalledTimes(1);
111+
expect(screen.queryByRole('alertdialog')).toBeNull();
112+
});
113+
}
114+
});

0 commit comments

Comments
 (0)