import { useState } from 'react' import { FileCode, Download, Upload, Copy, Check, AlertTriangle } from 'lucide-react' import clsx from 'clsx' import { useI18n } from '../i18n' import { getBatchImportTemplates } from '../utils/batchImportTemplates' export default function BatchImport({ onRefresh, onMessage, authFetch }) { const { t } = useI18n() const [jsonInput, setJsonInput] = useState('') const [loading, setLoading] = useState(false) const [result, setResult] = useState(null) const [copied, setCopied] = useState(false) const apiFetch = authFetch || fetch const templates = getBatchImportTemplates(t) const handleImport = async () => { if (!jsonInput.trim()) { onMessage('error', t('batchImport.enterJson')) return } let config try { config = JSON.parse(jsonInput) } catch (e) { onMessage('error', t('messages.invalidJson')) return } setLoading(true) setResult(null) try { const res = await apiFetch('/admin/import', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config), }) const data = await res.json() if (res.ok) { setResult(data) onMessage('success', t('batchImport.importSuccess', { keys: data.imported_keys, accounts: data.imported_accounts })) onRefresh() } else { onMessage('error', data.detail || t('messages.importFailed')) } } catch (e) { onMessage('error', t('messages.networkError')) } finally { setLoading(false) } } const loadTemplate = (key) => { const tpl = templates[key] if (tpl) { setJsonInput(JSON.stringify(tpl.config, null, 2)) onMessage('info', t('batchImport.templateLoaded', { name: tpl.name })) } } const handleExport = async () => { try { const res = await apiFetch('/admin/export') if (res.ok) { const data = await res.json() setJsonInput(JSON.stringify(JSON.parse(data.json), null, 2)) onMessage('success', t('batchImport.currentConfigLoaded')) } } catch (e) { onMessage('error', t('batchImport.fetchConfigFailed')) } } const copyBase64 = async () => { try { const res = await apiFetch('/admin/export') if (res.ok) { const data = await res.json() await navigator.clipboard.writeText(data.base64) setCopied(true) setTimeout(() => setCopied(false), 2000) onMessage('success', t('batchImport.copySuccess')) } } catch (e) { onMessage('error', t('messages.copyFailed')) } } return (
{t('batchImport.dataExportDesc')}
{t('batchImport.variableName')}: DS2API_CONFIG_JSON
{t('batchImport.importSummary', { keys: result.imported_keys, accounts: result.imported_accounts })}