'use client'; import { useState } from 'react'; import { Icons } from '@/components/ui/Icon'; import { settingsStore } from '@/lib/store/settings-store'; import type { VideoSource } from '@/lib/types'; export function JsonImportTab() { const [jsonText, setJsonText] = useState(''); const [error, setError] = useState(''); const [preview, setPreview] = useState(null); const [imported, setImported] = useState(false); const handleParse = () => { setError(''); setPreview(null); if (!jsonText.trim()) { setError('请粘贴 JSON 内容'); return; } try { const parsed = JSON.parse(jsonText); if (!Array.isArray(parsed)) { setError('JSON 格式错误:应为数组'); return; } const sources: VideoSource[] = []; for (let i = 0; i < parsed.length; i++) { const item = parsed[i]; if (!item.name || !item.baseUrl) { setError(`第 ${i + 1} 项缺少 name 或 baseUrl`); return; } sources.push({ id: item.id || `json-${item.name.toLowerCase().replace(/[^a-z0-9]/g, '-')}-${i}`, name: item.name, baseUrl: item.baseUrl, searchPath: item.searchPath || '/provide/vod', detailPath: item.detailPath || '/provide/vod', enabled: item.enabled !== false, headers: item.headers, priority: item.priority, group: item.group, }); } if (sources.length === 0) { setError('没有有效的视频源'); return; } setPreview(sources); } catch (e) { setError('JSON 解析失败:' + (e instanceof Error ? e.message : '格式错误')); } }; const handleImport = () => { if (!preview) return; const settings = settingsStore.getSettings(); const existingIds = new Set(settings.sources.map(s => s.id)); const newSources = preview.filter(s => !existingIds.has(s.id)); const updatedSources = [...settings.sources, ...newSources]; settingsStore.saveSettings({ ...settings, sources: updatedSources }); setImported(true); setPreview(null); setJsonText(''); }; return (

粘贴 JSON 数组格式的视频源配置。格式: {'[{ "name": "...", "baseUrl": "..." }]'}