SheetForm dirty-state confirm + wire isDirty into all form consumers (R4.5)

SheetForm gains an isDirty prop. When true, any close attempt (Cancel
button, header X, Radix overlay click, Escape) opens a 'Discard changes?'
ConfirmDialog instead of discarding unsaved edits. Radix dismiss callbacks
(onEscapeKeyDown, onPointerDownOutside) are intercepted when dirty so the
guard applies uniformly.

All four form consumers now compute and pass isDirty:
- ServicePage: name/enabled/config differ from the persisted instance.
- Settings machine editor: field-by-field draft vs editingMachine
  (create mode is always dirty; secret write-only fields excluded).
- Message compose: subject non-empty, body differs from default, or
  attachments present.
- WidgetConfigDialog: draft !== null (only draft mode is guarded; list
  mode has nothing to discard).

Tests: 3 new SheetForm dirty-guard cases (prompt on cancel, abort discard,
clean close when not dirty) + one focused dirty-guard test per consumer.
122 tests pass; lint/build green.

Refs openspec/changes/mobile-responsive-parity/verify-report.md residual
risk #1.
This commit is contained in:
Developer
2026-06-26 15:31:30 +00:00
parent 32516f6e3b
commit 09b9c45665
9 changed files with 248 additions and 13 deletions
@@ -315,7 +315,7 @@ export function WidgetConfigDialog({ open, onClose }: Props) {
<div className="flex items-center gap-2">
<Switch
id="widget-enabled"
className="mobile-touch-target"
className="mobile-touch-target"
checked={draft.enabled}
onCheckedChange={(checked) =>
setDraft({ ...draft, enabled: checked })
@@ -394,8 +394,8 @@ export function WidgetConfigDialog({ open, onClose }: Props) {
<ChevronDown className="h-4 w-4" />
</Button>
<Switch
className="mobile-touch-target"
checked={instance.enabled}
className="mobile-touch-target"
checked={instance.enabled}
onCheckedChange={() => toggleEnabled(instance)}
aria-label={`Toggle ${instance.title}`}
/>
@@ -479,6 +479,7 @@ export function WidgetConfigDialog({ open, onClose }: Props) {
onCancel={draft ? reset : () => handleClose(false)}
saveLabel={draft ? "Save widget" : "Done"}
isPending={draft ? saveWidget.isPending : false}
isDirty={draft !== null}
>
<div className="flex flex-col gap-4">{draftBody}</div>
</SheetForm>
@@ -50,4 +50,18 @@ describe("WidgetConfigDialog (mobile SheetForm — slice 8)", () => {
// List mode footer: "Done" button closes.
expect(screen.getByRole("button", { name: "Done" })).toBeInTheDocument();
});
it("prompts before discarding a widget draft (R4.5)", async () => {
const { userEvent } = await import("@testing-library/user-event");
render(<WidgetConfigDialog open={true} onClose={() => {}} />);
// Enter draft mode by clicking an "Add widget" button.
await userEvent.click(screen.getByRole("button", { name: /Backups/i }));
// Now in draft mode — Cancel should prompt before resetting.
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(
screen.getByRole("heading", { name: "Discard changes?" }),
).toBeInTheDocument();
});
});
@@ -93,4 +93,76 @@ describe("SheetForm", () => {
await userEvent.click(screen.getByRole("button", { name: "Close" }));
expect(onCancel).toHaveBeenCalledTimes(1);
});
describe("dirty-state confirm (R4.5)", () => {
it("prompts before discarding via Cancel when isDirty", async () => {
const onCancel = vi.fn();
render(
<SheetForm
open
onOpenChange={() => {}}
title="Edit"
onSave={() => {}}
onCancel={onCancel}
isDirty
>
<div />
</SheetForm>,
);
// Cancel does not immediately close; a confirm opens.
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(onCancel).not.toHaveBeenCalled();
expect(
screen.getByRole("heading", { name: "Discard changes?" }),
).toBeInTheDocument();
// Confirm discard -> actually closes.
await userEvent.click(screen.getByRole("button", { name: "Discard" }));
expect(onCancel).toHaveBeenCalledTimes(1);
});
it("closing the confirm without discarding keeps the form open", async () => {
const onCancel = vi.fn();
render(
<SheetForm
open
onOpenChange={() => {}}
title="Edit"
onSave={() => {}}
onCancel={onCancel}
isDirty
>
<div />
</SheetForm>,
);
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
// Two Cancel buttons now exist: the SheetForm footer and the confirm dialog.
const cancelButtons = screen.getAllByRole("button", { name: "Cancel" });
await userEvent.click(cancelButtons[cancelButtons.length - 1]);
expect(onCancel).not.toHaveBeenCalled();
});
it("closes immediately when not dirty", async () => {
const onCancel = vi.fn();
render(
<SheetForm
open
onOpenChange={() => {}}
title="Edit"
onSave={() => {}}
onCancel={onCancel}
>
<div />
</SheetForm>,
);
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(onCancel).toHaveBeenCalledTimes(1);
expect(
screen.queryByRole("heading", { name: "Discard changes?" }),
).not.toBeInTheDocument();
});
});
});
+60 -3
View File
@@ -4,6 +4,7 @@ import { Loader2, XIcon } from "lucide-react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Sheet, SheetContent, SheetTitle } from "@/components/ui/sheet";
import { ConfirmDialog } from "@/components/ConfirmDialog";
export interface SheetFormProps {
open: boolean;
@@ -17,6 +18,12 @@ export interface SheetFormProps {
saveLabel?: string;
/** Disable the Save button (e.g. when required fields are empty). */
saveDisabled?: boolean;
/**
* When true, any close attempt (Cancel button, header X, overlay click,
* Escape) prompts a discard-confirmation instead of immediately closing.
* Spec R4.5.
*/
isDirty?: boolean;
children: React.ReactNode;
/** Optional className applied to the scrolling body. */
bodyClassName?: string;
@@ -43,15 +50,53 @@ export function SheetForm({
isPending = false,
saveDisabled = false,
saveLabel = "Save",
isDirty = false,
children,
bodyClassName,
}: SheetFormProps) {
const [confirmDiscardOpen, setConfirmDiscardOpen] = React.useState(false);
// Route every close path (Cancel, header X, Radix overlay/Escape) through one
// guard so the dirty-confirm is applied uniformly (spec R4.5).
const attemptClose = React.useCallback(() => {
if (isDirty) {
setConfirmDiscardOpen(true);
} else {
onCancel();
}
}, [isDirty, onCancel]);
const handleOpenChange = React.useCallback(
(next: boolean) => {
if (!next) {
attemptClose();
} else {
onOpenChange(next);
}
},
[attemptClose, onOpenChange],
);
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<Sheet open={open} onOpenChange={handleOpenChange}>
<SheetContent
side="bottom"
showCloseButton={false}
className="flex h-[100dvh] w-full flex-col gap-0 p-0 sm:max-w-full"
onEscapeKeyDown={(e) => {
// Prevent Radix's default Escape close so our guard runs instead.
if (isDirty) {
e.preventDefault();
attemptClose();
}
}}
onPointerDownOutside={(e) => {
// Prevent overlay-click close so our guard runs instead.
if (isDirty) {
e.preventDefault();
attemptClose();
}
}}
>
{/* Header — fixed at top */}
<div className="flex h-14 shrink-0 items-center justify-between border-b border-border px-4">
@@ -62,7 +107,7 @@ export function SheetForm({
variant="ghost"
size="icon-sm"
aria-label="Close"
onClick={onCancel}
onClick={attemptClose}
>
<XIcon />
</Button>
@@ -75,7 +120,7 @@ export function SheetForm({
{/* Footer — fixed at bottom */}
<div className="flex shrink-0 items-center justify-end gap-2 border-t border-border bg-muted/50 p-4">
<Button variant="outline" onClick={onCancel} disabled={isPending}>
<Button variant="outline" onClick={attemptClose} disabled={isPending}>
Cancel
</Button>
<Button onClick={onSave} disabled={isPending || saveDisabled}>
@@ -90,6 +135,18 @@ export function SheetForm({
</Button>
</div>
</SheetContent>
<ConfirmDialog
open={confirmDiscardOpen}
title="Discard changes?"
message="You have unsaved changes. Discard them and close?"
confirmLabel="Discard"
onCancel={() => setConfirmDiscardOpen(false)}
onConfirm={() => {
setConfirmDiscardOpen(false);
onCancel();
}}
/>
</Sheet>
);
}
+10 -3
View File
@@ -174,6 +174,12 @@ export function ServicePage() {
/>
);
// Dirty when any editable field diverges from the persisted instance (mobile SheetForm R4.5 guard).
const isDirty =
name !== instance.name ||
enabled !== instance.enabled ||
JSON.stringify(draftConfig) !== JSON.stringify(instance.config);
if (isMobile) {
return (
<div className="flex flex-col gap-4">
@@ -183,10 +189,11 @@ export function ServicePage() {
title={name || instance.name}
onSave={save}
onCancel={() => {
setSheetOpen(false);
navigate("/services");
}}
setSheetOpen(false);
navigate("/services");
}}
isPending={saveService.isPending}
isDirty={isDirty}
>
<div className="flex flex-col gap-6">
<Field label="Name" htmlFor="service-name">
+25
View File
@@ -128,6 +128,30 @@ function emptyMachine(
};
}
/**
* Dirty check for the machine editor SheetForm guard (spec R4.5).
* Pragmatic field-by-field comparison of the user-editable fields. In create
* mode (editingMachine is null) the form is always dirty.
*/
function isMachineDraftDirty(
draft: MonitoringMachineInput,
editingMachine: MonitoringMachine | null,
): boolean {
if (!editingMachine) return true;
return (
draft.name !== editingMachine.name ||
draft.host !== editingMachine.host ||
draft.mode !== editingMachine.mode ||
draft.port !== editingMachine.port ||
draft.username !== editingMachine.username ||
draft.ssh_key_id !== editingMachine.ssh_key_id ||
draft.enabled !== editingMachine.enabled ||
draft.notes !== editingMachine.notes ||
JSON.stringify([...draft.services].sort()) !==
JSON.stringify([...editingMachine.services].sort())
);
}
function MachineEditor({
title,
hint,
@@ -1159,6 +1183,7 @@ export function Settings() {
(machineDraft.mode === "ssh" && !machineDraft.host.trim())
}
saveLabel={machineDraft.id ? "Save machine" : "Create machine"}
isDirty={isMachineDraftDirty(machineDraft, editingMachine)}
>
<MachineEditor
key={`${machineDraft.id ?? machineDraft.mode}-${machineDraft.mode}`}
+9 -4
View File
@@ -891,7 +891,7 @@ export function UsersPage() {
<div className="flex flex-wrap gap-1">
<Tooltip>
<TooltipTrigger asChild>
<UiButton
<UiButton
variant="ghost"
size="icon"
className="mobile-touch-target"
@@ -905,7 +905,7 @@ export function UsersPage() {
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<UiButton
<UiButton
variant="ghost"
size="icon"
className="mobile-touch-target"
@@ -919,7 +919,7 @@ export function UsersPage() {
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<UiButton
<UiButton
variant="ghost"
size="icon"
className="mobile-touch-target"
@@ -933,7 +933,7 @@ export function UsersPage() {
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<UiButton
<UiButton
variant="ghost"
size="icon"
className="mobile-touch-target"
@@ -1022,6 +1022,11 @@ export function UsersPage() {
isPending={sendUserMessage.isPending}
saveDisabled={!selectedDeliverableRows.length || !subject.trim()}
saveLabel="Send message"
isDirty={
subject.trim() !== "" ||
htmlBody.trim() !== DEFAULT_HTML_BODY.trim() ||
attachments.length > 0
}
>
<div className="flex flex-col gap-4">{composeBody}</div>
</SheetForm>
@@ -187,4 +187,27 @@ describe("Settings (mobile SheetForm — slice 7)", () => {
// (The page content itself is still rendered; only the sheet unmounts.)
expect(screen.queryByText("Edit machine")).not.toBeInTheDocument();
});
it("prompts before discarding unsaved machine edits (R4.5)", async () => {
machines = [localMachine()];
render(<Settings />);
const detailEdit = screen
.getAllByRole("button", { name: "Edit" })
.find((button) => button.textContent === "Edit") as HTMLButtonElement;
await userEvent.click(detailEdit);
// Edit the name to make the form dirty.
const nameInput = screen.getByLabelText("Name");
await userEvent.clear(nameInput);
await userEvent.type(nameInput, "Dirty name");
// Cancel should NOT immediately close — the discard confirm appears.
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(
screen.getByRole("heading", { name: "Discard changes?" }),
).toBeInTheDocument();
// The editor is still open.
expect(screen.getByText("Edit machine")).toBeInTheDocument();
});
});
@@ -373,4 +373,35 @@ describe("UsersPage (mobile card layout — slice 5)", () => {
).toBeInTheDocument();
expect(screen.getByLabelText("Subject")).toBeInTheDocument();
});
it("prompts before discarding unsaved compose edits (R4.5)", async () => {
users = [
userFixture({
jellyfin_id: "u1",
display_name: "Alice",
email: "alice@example.com",
}),
];
render(
<TooltipProvider>
<UsersPage />
</TooltipProvider>,
);
await userEvent.click(
screen.getByRole("checkbox", { name: /Select Alice/i }),
);
await userEvent.click(
screen.getByRole("button", { name: "Message selected" }),
);
// Type a subject to make the compose form dirty.
await userEvent.type(screen.getByLabelText("Subject"), "Urgent update");
// Cancel should NOT immediately close — the discard confirm appears.
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(
screen.getByRole("heading", { name: "Discard changes?" }),
).toBeInTheDocument();
});
});