"use client"; import { invoke } from "@tauri-apps/api/core"; import { emit } from "@tauri-apps/api/event"; import * as React from "react"; import { useCallback, useState } from "react"; import { GoPlus } from "react-icons/go"; import { LuPencil, LuTrash2 } from "react-icons/lu"; import { toast } from "sonner"; import { DeleteConfirmationDialog } from "@/components/delete-confirmation-dialog"; import { ProxyFormDialog } from "@/components/proxy-form-dialog"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { useProxyEvents } from "@/hooks/use-proxy-events"; import type { ProxyCheckResult, StoredProxy } from "@/types"; import { ProxyCheckButton } from "./proxy-check-button"; import { RippleButton } from "./ui/ripple"; interface ProxyManagementDialogProps { isOpen: boolean; onClose: () => void; } export function ProxyManagementDialog({ isOpen, onClose, }: ProxyManagementDialogProps) { const [showProxyForm, setShowProxyForm] = useState(false); const [editingProxy, setEditingProxy] = useState(null); const [proxyToDelete, setProxyToDelete] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const [checkingProxyId, setCheckingProxyId] = useState(null); const [proxyCheckResults, setProxyCheckResults] = useState< Record >({}); const { storedProxies, proxyUsage, isLoading } = useProxyEvents(); // Load cached check results on mount and when proxies change React.useEffect(() => { const loadCachedResults = async () => { const results: Record = {}; for (const proxy of storedProxies) { try { const cached = await invoke( "get_cached_proxy_check", { proxyId: proxy.id }, ); if (cached) { results[proxy.id] = cached; } } catch (_error) { // Ignore errors } } setProxyCheckResults(results); }; if (storedProxies.length > 0) { void loadCachedResults(); } }, [storedProxies]); const handleDeleteProxy = useCallback((proxy: StoredProxy) => { // Open in-app confirmation dialog setProxyToDelete(proxy); }, []); const handleConfirmDelete = useCallback(async () => { if (!proxyToDelete) return; setIsDeleting(true); try { await invoke("delete_stored_proxy", { proxyId: proxyToDelete.id }); toast.success("Proxy deleted successfully"); await emit("stored-proxies-changed"); } catch (error) { console.error("Failed to delete proxy:", error); toast.error("Failed to delete proxy"); } finally { setIsDeleting(false); setProxyToDelete(null); } }, [proxyToDelete]); const handleCreateProxy = useCallback(() => { setEditingProxy(null); setShowProxyForm(true); }, []); const handleEditProxy = useCallback((proxy: StoredProxy) => { setEditingProxy(proxy); setShowProxyForm(true); }, []); const handleProxyFormClose = useCallback(() => { setShowProxyForm(false); setEditingProxy(null); }, []); return ( <> Proxy Management Manage your saved proxy configurations for reuse across profiles
{/* Create new proxy button */}
Create
{/* Proxies list */} {isLoading ? (
Loading proxies...
) : storedProxies.length === 0 ? (
No proxies created yet. Create your first proxy using the button above.
) : (
Name Usage Actions {storedProxies.map((proxy) => ( {proxy.name} {proxyUsage[proxy.id] ?? 0}
{ setProxyCheckResults((prev) => ({ ...prev, [proxy.id]: result, })); }} onCheckFailed={(result) => { setProxyCheckResults((prev) => ({ ...prev, [proxy.id]: result, })); }} />

Edit proxy

{(proxyUsage[proxy.id] ?? 0) > 0 ? (

Cannot delete: in use by{" "} {proxyUsage[proxy.id]} profile {proxyUsage[proxy.id] > 1 ? "s" : ""}

) : (

Delete proxy

)}
))}
)}
Close
setProxyToDelete(null)} onConfirm={handleConfirmDelete} title="Delete Proxy" description={`This action cannot be undone. This will permanently delete the proxy "${proxyToDelete?.name ?? ""}".`} confirmButtonText="Delete" isLoading={isDeleting} /> ); }