Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor(web): Clean up `DeleteAutomationButton` (#16523)

Ben Bacheme6bf679f07e5
diff --git a/web/src/features/automations/components/AutomationDetails.tsx b/web/src/features/automations/components/AutomationDetails.tsx
index ff68976216..52ac67c208 100644
--- a/web/src/features/automations/components/AutomationDetails.tsx
+++ b/web/src/features/automations/components/AutomationDetails.tsx
@@ -19,7 +19,7 @@ import {
} from "@/src/components/ui/tabs-bar";
import Header from "@/src/components/layouts/header";
import { SettingsTableCard } from "@/src/components/layouts/settings-table-card";
-import { DeleteAutomationButton } from "./DeleteAutomationButton";
+import { DeleteAutomationDialogController } from "./DeleteAutomationDialogController";
import { useQueryParam, StringParam, withDefault } from "use-query-params";
interface AutomationDetailsProps {
@@ -125,12 +125,23 @@ export const AutomationDetails: React.FC<AutomationDetailsProps> = ({
<Edit className="mr-2 h-4 w-4" />
Edit
</Button>
- <DeleteAutomationButton
+ <DeleteAutomationDialogController
projectId={projectId}
automationId={automationId}
- variant="button"
onSuccess={onDelete}
- />
+ >
+ {({ disabled, openDialog }) => (
+ <Button
+ type="button"
+ variant="outline"
+ className="border-light-red flex items-center"
+ disabled={disabled !== undefined}
+ onClick={openDialog}
+ >
+ <span className="text-dark-red">Delete</span>
+ </Button>
+ )}
+ </DeleteAutomationDialogController>
</div>
}
/>
diff --git a/web/src/features/automations/components/DeleteAutomationButton.tsx b/web/src/features/automations/components/DeleteAutomationButton.tsx
deleted file mode 100644
index 384dd1110d..0000000000
--- a/web/src/features/automations/components/DeleteAutomationButton.tsx
+++ /dev/null
@@ -1,100 +0,0 @@
-/* eslint-disable @repo/no-abstracted-overlay-trigger */
-import { useState } from "react";
-import { Button } from "@/src/components/ui/button";
-import { Trash } from "lucide-react";
-import { api } from "@/src/utils/api";
-import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
-import {
- Popover,
- PopoverContent,
- PopoverTrigger,
-} from "@/src/components/ui/popover";
-import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
-
-interface DeleteAutomationButtonProps {
- projectId: string;
- automationId: string;
- onSuccess?: () => void;
- variant?: "icon" | "button"; // "icon" for list view, "button" for form view
-}
-
-export const DeleteAutomationButton: React.FC<DeleteAutomationButtonProps> = ({
- projectId,
- automationId,
- onSuccess,
- variant = "icon",
-}) => {
- const [isOpen, setIsOpen] = useState(false);
- const utils = api.useUtils();
- const hasAccess = useHasProjectAccess({
- projectId,
- scope: "automations:CUD",
- });
-
- const deleteAutomationMutation = api.automations.deleteAutomation.useMutation(
- {
- onSuccess: () => {
- showSuccessToast({
- title: "Automation deleted",
- description: "The automation has been deleted successfully.",
- });
-
- if (onSuccess) {
- onSuccess();
- }
-
- utils.automations.invalidate();
- },
- },
- );
-
- return (
- <Popover open={isOpen} onOpenChange={() => setIsOpen(!isOpen)}>
- <PopoverTrigger asChild>
- {variant === "icon" ? (
- <Button
- variant="ghost"
- size="sm"
- className="h-8 w-8 p-0"
- disabled={!hasAccess}
- >
- <Trash className="h-4 w-4" />
- <span className="sr-only">Delete</span>
- </Button>
- ) : (
- <Button
- type="button"
- variant="outline"
- className="border-light-red flex items-center"
- disabled={!hasAccess}
- >
- <span className="text-dark-red">Delete</span>
- </Button>
- )}
- </PopoverTrigger>
- <PopoverContent>
- <h2 className="mb-3 font-bold">Please confirm</h2>
- <p className="mb-3 text-sm">
- This action permanently deletes this automation and execution history.
- This cannot be undone.
- </p>
- <div className="flex justify-end space-x-4">
- <Button
- type="button"
- variant="destructive"
- loading={deleteAutomationMutation.isPending}
- onClick={() => {
- deleteAutomationMutation.mutateAsync({
- projectId,
- automationId,
- });
- setIsOpen(false);
- }}
- >
- Delete Automation
- </Button>
- </div>
- </PopoverContent>
- </Popover>
- );
-};
diff --git a/web/src/features/automations/components/DeleteAutomationDialog.stories.tsx b/web/src/features/automations/components/DeleteAutomationDialog.stories.tsx
new file mode 100644
index 0000000000..649a6f0617
--- /dev/null
+++ b/web/src/features/automations/components/DeleteAutomationDialog.stories.tsx
@@ -0,0 +1,47 @@
+import { expect, fn, userEvent, within } from "storybook/test";
+
+import preview from "../../../../.storybook/preview";
+import { DeleteAutomationDialog } from "./DeleteAutomationDialog";
+
+const meta = preview.meta({
+ component: DeleteAutomationDialog,
+});
+
+export default meta;
+
+export const Default = meta.story({
+ args: {
+ open: true,
+ onOpenChange: fn(),
+ isPending: false,
+ onConfirm: fn(),
+ },
+});
+
+export const Loading = meta.story({
+ args: {
+ open: true,
+ onOpenChange: fn(),
+ isPending: true,
+ onConfirm: fn(),
+ },
+});
+
+export const ConfirmsDeletion = meta.story({
+ name: "(Test) Confirms deletion",
+ args: {
+ open: true,
+ onOpenChange: fn(),
+ isPending: false,
+ onConfirm: fn(),
+ },
+ play: async ({ args, canvasElement }) => {
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(
+ body.getByRole("button", { name: "Delete Automation" }),
+ );
+
+ await expect(args.onConfirm).toHaveBeenCalledOnce();
+ },
+});
diff --git a/web/src/features/automations/components/DeleteAutomationDialog.tsx b/web/src/features/automations/components/DeleteAutomationDialog.tsx
new file mode 100644
index 0000000000..a4169136aa
--- /dev/null
+++ b/web/src/features/automations/components/DeleteAutomationDialog.tsx
@@ -0,0 +1,27 @@
+import { ConfirmDialog } from "@/src/components/ui/confirm-dialog";
+
+export interface DeleteAutomationDialogProps {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ isPending: boolean;
+ onConfirm: () => void;
+}
+
+export function DeleteAutomationDialog({
+ open,
+ onOpenChange,
+ isPending,
+ onConfirm,
+}: DeleteAutomationDialogProps) {
+ return (
+ <ConfirmDialog
+ open={open}
+ onOpenChange={onOpenChange}
+ title="Please confirm"
+ description="This action permanently deletes this automation and execution history. This cannot be undone."
+ confirmLabel="Delete Automation"
+ loading={isPending}
+ onConfirm={onConfirm}
+ />
+ );
+}
diff --git a/web/src/features/automations/components/DeleteAutomationDialogController.tsx b/web/src/features/automations/components/DeleteAutomationDialogController.tsx
new file mode 100644
index 0000000000..5ac50bae19
--- /dev/null
+++ b/web/src/features/automations/components/DeleteAutomationDialogController.tsx
@@ -0,0 +1,80 @@
+import type * as React from "react";
+import { useState } from "react";
+
+import { api } from "@/src/utils/api";
+import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { DeleteAutomationDialog } from "./DeleteAutomationDialog";
+
+type DeleteAutomationDialogControllerProps = {
+ projectId: string;
+ automationId: string;
+ onSuccess?: () => void;
+ children: (control: {
+ disabled: { reason: string } | undefined;
+ openDialog: () => void;
+ }) => React.ReactNode;
+};
+
+export const DeleteAutomationDialogController = ({
+ projectId,
+ automationId,
+ onSuccess,
+ children,
+}: DeleteAutomationDialogControllerProps) => {
+ const [open, setOpen] = useState(false);
+ const utils = api.useUtils();
+ const hasAccess = useHasProjectAccess({
+ projectId,
+ scope: "automations:CUD",
+ });
+
+ const deleteAutomationMutation = api.automations.deleteAutomation.useMutation(
+ {
+ onSuccess: () => {
+ showSuccessToast({
+ title: "Automation deleted",
+ description: "The automation has been deleted successfully.",
+ });
+
+ onSuccess?.();
+
+ utils.automations.invalidate();
+ },
+ },
+ );
+
+ const disabled = hasAccess
+ ? undefined
+ : { reason: "You don't have permission to delete this automation." };
+
+ const openDialog = () => {
+ if (!hasAccess) return;
+
+ setOpen(true);
+ };
+
+ const handleDelete = async () => {
+ try {
+ await deleteAutomationMutation.mutateAsync({
+ projectId,
+ automationId,
+ });
+ setOpen(false);
+ } catch {
+ // The tRPC error handler owns mutation failures; keep the dialog open.
+ }
+ };
+
+ return (
+ <>
+ {children({ disabled, openDialog })}
+ <DeleteAutomationDialog
+ open={hasAccess && open}
+ onOpenChange={setOpen}
+ isPending={deleteAutomationMutation.isPending}
+ onConfirm={handleDelete}
+ />
+ </>
+ );
+};
diff --git a/web/src/features/automations/components/automationForm.tsx b/web/src/features/automations/components/automationForm.tsx
index ade3f18eca..4174d05e7a 100644
--- a/web/src/features/automations/components/automationForm.tsx
+++ b/web/src/features/automations/components/automationForm.tsx
@@ -43,7 +43,7 @@ import {
webhookActionFilterOptions,
} from "@langfuse/shared";
import { InlineFilterBuilder } from "@/src/features/filters/components/filter-builder";
-import { DeleteAutomationButton } from "./DeleteAutomationButton";
+import { DeleteAutomationDialogController } from "./DeleteAutomationDialogController";
import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
@@ -813,15 +813,25 @@ export const AutomationForm = ({
automation?.trigger.id &&
automation?.action.id && (
<div>
- <DeleteAutomationButton
+ <DeleteAutomationDialogController
projectId={projectId}
automationId={automation.id}
- variant="button"
onSuccess={() => {
- utils.automations.invalidate();
router.push(`/project/${projectId}/settings/automations`);
}}
- />
+ >
+ {({ disabled, openDialog }) => (
+ <Button
+ type="button"
+ variant="outline"
+ className="border-light-red flex items-center"
+ disabled={disabled !== undefined}
+ onClick={openDialog}
+ >
+ <span className="text-dark-red">Delete</span>
+ </Button>
+ )}
+ </DeleteAutomationDialogController>
</div>
)}
<div className="grow"></div>