2025-05-16 15:54:17 +02:00
|
|
|
"use client";
|
2024-12-16 20:01:30 +01:00
|
|
|
|
2025-08-27 18:01:28 +02:00
|
|
|
import {Trash2} from "lucide-react";
|
2026-05-24 17:09:12 +02:00
|
|
|
import {ButtonWithConfirm} from "@/components/common/button-with-confirm";
|
2025-08-27 18:01:28 +02:00
|
|
|
import {useMutation} from "@tanstack/react-query";
|
|
|
|
|
import {useRouter} from "next/navigation";
|
|
|
|
|
import {toast} from "sonner";
|
2026-05-24 17:09:12 +02:00
|
|
|
import {deleteAgentAction} from "@/features/agents/agent-delete.action";
|
2026-01-11 12:37:57 +01:00
|
|
|
import {useIsMobile} from "@/hooks/use-mobile";
|
2024-12-01 11:11:27 +01:00
|
|
|
|
2025-07-27 21:26:31 +02:00
|
|
|
export type ButtonDeleteAgentProps = {
|
2026-04-10 18:37:35 +02:00
|
|
|
text?: string,
|
|
|
|
|
agentId: string,
|
|
|
|
|
organizationId?: string
|
|
|
|
|
organizationIds?: string[]
|
2025-05-16 15:54:17 +02:00
|
|
|
};
|
2024-12-01 11:11:27 +01:00
|
|
|
|
2025-07-27 21:26:31 +02:00
|
|
|
export const ButtonDeleteAgent = (props: ButtonDeleteAgentProps) => {
|
2025-05-16 15:54:17 +02:00
|
|
|
const router = useRouter();
|
2026-01-11 12:37:57 +01:00
|
|
|
const isMobile = useIsMobile();
|
2025-07-27 21:26:31 +02:00
|
|
|
|
2024-12-17 19:42:02 +01:00
|
|
|
const mutation = useMutation({
|
2026-04-10 18:37:35 +02:00
|
|
|
mutationFn: () => deleteAgentAction({agentId: props.agentId, organizationId: props.organizationId, organizationIds: props.organizationIds}),
|
2025-01-03 15:55:16 +01:00
|
|
|
onSuccess: async (result: any) => {
|
2025-05-16 15:54:17 +02:00
|
|
|
if (result.data?.success) {
|
2024-12-17 19:42:02 +01:00
|
|
|
toast.success(result.data.actionSuccess.message);
|
2026-04-10 18:37:35 +02:00
|
|
|
router.push(props.organizationId ? "/dashboard/settings?tab=agents" : "/dashboard/agents");
|
2025-05-16 15:54:17 +02:00
|
|
|
} else {
|
2025-01-03 15:55:16 +01:00
|
|
|
toast.error(result.data.actionError.message || "Unknown error occurred.");
|
2024-12-17 19:42:02 +01:00
|
|
|
}
|
|
|
|
|
},
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
2024-12-01 11:11:27 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ButtonWithConfirm
|
2025-08-27 18:01:28 +02:00
|
|
|
title={props.text ? props.text : ""}
|
|
|
|
|
description="Are you sure you want to remove this agent? This action cannot be undone."
|
|
|
|
|
button={{
|
|
|
|
|
main: {
|
2026-04-10 18:37:35 +02:00
|
|
|
text: props.text ? !isMobile ? props.text : "" : "",
|
2025-08-27 18:01:28 +02:00
|
|
|
variant: "outline",
|
|
|
|
|
icon: <Trash2 color="red"/>,
|
|
|
|
|
},
|
|
|
|
|
confirm: {
|
|
|
|
|
className: "w-full",
|
|
|
|
|
text: "Delete",
|
|
|
|
|
icon: <Trash2/>,
|
|
|
|
|
variant: "destructive",
|
|
|
|
|
onClick: () => {
|
|
|
|
|
mutation.mutate();
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
cancel: {
|
|
|
|
|
className: "w-full",
|
|
|
|
|
text: "Cancel",
|
|
|
|
|
icon: <Trash2/>,
|
|
|
|
|
variant: "outline",
|
|
|
|
|
},
|
2024-12-01 11:11:27 +01:00
|
|
|
}}
|
2024-12-17 19:42:02 +01:00
|
|
|
isPending={mutation.isPending}
|
2024-12-01 11:11:27 +01:00
|
|
|
/>
|
2025-05-16 15:54:17 +02:00
|
|
|
);
|
|
|
|
|
};
|