fix(settings): destructive session and device controls ask before firing

Signing every other device out, revoking a single session, revoking a
trusted device and revoking them all each fired on a single tap. None is
undoable — the other devices simply have to sign in again, and a revoked
trusted device asks for its second factor next time — while every other
destructive surface in the app confirms first. On a shared screen a
mis-tap is a support call.

A sweep of the rest of the tree found two more hard deletes with neither
a confirmation nor an undo: a Coach reminder and a travel entry. Both now
confirm. Deleting a document deliberately does not: it soft-deletes and
offers an undo, which covers the mis-tap.

The four controls share one ConfirmButton rather than four copies of the
dialog, so the next destructive control gets the confirmation by using
it instead of by someone remembering. Copy is per control and states the
actual consequence — no claim of more finality than is true.
This commit is contained in:
Marc-André Bombeck committed 2026-07-19 12:46:04 +02:00
1 parent 571e415eaa
commit 670e7097a2
12 files changed
+361 -88

No files matched your search

@@ -0,0 +1,67 @@
import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
/**
* Destructive settings controls ask first.
*
* Signing every other device out, and revoking a trusted device, used to fire
* on a single tap. Neither is undoable — the other devices simply have to log
* in again, and a revoked trusted device asks for a second factor next time —
* while every other destructive surface in the app confirms. On a shared
* screen a mis-tap is a support call.
*
* This reads the sources rather than rendering, because what it pins is
* structural: that these particular controls route through `ConfirmButton`
* and never call a revoke mutation straight from an `onClick`. A render test
* would prove the dialog exists in one arrangement; this proves no control on
* these cards can go back to firing directly.
*/
const CARDS = [
{
file: "security-sessions-card.tsx",
slots: ["revoke-session", "sign-out-everywhere"],
},
{
file: "trusted-devices-card.tsx",
slots: ["revoke-trusted-device", "revoke-all-trusted-devices"],
},
];
function source(file: string) {
return readFileSync(
join(process.cwd(), "src", "components", "settings", file),
"utf8",
);
}
describe("destructive session controls confirm before firing", () => {
for (const card of CARDS) {
describe(card.file, () => {
const src = source(card.file);
it("routes every destructive control through ConfirmButton", () => {
expect(src).toContain("ConfirmButton");
for (const slot of card.slots) {
expect(src).toContain(`slot="${slot}"`);
}
});
it("never fires a revoke straight from an onClick", () => {
// `onConfirm={() => revoke…}` is the confirmed path and is fine;
// `onClick={() => revoke…}` is the unguarded one this forbids.
const unguarded = src.match(/onClick=\{\(\) =>\s*revoke/g) ?? [];
expect(unguarded).toEqual([]);
});
it("gives each control its own confirmation body", () => {
// A shared generic body would let one control's dialog describe
// another's consequence — the reason the copy is per-control.
const bodies = src.match(/body=\{t\("([^"]+)"/g) ?? [];
expect(bodies.length).toBe(card.slots.length);
expect(new Set(bodies).size).toBe(card.slots.length);
});
});
}
});
@@ -18,13 +18,14 @@
* Coach surface like the rest of the memory controls.
*/
import { useMemo } from "react";
import { BellRing, Check, Loader2, Trash2, X } from "lucide-react";
import { BellRing, Check, Trash2, X } from "lucide-react";
import { Button } from "@/components/ui/button";
import { EmptyState } from "@/components/ui/empty-state";
import { formatDateOrRelative } from "@/lib/format";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { ConfirmButton } from "@/components/settings/confirm-button";
import { useTranslations } from "@/lib/i18n/context";
import {
useCoachReminders,
@@ -129,25 +130,21 @@ export function CoachRemindersSection({
{t("settings.ai.coachReminders.dismiss")}
</Button>
)}
<Button
type="button"
<ConfirmButton
slot="coach-reminder-delete"
variant="ghost"
size="sm"
className="text-muted-foreground hover:text-destructive min-h-9"
disabled={!isAuthenticated || busy}
data-testid="settings-coach-reminder-delete"
aria-label={t("settings.ai.coachReminders.deleteAria")}
onClick={() => remove.mutate(r.id)}
>
{busy ? (
<Loader2
className="size-3.5 animate-spin motion-reduce:animate-none"
aria-hidden
/>
) : (
<Trash2 className="size-3.5" aria-hidden />
)}
</Button>
disabled={!isAuthenticated}
pending={busy}
ariaLabel={t("settings.ai.coachReminders.deleteAria")}
label=""
icon={<Trash2 className="size-3.5" aria-hidden />}
title={t("settings.ai.coachReminders.deleteConfirmTitle")}
body={t("settings.ai.coachReminders.deleteConfirmBody")}
confirmLabel={t("settings.ai.coachReminders.deleteConfirmAction")}
onConfirm={() => remove.mutate(r.id)}
/>
</div>
</li>
);
+119
View File
@@ -0,0 +1,119 @@
"use client";
/**
* A destructive settings control that asks first.
*
* The security card's "sign out everywhere" and its per-row revoke, and the
* same pair on trusted devices, fired on a single tap with no confirmation —
* while every other destructive surface in the app confirms. Signing every
* other device out is not recoverable by an undo; the other devices simply
* have to log in again, and on a shared screen a mis-tap is a support call.
*
* Wrapping the pattern rather than inlining a fourth dialog is the point: the
* next destructive control gets the confirmation by using this, instead of by
* someone remembering to add one.
*
* The copy contract matches the rest of the app after the delete-confirmation
* sweep — say what happens, and claim no more finality than is true. Callers
* pass their own body text so each control can be specific about its own
* consequence.
*/
import type { ReactNode } from "react";
import { Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { useTranslations } from "@/lib/i18n/context";
export function ConfirmButton({
label,
title,
body,
confirmLabel,
onConfirm,
pending = false,
disabled = false,
size,
variant = "outline",
className,
icon,
ariaLabel,
slot,
}: {
/** The control's own label, before the dialog opens. */
label: string;
title: string;
body: string;
/** The dialog's action label. Names the act, not "OK". */
confirmLabel: string;
onConfirm: () => void;
pending?: boolean;
disabled?: boolean;
size?: "sm" | "icon";
variant?: "outline" | "ghost";
className?: string;
/** For an icon-only control, where `label` renders as an icon. */
ariaLabel?: string;
icon?: ReactNode;
/** `data-slot` for tests and the design guards. */
slot?: string;
}) {
const { t } = useTranslations();
return (
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
type="button"
variant={variant}
size={size}
className={className}
aria-label={ariaLabel}
data-slot={slot}
disabled={disabled || pending}
>
{pending ? (
<Loader2
className="size-4 animate-spin motion-reduce:animate-none"
aria-hidden
/>
) : (
icon
)}
{label}
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{title}</AlertDialogTitle>
<AlertDialogDescription>{body}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t("common.cancel")}</AlertDialogCancel>
<AlertDialogAction
data-slot={slot ? `${slot}-confirm` : undefined}
disabled={pending}
aria-busy={pending || undefined}
onClick={onConfirm}
>
{pending && (
<Loader2 className="mr-1 size-3.5 animate-spin motion-reduce:animate-none" />
)}
{confirmLabel}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
@@ -27,6 +27,7 @@ import { DateField } from "@/components/ui/date-field";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useAuth } from "@/hooks/use-auth";
import { ConfirmButton } from "@/components/settings/confirm-button";
import { useTranslations } from "@/lib/i18n/context";
import { apiDelete, apiGet, apiPost, apiPut } from "@/lib/api/api-fetch";
import { queryKeys } from "@/lib/query-keys";
@@ -325,19 +326,26 @@ export function EnvironmentSection() {
<span className="text-sm">
{tr.label} · {tr.startDate} – {tr.endDate}
</span>
<Button
type="button"
<ConfirmButton
slot="travel-remove"
size="icon"
variant="ghost"
className="min-h-11 min-w-11 sm:min-h-9 sm:min-w-9"
aria-label={t(
ariaLabel={t("settings.sections.environment.travel.remove")}
label=""
icon={<Trash2 className="size-4" aria-hidden />}
pending={removeTravel.isPending}
title={t(
"settings.sections.environment.travel.removeConfirmTitle",
)}
body={t(
"settings.sections.environment.travel.removeConfirmBody",
)}
confirmLabel={t(
"settings.sections.environment.travel.remove",
)}
disabled={removeTravel.isPending}
onClick={() => removeTravel.mutate(tr.id)}
>
<Trash2 className="size-4" />
</Button>
onConfirm={() => removeTravel.mutate(tr.id)}
/>
</li>
))}
</ul>
@@ -13,8 +13,8 @@ import { useId, useState } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { ChevronDown, Loader2, MonitorSmartphone } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SettingsCard } from "@/components/settings/settings-card";
import { ConfirmButton } from "@/components/settings/confirm-button";
import { EmptyState } from "@/components/ui/empty-state";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
@@ -168,16 +168,19 @@ export function SecuritySessionsCard({
)}
</div>
{!s.isCurrent && (
<Button
type="button"
variant="outline"
<ConfirmButton
slot="revoke-session"
size="sm"
className="min-h-9 shrink-0"
onClick={() => revokeOne.mutate(s.id)}
disabled={revokeOne.isPending}
>
{t("settings.security.revokeSession")}
</Button>
label={t("settings.security.revokeSession")}
title={t("settings.security.revokeSessionConfirmTitle")}
body={t("settings.security.revokeSessionConfirmBody", {
device: s.device,
})}
confirmLabel={t("settings.security.revokeSession")}
onConfirm={() => revokeOne.mutate(s.id)}
pending={revokeOne.isPending}
/>
)}
</li>
))}
@@ -186,18 +189,16 @@ export function SecuritySessionsCard({
{hasOthers && (
<div className="flex justify-end">
<Button
type="button"
variant="outline"
<ConfirmButton
slot="sign-out-everywhere"
className="min-h-11 sm:min-h-9"
onClick={() => revokeOthers.mutate()}
disabled={revokeOthers.isPending}
>
{revokeOthers.isPending && (
<Loader2 className="h-4 w-4 animate-spin motion-reduce:animate-none" />
)}
{t("settings.security.signOutEverywhere")}
</Button>
label={t("settings.security.signOutEverywhere")}
title={t("settings.security.signOutEverywhereConfirmTitle")}
body={t("settings.security.signOutEverywhereConfirmBody")}
confirmLabel={t("settings.security.signOutEverywhere")}
onConfirm={() => revokeOthers.mutate()}
pending={revokeOthers.isPending}
/>
</div>
)}
@@ -12,8 +12,8 @@ import { useState } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { Loader2, ShieldCheck } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SettingsCard } from "@/components/settings/settings-card";
import { ConfirmButton } from "@/components/settings/confirm-button";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
@@ -130,16 +130,19 @@ export function TrustedDevicesCard({
})}
</p>
</div>
<Button
type="button"
variant="outline"
<ConfirmButton
slot="revoke-trusted-device"
size="sm"
className="min-h-9 shrink-0"
onClick={() => revokeOne.mutate(d.id)}
disabled={revokeOne.isPending}
>
{t("settings.security.trustedDevices.revoke")}
</Button>
label={t("settings.security.trustedDevices.revoke")}
title={t(
"settings.security.trustedDevices.revokeConfirmTitle",
)}
body={t("settings.security.trustedDevices.revokeConfirmBody")}
confirmLabel={t("settings.security.trustedDevices.revoke")}
onConfirm={() => revokeOne.mutate(d.id)}
pending={revokeOne.isPending}
/>
</li>
))}
</ul>
@@ -147,18 +150,18 @@ export function TrustedDevicesCard({
{devices.length > 0 && (
<div className="flex justify-end">
<Button
type="button"
variant="outline"
<ConfirmButton
slot="revoke-all-trusted-devices"
className="min-h-11 sm:min-h-9"
onClick={() => revokeAll.mutate()}
disabled={revokeAll.isPending}
>
{revokeAll.isPending && (
<Loader2 className="h-4 w-4 animate-spin motion-reduce:animate-none" />
label={t("settings.security.trustedDevices.revokeAll")}
title={t(
"settings.security.trustedDevices.revokeAllConfirmTitle",
)}
{t("settings.security.trustedDevices.revokeAll")}
</Button>
body={t("settings.security.trustedDevices.revokeAllConfirmBody")}
confirmLabel={t("settings.security.trustedDevices.revokeAll")}
onConfirm={() => revokeAll.mutate()}
pending={revokeAll.isPending}
/>
</div>
)}