Skip to content

Commit f6ce715

Browse files
committed
feat(console): a settings toggle that declares confirmOffText asks before it is turned off
The settings page's toggle row reads SpecifierSchema.confirmOffText: turning the switch from on to off opens a confirmation carrying the text (resolved through settings.NAMESPACE.keys.KEY.confirmOffText, else the manifest literal); Cancel leaves the switch on with nothing drafted, Continue drafts false for the page's Save. Turning on asks nothing; a toggle without the key is unchanged. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L6bhgX6EQZ4fi7BLKzhfLT
1 parent 8e5fabe commit f6ce715

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': minor
3+
---
4+
5+
feat(console): a settings toggle that declares `confirmOffText` asks before it is turned off
6+
7+
Clause-②: no
8+
Metadata-protocol: semantic
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 {
@@ -394,11 +394,91 @@ function DomainCombobox({
394394
);
395395
}
396396

397+
/**
398+
* <ToggleSwitch> — a `toggle` row's control.
399+
*
400+
* A specifier that declares `confirmOffText` (`SpecifierSchema`: "Confirm
401+
* dialog text shown before a toggle's value changes to off (omit = no
402+
* confirm)") asks before the switch goes from on to off. Cancel leaves the
403+
* switch on and hands nothing to `onChange`, so the page's draft never holds
404+
* the off and no Save can send it; Continue hands `false` to `onChange`, and
405+
* the page's Save sends it like any other edit. Turning the switch on asks
406+
* nothing, and a toggle without the key changes at once, as before.
407+
*
408+
* Judged on the DECLARATION (`confirmText != null`), as the `action_button`
409+
* arm judges a handler's `confirmText`. The dialog is opened by state, never by
410+
* an `AlertDialogTrigger`, since only one of the switch's two directions asks;
411+
* so focus is handed back to the switch by hand when it closes.
412+
*
413+
* Props beyond its own reach the `<Switch>`, so the row's `aria-invalid` /
414+
* `aria-describedby` land on the focusable control.
415+
*/
416+
function ToggleSwitch({
417+
id,
418+
checked,
419+
disabled,
420+
confirmOffText,
421+
onChange,
422+
...switchProps
423+
}: {
424+
id: string;
425+
checked: boolean;
426+
disabled?: boolean;
427+
/** The resolved question, or `undefined` when the specifier declares none. */
428+
confirmOffText?: string;
429+
onChange: (next: boolean) => void;
430+
'aria-invalid'?: boolean;
431+
'aria-describedby'?: string;
432+
}) {
433+
const { t } = useObjectTranslation();
434+
const [asking, setAsking] = useState(false);
435+
const switchRef = useRef<HTMLButtonElement>(null);
436+
return (
437+
<>
438+
<Switch
439+
ref={switchRef}
440+
id={id}
441+
checked={checked}
442+
disabled={disabled}
443+
onCheckedChange={(next) => {
444+
if (checked && !next && confirmOffText !== undefined) {
445+
setAsking(true);
446+
return;
447+
}
448+
onChange(next);
449+
}}
450+
{...switchProps}
451+
/>
452+
{confirmOffText !== undefined ? (
453+
<AlertDialog open={asking} onOpenChange={setAsking}>
454+
<AlertDialogContent
455+
onCloseAutoFocus={(e) => {
456+
e.preventDefault();
457+
switchRef.current?.focus();
458+
}}
459+
>
460+
<AlertDialogHeader>
461+
<AlertDialogTitle>{t('actionConfirm.title')}</AlertDialogTitle>
462+
<AlertDialogDescription>{confirmOffText}</AlertDialogDescription>
463+
</AlertDialogHeader>
464+
<AlertDialogFooter>
465+
<AlertDialogCancel>{t('actionConfirm.cancel')}</AlertDialogCancel>
466+
<AlertDialogAction onClick={() => onChange(false)}>
467+
{t('actionConfirm.confirm')}
468+
</AlertDialogAction>
469+
</AlertDialogFooter>
470+
</AlertDialogContent>
471+
</AlertDialog>
472+
) : null}
473+
</>
474+
);
475+
}
476+
397477
export function SettingsField(props: SettingsFieldProps) {
398478
const { spec, resolved, value, onChange, onAction, locked, saving, labels, error, writeScope } =
399479
props;
400480
const id = useId();
401-
const { t } = useObjectTranslation();
481+
const { t, language } = useObjectTranslation();
402482
const disabled = Boolean(locked || saving);
403483
const literalLabel = resolveLabel(spec.label);
404484
// Field-scoped label/help/placeholder/option resolution. Falls back to the
@@ -668,7 +748,17 @@ export function SettingsField(props: SettingsFieldProps) {
668748
onChange={(e) => onChange(e.target.value === '' ? null : Number(e.target.value))}
669749
/>,
670750
);
671-
case 'toggle':
751+
case 'toggle': {
752+
// The question a declared `confirmOffText` asks, through the
753+
// `keys.<key>.confirmOffText` slot beside the row's label; a miss is the
754+
// manifest literal, an inline locale map picked for the page's language.
755+
const declaredConfirmOff = spec.confirmOffText;
756+
const literalConfirmOff =
757+
declaredConfirmOff != null ? pickLocalized(declaredConfirmOff, language) : undefined;
758+
const confirmOffText =
759+
literalConfirmOff !== undefined && spec.key && labels
760+
? labels.fieldConfirmOff(spec.key, literalConfirmOff)
761+
: literalConfirmOff;
672762
return (
673763
<div className="flex items-center justify-between py-3" data-settings-key={spec.key}>
674764
<div>
@@ -690,16 +780,18 @@ export function SettingsField(props: SettingsFieldProps) {
690780
<FieldDescription description={fieldHelp} />
691781
)}
692782
</div>
693-
<Switch
783+
<ToggleSwitch
694784
id={id}
695785
aria-invalid={error ? true : undefined}
696786
aria-describedby={error ? errorId : undefined}
697787
checked={Boolean(value)}
698788
disabled={disabled}
699-
onCheckedChange={(checked) => onChange(checked)}
789+
confirmOffText={confirmOffText}
790+
onChange={(checked) => onChange(checked)}
700791
/>
701792
</div>
702793
);
794+
}
703795
case 'select': {
704796
// Keyed off the DECLARATION, never off the key: a key that gains a
705797
// domain server-side gets the right control here with no edit.

0 commit comments

Comments
 (0)