Skip to content

Commit c447019

Browse files
feat(console): a settings toggle that declares confirmOffText asks before it is turned off (#30575)
<!-- claude-projects-attribution: {"github_login":"os-sales"} --> _Requested by **Sales** · [project thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuPzNWr1DMSTTyfzcUxqrPoz)_ domain:ui#4 · os-sales · session_01L6bhgX6EQZ4fi7BLKzhfLT Fixes #30500 Refs #30299 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:设置页里声明了 `confirmOffText` 的开关(目前是「认证」页的「启用邮箱/密码登录」),从开拨到关时先弹确认框,显示这段话;点「取消」开关保持开、不进未保存更改、什么都不存;点「继续」开关才变关,再按页面的「保存更改」照常保存。打开开关不问,没声明的开关行为不变。 - 为什么改:#30418 裁决 A 的 ui 半边。spec(PR #30489)已让开关声明 `confirmOffText`,认证 manifest 也声明了,但控制台不读,Setup 关掉密码登录时一句都不问(声明 ≠ 兑现)。 - 风险与回滚:只改控制台设置页的开关一支,复用 `action_button` 确认框已用的 `AlertDialog` 与 `actionConfirm.*` 文案键,不加 locale 键、不加导出;服务器「最后一种登录方式」的拒绝仍在,确认框不替代它。回滚即撤本 PR。 - 先看:`ui/apps/console/src/pages/settings/SettingsField.tsx` 的 `ToggleSwitch` 与 `case 'toggle'`;`useSettingsLabel.ts` 新的 `fieldConfirmOff`;新测试 `SettingsView.confirmOff-30500.test.tsx`。 - 两分钟验证:`pnpm --dir ui exec vitest run apps/console/src/pages/settings/__tests__/SettingsView.confirmOff-30500.test.tsx`,预期 1 个文件、8 条全绿。 - 需要你做的:无,席位落地。 ## What changes The settings page's `toggle` row reads `SpecifierSchema.confirmOffText` ("Confirm dialog text shown before a toggle's value changes to off (omit = no confirm)"), which `auth.manifest.ts` declares on `email_password_enabled`. - `SettingsField.tsx`: the toggle's switch is a new `ToggleSwitch`. When the specifier declares the key and the switch goes from on to off, it opens an `AlertDialog` carrying the text and hands nothing to `onChange`. **Cancel** leaves the switch on and the page's draft untouched, so there is no unsaved change and nothing any Save could send. **Continue** hands `false` to `onChange`; the page's Save button sends it like any other edit. Turning the switch on asks nothing. A toggle without the key changes at once, exactly as before. Judged on the declaration (`!= null`), as the `action_button` arm judges a handler's `confirmText`. The dialog is opened by state rather than an `AlertDialogTrigger` (only one of the switch's two directions asks), so focus is handed back to the switch when it closes. - The text: `settings.NAMESPACE.keys.KEY.confirmOffText` through the page's label helpers, the slot `translation.zod.ts` declares beside the key's `label`; a miss is the manifest literal. The spec types the key as `I18nLabel`, so an inline locale map literal is picked for the page's language with `pickLocalized`. - `useSettingsLabel.ts`: `fieldConfirmOff(key, fallback)`, through the hook's one `resolve()`. - `types.ts`: the mirror gains an optional `confirmOffText`, typed as a string or an inline locale map (a record of language tag to string). - The dialog's title and buttons are the existing `actionConfirm.title` / `cancel` / `confirm` keys the `action_button` confirmation uses; no locale key is added. Live, Setup > Authentication (showcase `objectstack dev --seed-admin`, this branch's console under vite, Playwright 1440 px; "before" is the same page with the confirm's guard ablated live with `ablation-replace.mjs --hold`, then restored): | | en-US | zh-CN | |:--|:--|:--| | before: one click turns password sign-in off, no question | ![before en](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/before-2-after-switch-off-click-en-US-1440.png?raw=true) | ![before zh](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/before-2-after-switch-off-click-zh-CN-1440.png?raw=true) | | after: the click asks first, the switch still on | ![after en](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/after-2-after-switch-off-click-en-US-1440.png?raw=true) | ![after zh](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/after-2-after-switch-off-click-zh-CN-1440.png?raw=true) | zh-CN after Cancel (switch on, no unsaved bar): ![cancel](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/after-3-after-cancel-zh-CN-1440.png?raw=true) zh-CN after Continue (switch off, one unsaved change): ![continue](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/after-4-after-continue-zh-CN-1440.png?raw=true) zh-CN after Save (saved, row reads Modified): ![saved](https://github.com/objectstack-ai/objectstack/blob/3b1df64827d84f9c32806dc2d969e933e136200f/evidence/30500/after-5-after-save-zh-CN-1440.png?raw=true) ## Why Ruling A on #30418 (6657805240), bind (4): the console's settings page honours `confirmOffText` before save. Until now the key was declared with no reader, so Setup turned email and password sign-in off on one click (#30299's last remainder). ## Tests `ui/apps/console/src/pages/settings/__tests__/SettingsView.confirmOff-30500.test.tsx`: the whole page over a stubbed `fetch` and the real `api.ts`, so the save cases read the PUT body as it leaves the client. - declared, on to off: the confirmation carries the text and the switch stays on, nothing drafted; - Cancel: the switch stays on, no unsaved bar, no Save button, zero PUTs, focus back on the switch; - Continue: the switch is off, one unsaved change, Save sends exactly `{"email_password_enabled":false}`; - declared, off to on: no confirmation, Save sends `true`; - asks again on the next turn to off after a Continue and a turn back on; - a toggle without the key: no confirmation, Save sends `false`; - the text from a served `settings.auth.keys.email_password_enabled.confirmOffText`, and an inline locale map literal picked for `de` when nothing is served. ## Acceptance notes - The question sits on the switch, before the off reaches the draft; the page keeps its one write door (Save). So "cancel saves nothing" holds because nothing was drafted, and "confirm saves" is Continue then Save. - Reset to default (the link beside a `Modified` / `Overrides` badge) clears the layer's value without asking. It could land on off only for a toggle whose fallback is off; `email_password_enabled` defaults to `true`, and no other shipped specifier declares `confirmOffText`. Discard returns to the stored value and asks nothing either. - The dialog does not replace the server's last-sign-in-method floor: on the live stack with no other method configured, Continue then Save was refused `400 SETTINGS_VALIDATION` with the server's sentence under the row; with Google sign-in set in the same save it went through `200`. - Observed, not filed (no named producer): the `action_button` arm resolves a handler's `confirmText` with `resolveLabel`, which reads a string or the retired `{ defaultValue, key }` shape, so an inline locale map there would render an empty question. No shipped manifest declares one. Carrier: none. - No package gains an export, so `Clause-②: no` stands. `Metadata-protocol: none`: the spec is untouched and `confirmOffText` already meant this (PR #30489); the console only honours it, and AGENTS.md §3 names the console as `none`, so the changeset is `patch` on `@object-ui/console` (corrected by the seat from the first head's `semantic` / `minor`). - Branch is the card thread's harness-assigned `claude/project-thread-ngbinh`, not `claude/issue-30500-*` (named in the claim amendment). ## Verification <details><summary>Unit tests</summary> - New file, at `f6ce715a`: `pnpm exec vitest run --maxWorkers=2 apps/console/src/pages/settings/__tests__/SettingsView.confirmOff-30500.test.tsx` (in `ui/`, under `os-verify-lock`): `Test Files 1 passed (1)`, `Tests 8 passed (8)`, `VERDICT command-exit 0`. - The settings directory at merge head `4a1e9138` (main with PR #30540 merged in): `pnpm exec vitest run --maxWorkers=2 apps/console/src/pages/settings`: `Test Files 36 passed (36)`, `Tests 305 passed (305)`, `VERDICT command-exit 0`. </details> <details><summary>Ablation</summary> `ablation-replace.mjs` on `SettingsField.tsx`, anchor `if (checked && !next && confirmOffText !== undefined) {` to `if (false) {` (anchor 1 to 0, blob 861c4c8 to fd282923): the new file goes `6 failed | 2 passed (8)`. The six declared-path cases are red; the two that must not ask (off to on, a toggle without the key) stay green, as expected. Restored: blob equals HEAD, `git diff HEAD` empty. </details> <details><summary>Live readings (real stack)</summary> Showcase `objectstack dev --seed-admin` on its own port, the branch's console under vite with `DEV_PROXY_TARGET`, Playwright 1440 px, page `/system/settings/auth`, row `email_password_enabled`. - after, en-US: the click on the switch opens a dialog reading "Confirm Action / People who sign in with a password will need another way in. / Cancel / Continue"; the switch reads `aria-checked=true`, no unsaved bar. Cancel: switch still on, no unsaved bar, focus on the switch. Continue: switch off, "1 unsaved change". Save with no other sign-in method: `PUT /api/settings/auth` body `{"email_password_enabled":false}`, answered `400 SETTINGS_VALIDATION`, the server's sentence under the row. Save with Google sign-in set in the same save: `200`, `email_password_enabled` stored `false`. - after, zh-CN: the dialog reads "确认操作 / 使用密码登录的用户将需要其他登录方式。 / 取消 / 继续" (the text from the server's zh-CN `settings.auth.keys.email_password_enabled.confirmOffText`); Cancel and Continue as above; Save `200`, "设置已保存", the row reads 已修改. - before (guard ablated live, both locales): the click turns the switch off at once with no dialog and one unsaved change; Save sends `false`. </details> <details><summary>Derived gates (dispatch-gates --commands at f6ce715; the list re-derived identical at 4a1e913)</summary> 34 derived, 34 run, all exit 0; `--ran` reconciliation: 34 run, 0 NOT-MEASURED, 0 UNRUN. The ratchet families re-run on the merge head `4a1e9138`: `pnpm check:type-check-coverage :: exit 0`, `pnpm check:type-check-debt :: exit 0`. ```text 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 --dir ui exec vitest run scripts/ :: 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 0 pnpm check:undeclared-dep-imports :: exit 0 pnpm check:watch-hint-literal :: exit 0 pnpm check:where-matcher :: exit 0 ``` Also: `turbo run build --filter='@object-ui/console^...'` then `@object-ui/console` `type-check` at `4a1e9138`: `VERDICT command-exit 0`. `eslint` on the four changed console files: 0 errors (1 warning, on a line this PR does not touch). `pnpm --dir ui run check:new-line-citations`: 0 new citations. Repo-wide lint and the full UI suites are CI's. </details> ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (no spec change) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes. (eslint on the changed files locally; repo-wide lint is CI's) - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: n/a. - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. --- _Generated by [Claude Code](https://claude.ai/code/session_01L6bhgX6EQZ4fi7BLKzhfLT)_ <!-- signed: domain:ui#4 · os-sales · session_01L6bhgVfPeFZkN1FP93imKU --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4128213 commit c447019

5 files changed

Lines changed: 397 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+
feat(console): a settings toggle that declares `confirmOffText` asks before it is turned off
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A settings manifest's `toggle` specifier may declare `confirmOffText` (`SpecifierSchema`: "Confirm dialog text shown before a toggle's value changes to off (omit = no confirm)"), and the authentication manifest declares it on `email_password_enabled`. The Console's settings page did not read the key, so Setup turned email and password sign-in off without asking. Now turning such a toggle off opens a confirmation carrying that text, translated through `settings.NAMESPACE.keys.KEY.confirmOffText` when the locale has it (an inline locale map literal is picked for the page's language otherwise). Cancel leaves the toggle on and puts nothing in the page's unsaved changes, so nothing can be saved; Continue turns it off, and Save sends it like any other edit. Turning a toggle on asks nothing, and a toggle without the key changes at once, exactly as before.

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

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* (which carries provenance and lock state).
77
*/
88

9-
import { cloneElement, isValidElement, lazy, Suspense, useId, useState, type ReactElement } from 'react';
9+
import { cloneElement, isValidElement, lazy, Suspense, useId, useRef, useState, type ReactElement } from 'react';
1010
import {
1111
Input,
1212
Textarea,
@@ -39,7 +39,7 @@ import {
3939
cn,
4040
getLazyIcon,
4141
} from '@object-ui/components';
42-
import { useObjectTranslation } from '@object-ui/i18n';
42+
import { pickLocalized, useObjectTranslation } from '@object-ui/i18n';
4343
import { AlertTriangle, ChevronRight, Info, RotateCcw } from 'lucide-react';
4444
import { EnvLockBadge } from './EnvLockBadge';
4545
import {
@@ -402,11 +402,91 @@ function DomainCombobox({
402402
);
403403
}
404404

405+
/**
406+
* <ToggleSwitch> — a `toggle` row's control.
407+
*
408+
* A specifier that declares `confirmOffText` (`SpecifierSchema`: "Confirm
409+
* dialog text shown before a toggle's value changes to off (omit = no
410+
* confirm)") asks before the switch goes from on to off. Cancel leaves the
411+
* switch on and hands nothing to `onChange`, so the page's draft never holds
412+
* the off and no Save can send it; Continue hands `false` to `onChange`, and
413+
* the page's Save sends it like any other edit. Turning the switch on asks
414+
* nothing, and a toggle without the key changes at once, as before.
415+
*
416+
* Judged on the DECLARATION (`confirmText != null`), as the `action_button`
417+
* arm judges a handler's `confirmText`. The dialog is opened by state, never by
418+
* an `AlertDialogTrigger`, since only one of the switch's two directions asks;
419+
* so focus is handed back to the switch by hand when it closes.
420+
*
421+
* Props beyond its own reach the `<Switch>`, so the row's `aria-invalid` /
422+
* `aria-describedby` land on the focusable control.
423+
*/
424+
function ToggleSwitch({
425+
id,
426+
checked,
427+
disabled,
428+
confirmOffText,
429+
onChange,
430+
...switchProps
431+
}: {
432+
id: string;
433+
checked: boolean;
434+
disabled?: boolean;
435+
/** The resolved question, or `undefined` when the specifier declares none. */
436+
confirmOffText?: string;
437+
onChange: (next: boolean) => void;
438+
'aria-invalid'?: boolean;
439+
'aria-describedby'?: string;
440+
}) {
441+
const { t } = useObjectTranslation();
442+
const [asking, setAsking] = useState(false);
443+
const switchRef = useRef<HTMLButtonElement>(null);
444+
return (
445+
<>
446+
<Switch
447+
ref={switchRef}
448+
id={id}
449+
checked={checked}
450+
disabled={disabled}
451+
onCheckedChange={(next) => {
452+
if (checked && !next && confirmOffText !== undefined) {
453+
setAsking(true);
454+
return;
455+
}
456+
onChange(next);
457+
}}
458+
{...switchProps}
459+
/>
460+
{confirmOffText !== undefined ? (
461+
<AlertDialog open={asking} onOpenChange={setAsking}>
462+
<AlertDialogContent
463+
onCloseAutoFocus={(e) => {
464+
e.preventDefault();
465+
switchRef.current?.focus();
466+
}}
467+
>
468+
<AlertDialogHeader>
469+
<AlertDialogTitle>{t('actionConfirm.title')}</AlertDialogTitle>
470+
<AlertDialogDescription>{confirmOffText}</AlertDialogDescription>
471+
</AlertDialogHeader>
472+
<AlertDialogFooter>
473+
<AlertDialogCancel>{t('actionConfirm.cancel')}</AlertDialogCancel>
474+
<AlertDialogAction onClick={() => onChange(false)}>
475+
{t('actionConfirm.confirm')}
476+
</AlertDialogAction>
477+
</AlertDialogFooter>
478+
</AlertDialogContent>
479+
</AlertDialog>
480+
) : null}
481+
</>
482+
);
483+
}
484+
405485
export function SettingsField(props: SettingsFieldProps) {
406486
const { spec, resolved, value, onChange, onAction, locked, saving, labels, error, note, writeScope } =
407487
props;
408488
const id = useId();
409-
const { t } = useObjectTranslation();
489+
const { t, language } = useObjectTranslation();
410490
const disabled = Boolean(locked || saving);
411491
const literalLabel = resolveLabel(spec.label);
412492
// Field-scoped label/help/placeholder/option resolution. Falls back to the
@@ -685,7 +765,17 @@ export function SettingsField(props: SettingsFieldProps) {
685765
onChange={(e) => onChange(e.target.value === '' ? null : Number(e.target.value))}
686766
/>,
687767
);
688-
case 'toggle':
768+
case 'toggle': {
769+
// The question a declared `confirmOffText` asks, through the
770+
// `keys.<key>.confirmOffText` slot beside the row's label; a miss is the
771+
// manifest literal, an inline locale map picked for the page's language.
772+
const declaredConfirmOff = spec.confirmOffText;
773+
const literalConfirmOff =
774+
declaredConfirmOff != null ? pickLocalized(declaredConfirmOff, language) : undefined;
775+
const confirmOffText =
776+
literalConfirmOff !== undefined && spec.key && labels
777+
? labels.fieldConfirmOff(spec.key, literalConfirmOff)
778+
: literalConfirmOff;
689779
return (
690780
<div className="flex items-center justify-between py-3" data-settings-key={spec.key}>
691781
<div>
@@ -707,16 +797,18 @@ export function SettingsField(props: SettingsFieldProps) {
707797
<FieldDescription description={fieldHelp} />
708798
)}
709799
</div>
710-
<Switch
800+
<ToggleSwitch
711801
id={id}
712802
aria-invalid={error ? true : undefined}
713803
aria-describedby={error ? errorId : undefined}
714804
checked={Boolean(value)}
715805
disabled={disabled}
716-
onCheckedChange={(checked) => onChange(checked)}
806+
confirmOffText={confirmOffText}
807+
onChange={(checked) => onChange(checked)}
717808
/>
718809
</div>
719810
);
811+
}
720812
case 'select': {
721813
// Keyed off the DECLARATION, never off the key: a key that gains a
722814
// domain server-side gets the right control here with no edit.

0 commit comments

Comments
 (0)