From e0a0ddf6ba8efe3969f623ff1a99ddfc0e40a72d Mon Sep 17 00:00:00 2001 From: Vivek R <123vivekr@gmail.com> Date: Tue, 24 Jun 2025 01:10:26 +0530 Subject: [PATCH] fix: replace native confirm dialog with React dialog for agent deletion - Replace synchronous confirm() with proper React Dialog component - Add state management for delete confirmation dialog - Prevent immediate deletion before user confirmation - Add loading states and proper error handling - Improve UX with clear confirmation message and responsive design --- src/components/CCAgents.tsx | 89 +++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/src/components/CCAgents.tsx b/src/components/CCAgents.tsx index 2d46c31..af45a2f 100644 --- a/src/components/CCAgents.tsx +++ b/src/components/CCAgents.tsx @@ -30,6 +30,14 @@ import { DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; import { api, type Agent, type AgentRunWithMetrics } from "@/lib/api"; import { save, open } from "@tauri-apps/plugin-dialog"; import { invoke } from "@tauri-apps/api/core"; @@ -87,6 +95,9 @@ export const CCAgents: React.FC = ({ onBack, className }) => { const [selectedAgent, setSelectedAgent] = useState(null); const [selectedRunId, setSelectedRunId] = useState(null); const [showGitHubBrowser, setShowGitHubBrowser] = useState(false); + const [showDeleteDialog, setShowDeleteDialog] = useState(false); + const [agentToDelete, setAgentToDelete] = useState(null); + const [isDeleting, setIsDeleting] = useState(false); const AGENTS_PER_PAGE = 9; // 3x3 grid @@ -122,20 +133,46 @@ export const CCAgents: React.FC = ({ onBack, className }) => { } }; - const handleDeleteAgent = async (id: number) => { - if (!confirm("Are you sure you want to delete this agent?")) return; + /** + * Initiates the delete agent process by showing the confirmation dialog + * @param agent - The agent to be deleted + */ + const handleDeleteAgent = (agent: Agent) => { + setAgentToDelete(agent); + setShowDeleteDialog(true); + }; + + /** + * Confirms and executes the agent deletion + * Only called when user explicitly confirms the deletion + */ + const confirmDeleteAgent = async () => { + if (!agentToDelete?.id) return; try { - await api.deleteAgent(id); + setIsDeleting(true); + await api.deleteAgent(agentToDelete.id); setToast({ message: "Agent deleted successfully", type: "success" }); await loadAgents(); await loadRuns(); // Reload runs as they might be affected } catch (err) { console.error("Failed to delete agent:", err); setToast({ message: "Failed to delete agent", type: "error" }); + } finally { + setIsDeleting(false); + setShowDeleteDialog(false); + setAgentToDelete(null); } }; + /** + * Cancels the delete operation and closes the dialog + */ + const cancelDeleteAgent = () => { + setShowDeleteDialog(false); + setAgentToDelete(null); + }; + const handleEditAgent = (agent: Agent) => { setSelectedAgent(agent); setView("edit"); @@ -473,7 +510,7 @@ export const CCAgents: React.FC = ({ onBack, className }) => { + + + + ); };