"use client"; import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; 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"; import { RippleButton } from "./ui/ripple"; 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); const [proxyUsage, setProxyUsage] = useState>({}); // 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); } }, []); const loadProxyUsage = useCallback(async () => { try { const profiles = await invoke>( "list_browser_profiles", ); const counts: Record = {}; for (const p of profiles) { if (p.proxy_id) { counts[p.proxy_id] = (counts[p.proxy_id] ?? 0) + 1; } } setProxyUsage(counts); } catch (error) { // Non-fatal console.error("Failed to load proxy usage:", error); } }, []); useEffect(() => { if (isOpen) { loadStoredProxies(); void loadProxyUsage(); if (isProxyDisabled) { setSelectedProxyId(null); } else { // Reset to initial proxy ID when dialog opens setSelectedProxyId(initialProxyId || null); } } }, [ isOpen, isProxyDisabled, loadStoredProxies, initialProxyId, loadProxyUsage, ]); // Refresh usage when profiles change useEffect(() => { let unlisten: (() => void) | undefined; const setup = async () => { try { unlisten = await listen("profile-updated", () => { void loadProxyUsage(); }); } catch (e) { console.error(e); } }; if (isOpen) void setup(); return () => { if (unlisten) unlisten(); }; }, [isOpen, loadProxyUsage]); 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

Create a new proxy configuration

{loading ? (

Loading proxies...

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

No saved proxies available.

)}
)}
Cancel Save
); }