import { useState, useEffect } from 'react'; import { createPortal } from 'react-dom'; import { useTranslation } from 'react-i18next'; import { X, RefreshCw, Link2, Unlink } from 'lucide-react'; import { request } from '../../../utils/request'; import { showToast } from '../../common/ToastContainer'; import { useAccountStore } from '../../../stores/useAccountStore'; import { ProxyEntry } from '../../../types/config'; interface ProxyBindingManagerProps { isOpen: boolean; onClose: () => void; proxies: ProxyEntry[]; } export default function ProxyBindingManager({ isOpen, onClose, proxies }: ProxyBindingManagerProps) { const { t } = useTranslation(); const { accounts, fetchAccounts } = useAccountStore(); const [bindings, setBindings] = useState>({}); const [isLoading, setIsLoading] = useState(false); const [isSaving, setIsSaving] = useState(null); // Filter enabled proxies for selection const availableProxies = proxies.filter(p => p.enabled); useEffect(() => { if (isOpen) { refreshData(); } }, [isOpen]); const refreshData = async () => { setIsLoading(true); try { await fetchAccounts(); const currentBindings = await request>('get_all_account_bindings'); setBindings(currentBindings || {}); } catch (error) { console.error('Failed to load bindings:', error); showToast(t('settings.proxy_pool.binding.load_failed', 'Failed to load bindings'), 'error'); } finally { setIsLoading(false); } }; const handleBind = async (accountId: string, proxyId: string) => { setIsSaving(accountId); try { if (proxyId === '') { // Unbind await request('unbind_account_proxy', { accountId }); const newBindings = { ...bindings }; delete newBindings[accountId]; setBindings(newBindings); showToast(t('settings.proxy_pool.binding.unbind_success', 'Unbound successfully'), 'success'); } else { // Bind await request('bind_account_proxy', { accountId, proxyId }); setBindings({ ...bindings, [accountId]: proxyId }); showToast(t('settings.proxy_pool.binding.bind_success', 'Bound successfully'), 'success'); } } catch (error) { console.error('Failed to update binding:', error); showToast(t('settings.proxy_pool.binding.update_failed', 'Failed to update binding'), 'error'); } finally { setIsSaving(null); } }; if (!isOpen) return null; return createPortal(
{/* Header */}

{t('settings.proxy_pool.binding.title', 'Account Proxy Bindings')}

{/* Content */}
{isLoading && accounts.length === 0 ? (
) : (
{t('settings.account.email', 'Email')}
{t('settings.proxy_pool.binding.assigned_proxy', 'Assigned Proxy')}
{accounts.map(account => { const currentProxyId = bindings[account.id] || ''; return (
{account.email}
{isSaving === account.id ? (
) : ( bindings[account.id] ? : )}
); })} {accounts.length === 0 && (
{t('settings.account.no_accounts', 'No accounts found')}
)}
)}
{/* Footer */}
, document.body ); }