import { useCallback, useEffect, useState } from 'react'; import { Alert, Button, Card, Input, Modal, Popconfirm, Select, Space, Table, Tag, Tooltip, Typography, } from 'antd'; import type { TableProps } from 'antd'; import { CopyOutlined, DeleteOutlined, GlobalOutlined, ImportOutlined, ReloadOutlined, SearchOutlined, } from '@ant-design/icons'; import { huyaApi, type HuyaAccountItem, type HuyaBrowserExchangeKit } from '../api/modules'; import { getErrorMessage } from '../utils/error'; import { message } from '../utils/antdMessage'; import { copyTextToClipboard } from '../utils/clipboard'; import { huyaExchangeHelperReady, openExchangeTarget, requestExtensionInject } from '../utils/huyaExchange'; const { Text, Paragraph } = Typography; const SCOPE = 'browser_exchange' as const; const IMPORT_PAGE_SIZE = 20; export default function HuyaBrowserExchangePage() { const [pool, setPool] = useState([]); const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(false); // 导入弹窗:候选池走服务端分页筛选(搜索/标签/仅有 CK),与斗鱼操作台导入一致 const [importOpen, setImportOpen] = useState(false); const [importPool, setImportPool] = useState([]); const [importTotal, setImportTotal] = useState(0); const [importPage, setImportPage] = useState(1); const [importLoading, setImportLoading] = useState(false); const [importSearch, setImportSearch] = useState(''); const [importTag, setImportTag] = useState(''); const [importTags, setImportTags] = useState([]); const [importSelectedIds, setImportSelectedIds] = useState([]); const [importAllLoading, setImportAllLoading] = useState(false); const [exchangeKit, setExchangeKit] = useState(null); const [exchangeTitle, setExchangeTitle] = useState(''); const [exchangeId, setExchangeId] = useState(null); const load = useCallback(async () => { setLoading(true); try { const [poolResult, workbenchResult] = await Promise.all([ huyaApi.listAccounts({ include_cookie: false }), huyaApi.listWorkbenchAccounts(SCOPE), ]); setPool(poolResult); const byId = new Map(poolResult.map((item) => [item.id, item])); setAccounts( workbenchResult.account_ids .map((id) => byId.get(Number(id))) .filter((item): item is HuyaAccountItem => Boolean(item)), ); } catch (error) { message.error(getErrorMessage(error)); } finally { setLoading(false); } }, []); const loadImportPool = useCallback(async () => { setImportLoading(true); try { // 候选池 = 账号库按搜索/标签过滤且必须有 Cookie,剔除已加入兑换列表的账号 const result = await huyaApi.listAccountsPaged({ search: importSearch.trim() || undefined, tag: importTag || undefined, has_cookie: true, page: importPage, page_size: IMPORT_PAGE_SIZE, }); const exclude = new Set(accounts.map((item) => item.id)); setImportPool(result.items.filter((item) => !exclude.has(item.id))); setImportTotal(result.total); } catch (error) { message.error(getErrorMessage(error)); } finally { setImportLoading(false); } }, [accounts, importPage, importSearch, importTag]); useEffect(() => { void load(); }, [load]); useEffect(() => { if (importOpen) void loadImportPool(); }, [importOpen, loadImportPool]); useEffect(() => { if (!importOpen) return; void huyaApi.listTags().then(setImportTags).catch(() => {}); }, [importOpen]); // 切换搜索/标签/翻页时清空已选,避免计数与可见行不一致 useEffect(() => { setImportPage(1); setImportSelectedIds([]); }, [importSearch, importTag]); useEffect(() => { setImportSelectedIds([]); }, [importPage]); const persistAccounts = async (ids: number[]): Promise => { const saved = await huyaApi.updateWorkbenchAccounts(ids, SCOPE); const byId = new Map(pool.map((item) => [item.id, item])); setAccounts( saved.account_ids .map((id) => byId.get(Number(id))) .filter((item): item is HuyaAccountItem => Boolean(item)), ); return saved.account_ids; }; const removeAccount = async (record: HuyaAccountItem) => { try { await persistAccounts(accounts.filter((item) => item.id !== record.id).map((item) => item.id)); } catch (error) { message.error(getErrorMessage(error)); } }; const importAccounts = async (ids: number[]) => { const toImport = importPool.filter((item) => ids.includes(item.id)); if (!toImport.length) return; try { await persistAccounts([...new Set([...accounts.map((item) => item.id), ...toImport.map((item) => item.id)])]); setImportSelectedIds([]); setImportOpen(false); message.success(`已导入 ${toImport.length} 个账号`); } catch (error) { message.error(getErrorMessage(error)); } }; const importAllAccounts = async () => { setImportAllLoading(true); try { const result = await huyaApi.listAccountIds({ search: importSearch.trim() || undefined, tag: importTag || undefined, has_cookie: true, }); const existing = new Set(accounts.map((item) => item.id)); const imported = result.account_ids.filter((id) => !existing.has(id)); if (!imported.length) { message.info('当前筛选条件下没有可新增的账号'); return; } await persistAccounts([...accounts.map((item) => item.id), ...imported]); setImportSelectedIds([]); setImportOpen(false); message.success(`已一键导入 ${imported.length} 个账号`); } catch (error) { message.error(getErrorMessage(error)); } finally { setImportAllLoading(false); } }; const handleExchange = async (record: HuyaAccountItem) => { setExchangeId(record.id); try { const kit = await huyaApi.createBrowserExchange(record.id); requestExtensionInject(kit); setExchangeKit(kit); setExchangeTitle(record.nickname || record.username || record.yyuid || `账号#${record.id}`); } catch (error) { message.error(getErrorMessage(error)); } finally { setExchangeId(null); } }; const copyExchangeText = async (text: string, tip: string) => { try { await copyTextToClipboard(text); message.success(tip); } catch { message.error('复制失败,请重试'); } }; const exchangeHelperReady = huyaExchangeHelperReady(); const columns: TableProps['columns'] = [ { title: '账号', render: (_value, record) => ( {record.nickname || record.username || record.uid} {record.username && record.username !== (record.nickname || record.username) && ( {record.username} )} ), }, { title: '标签', dataIndex: 'tag', render: (tag: string) => (tag ? {tag} : '-') }, { title: '登录态', render: (_value, record) => (record.has_cookie ? 有 Cookie : 无 Cookie), }, { title: '操作', width: 200, render: (_value, record) => ( void removeAccount(record)}> {!exchangeHelperReady && ( )} rowKey="id" size="small" columns={columns} dataSource={accounts} loading={loading} pagination={accounts.length > 20 ? { pageSize: 20 } : false} locale={{ emptyText: '还没有账号,点击右上角「导入账号」开始' }} /> setImportOpen(false)} footer={null} width={700} >
} allowClear value={importSearch} onChange={(event) => setImportSearch(event.target.value)} style={{ width: 220 }} />