/** * Proxy Settings Component * Configure browser proxy settings with connectivity testing */ import { useState, useEffect } from 'react'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { Wifi, RefreshCw, AlertCircle, CheckCircle, Loader2 } from 'lucide-react'; import { useI18n } from '@/contexts'; import { fetchProxyConfig, updateProxyConfig, testProxyConnectivity } from '@/api'; import type { ProxyConfig, ProxyTestResult } from '@/api'; import styles from './SettingsPanel.module.css'; export function ProxySettings() { const { t } = useI18n(); const queryClient = useQueryClient(); const [localConfig, setLocalConfig] = useState({ enabled: false, address: 'http://127.0.0.1:7890' }); const [testResult, setTestResult] = useState(null); const [hasChanges, setHasChanges] = useState(false); // Fetch current config const { data: config, isLoading } = useQuery({ queryKey: ['proxyConfig'], queryFn: fetchProxyConfig, }); // Update local state when config loads useEffect(() => { if (config) { setLocalConfig(config); setHasChanges(false); } }, [config]); // Update config mutation const updateMutation = useMutation({ mutationFn: updateProxyConfig, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['proxyConfig'] }); setHasChanges(false); }, }); // Test connectivity mutation const testMutation = useMutation({ mutationFn: (address: string) => testProxyConnectivity(address), onSuccess: (result) => setTestResult(result), }); const handleEnabledChange = (enabled: boolean) => { setLocalConfig((prev) => ({ ...prev, enabled })); setHasChanges(true); }; const handleAddressChange = (address: string) => { setLocalConfig((prev) => ({ ...prev, address })); setHasChanges(true); setTestResult(null); }; const handleSave = () => { updateMutation.mutate(localConfig); }; const handleTest = () => { if (localConfig.address.trim()) { testMutation.mutate(localConfig.address); } }; if (isLoading) { return (
{t.common.loading}
); } return (
{/* Enable Toggle */}
{t.settingsPage.enableBrowserProxy} {t.settingsPage.enableBrowserProxyDesc}
{/* Proxy Address Input */}
handleAddressChange(e.target.value)} placeholder="http://127.0.0.1:7890" aria-label={t.settingsPage.proxyAddress} />
{/* Action Buttons */}
{/* Test Result */} {testResult && (
{testResult.success ? : } {testResult.message} {testResult.latency_ms && ` (${testResult.latency_ms}ms)`}
)}
); }