Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(automations): make the draft delete confirmation a real dialog (#18394)

Engel Nyst7575a859fb09
diff --git a/__tests__/routes/automations-list.test.tsx b/__tests__/routes/automations-list.test.tsx
index 732c4500cb..2a2b53305a 100644
--- a/__tests__/routes/automations-list.test.tsx
+++ b/__tests__/routes/automations-list.test.tsx
@@ -452,6 +452,42 @@ describe("AutomationsList β€” draft sections", () => {
);
});
+ it("confirms a saved draft deletion in a named modal dialog that Escape cancels", async () => {
+ // Arrange β€” open the confirmation from the saved draft's Delete button.
+ const user = userEvent.setup();
+ vi.mocked(AutomationService.listServerDrafts).mockResolvedValue(
+ draftListResponse,
+ );
+ vi.mocked(AutomationService.deleteServerDraft).mockReset();
+ renderList();
+ await user.click(
+ await screen.findByTestId("automation-setup-draft-delete-draft-1"),
+ );
+
+ // Assert β€” a modal dialog that keyboard users land in, on Cancel.
+ const dialog = await screen.findByRole("dialog", {
+ name: I18nKey.AUTOMATION_SETUP$DELETE_DRAFT_TITLE,
+ });
+ expect(dialog).toHaveAttribute("aria-modal", "true");
+ expect(document.activeElement).toBe(
+ within(dialog).getByRole("button", { name: I18nKey.AUTOMATIONS$CANCEL }),
+ );
+
+ // Act β€” dismiss with the keyboard.
+ await user.keyboard("{Escape}");
+
+ // Assert β€” the dialog is gone, focus is back on the opener, and the
+ // draft was kept.
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ expect(
+ screen.getByTestId("automation-setup-draft-delete-draft-1"),
+ ).toHaveFocus();
+ expect(AutomationService.deleteServerDraft).not.toHaveBeenCalled();
+ expect(
+ screen.getByTestId("automation-setup-draft-draft-1"),
+ ).toBeInTheDocument();
+ });
+
it("treats a Cloud HttpError 404 from the drafts API as an empty drafts list", async () => {
// Cloud draft calls run through callCloudProxy, which throws the shared
// client's HttpError with `status` on the error itself (not under `response`).
diff --git a/src/components/features/automations/delete-confirmation-modal.tsx b/src/components/features/automations/delete-confirmation-modal.tsx
index ca72649c0a..bbb790399b 100644
--- a/src/components/features/automations/delete-confirmation-modal.tsx
+++ b/src/components/features/automations/delete-confirmation-modal.tsx
@@ -15,6 +15,13 @@ interface DeleteConfirmationModalProps {
isOpen: boolean;
onConfirm: () => void;
onCancel: () => void;
+ /** Overrides the default automation copy, e.g. for a saved draft. */
+ title?: string;
+ message?: string;
+ confirmLabel?: string;
+ confirmTestId?: string;
+ /** Disables Delete while the deletion is in flight. */
+ isConfirming?: boolean;
}
export function DeleteConfirmationModal({
@@ -22,19 +29,30 @@ export function DeleteConfirmationModal({
isOpen,
onConfirm,
onCancel,
+ title: titleOverride,
+ message,
+ confirmLabel,
+ confirmTestId,
+ isConfirming = false,
}: DeleteConfirmationModalProps) {
const { t } = useTranslation("openhands");
const cancelButtonRef = React.useRef<HTMLButtonElement>(null);
// Move keyboard focus into the dialog when it opens, onto the
// non-destructive choice, so Enter or Space cannot delete by accident.
+ // On close, hand focus back to the control that opened the dialog.
React.useEffect(() => {
- if (isOpen) cancelButtonRef.current?.focus();
+ if (!isOpen) return undefined;
+ const opener = document.activeElement;
+ cancelButtonRef.current?.focus();
+ return () => {
+ if (opener instanceof HTMLElement && opener.isConnected) opener.focus();
+ };
}, [isOpen]);
if (!isOpen) return null;
- const title = t(I18nKey.AUTOMATIONS$DELETE_CONFIRM_TITLE);
+ const title = titleOverride ?? t(I18nKey.AUTOMATIONS$DELETE_CONFIRM_TITLE);
// ModalBackdrop exposes the named `role="dialog"` with `aria-modal` and
// closes on Escape or a backdrop click.
@@ -58,9 +76,10 @@ export function DeleteConfirmationModal({
<h2 className={modalTitleLgMediumClassName}>{title}</h2>
<p className="mt-2 text-sm text-muted">
- {t(I18nKey.AUTOMATIONS$DELETE_CONFIRM_MESSAGE, {
- name: automationName,
- })}
+ {message ??
+ t(I18nKey.AUTOMATIONS$DELETE_CONFIRM_MESSAGE, {
+ name: automationName,
+ })}
</p>
<div className="mt-6 flex justify-end gap-3">
@@ -74,10 +93,12 @@ export function DeleteConfirmationModal({
</button>
<button
type="button"
+ data-testid={confirmTestId}
onClick={onConfirm}
- className="rounded-lg bg-danger px-4 py-2 text-sm text-white hover:bg-danger/80"
+ disabled={isConfirming}
+ className="rounded-lg bg-danger px-4 py-2 text-sm text-white hover:bg-danger/80 disabled:cursor-not-allowed disabled:opacity-60"
>
- {t(I18nKey.AUTOMATIONS$DELETE)}
+ {confirmLabel ?? t(I18nKey.AUTOMATIONS$DELETE)}
</button>
</div>
</div>
diff --git a/src/routes/automations-list.tsx b/src/routes/automations-list.tsx
index 4f387533d8..ddc8e0cd65 100644
--- a/src/routes/automations-list.tsx
+++ b/src/routes/automations-list.tsx
@@ -588,6 +588,9 @@ export default function AutomationsList() {
});
};
+ const deleteDraftName =
+ deleteDraftTarget?.name ?? t(I18nKey.AUTOMATIONS$UNTITLED_DRAFT);
+
const handleViewModeChange = useCallback((view: AutomationViewMode) => {
setViewMode(view);
writeStoredAutomationViewMode(view);
@@ -896,46 +899,19 @@ export default function AutomationsList() {
onCancel={() => setDeleteTarget(null)}
/>
- {deleteDraftTarget ? (
- <div className="fixed inset-0 z-50 flex items-center justify-center">
- <button
- type="button"
- className="absolute inset-0 bg-black/60"
- aria-label={t(I18nKey.BUTTON$CLOSE)}
- onClick={() => setDeleteDraftTarget(null)}
- />
- <div className="relative w-full max-w-sm rounded-xl border border-border bg-surface p-6">
- <h2 className="text-lg font-semibold text-content">
- {t(I18nKey.AUTOMATION_SETUP$DELETE_DRAFT_TITLE)}
- </h2>
- <p className="mt-2 text-sm text-muted">
- {t(I18nKey.AUTOMATION_SETUP$DELETE_DRAFT_MESSAGE, {
- name:
- deleteDraftTarget.name ??
- t(I18nKey.AUTOMATIONS$UNTITLED_DRAFT),
- })}
- </p>
- <div className="mt-6 flex justify-end gap-3">
- <button
- type="button"
- onClick={() => setDeleteDraftTarget(null)}
- className="rounded-lg border border-border px-4 py-2 text-sm text-contrast hover:bg-surface-raised"
- >
- {t(I18nKey.AUTOMATIONS$CANCEL)}
- </button>
- <button
- type="button"
- data-testid="automation-setup-draft-delete-confirm"
- onClick={handleDeleteDraftConfirm}
- disabled={deleteDraftMutation.isPending}
- className="rounded-lg bg-danger px-4 py-2 text-sm text-white hover:bg-danger/80 disabled:cursor-not-allowed disabled:opacity-60"
- >
- {t(I18nKey.AUTOMATION_SETUP$DELETE_DRAFT)}
- </button>
- </div>
- </div>
- </div>
- ) : null}
+ <DeleteConfirmationModal
+ automationName={deleteDraftName}
+ isOpen={deleteDraftTarget !== null}
+ onConfirm={handleDeleteDraftConfirm}
+ onCancel={() => setDeleteDraftTarget(null)}
+ title={t(I18nKey.AUTOMATION_SETUP$DELETE_DRAFT_TITLE)}
+ message={t(I18nKey.AUTOMATION_SETUP$DELETE_DRAFT_MESSAGE, {
+ name: deleteDraftName,
+ })}
+ confirmLabel={t(I18nKey.AUTOMATION_SETUP$DELETE_DRAFT)}
+ confirmTestId="automation-setup-draft-delete-confirm"
+ isConfirming={deleteDraftMutation.isPending}
+ />
<ImportAutomationModal
isOpen={isImportOpen}