"use client"; import { invoke } from "@tauri-apps/api/core"; import { useCallback, useEffect, useState } from "react"; import { FiPlus } from "react-icons/fi"; import { toast } from "sonner"; import { ProxyFormDialog } from "@/components/proxy-form-dialog"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; import type { StoredProxy } from "@/types"; interface ProxySettingsDialogProps { isOpen: boolean; onClose: () => void; onSave: (proxyId: string | null) => void; initialProxyId?: string | null; browserType?: string; } export function ProxySettingsDialog({ isOpen, onClose, onSave, initialProxyId, browserType, }: ProxySettingsDialogProps) { const [storedProxies, setStoredProxies] = useState([]); const [selectedProxyId, setSelectedProxyId] = useState( initialProxyId || null, ); const [loading, setLoading] = useState(false); const [showProxyForm, setShowProxyForm] = useState(false); // Helper to determine if proxy should be disabled for the selected browser const isProxyDisabled = browserType === "tor-browser"; const loadStoredProxies = useCallback(async () => { try { setLoading(true); const proxies = await invoke("get_stored_proxies"); setStoredProxies(proxies); } catch (error) { console.error("Failed to load stored proxies:", error); toast.error("Failed to load proxies"); } finally { setLoading(false); } }, []); useEffect(() => { if (isOpen) { loadStoredProxies(); if (isProxyDisabled) { setSelectedProxyId(null); } } }, [isOpen, isProxyDisabled, loadStoredProxies]); const handleCreateProxy = useCallback(() => { setShowProxyForm(true); }, []); const handleProxySaved = useCallback((savedProxy: StoredProxy) => { setStoredProxies((prev) => { const existingIndex = prev.findIndex((p) => p.id === savedProxy.id); if (existingIndex >= 0) { // Update existing proxy const updated = [...prev]; updated[existingIndex] = savedProxy; return updated; } else { // Add new proxy return [...prev, savedProxy]; } }); setSelectedProxyId(savedProxy.id); setShowProxyForm(false); }, []); const handleProxyFormClose = useCallback(() => { setShowProxyForm(false); }, []); const handleSave = () => { onSave(selectedProxyId); }; const hasChanged = () => { return selectedProxyId !== initialProxyId; }; return ( <> { if (!open) { onClose(); } }} > Proxy Settings
{isProxyDisabled && (

Tor Browser has its own built-in proxy system and doesn't support additional proxy configuration.

)} {!isProxyDisabled && ( <> {/* Proxy Selection */}

Create a new proxy configuration

{loading ? (

Loading proxies...

) : ( storedProxies.map((proxy) => ( )) )} {!loading && storedProxies.length === 0 && (

No saved proxies available.

)}
)}
); }