'use client'; import { useState } from 'react'; import { fetchSourcesFromUrl, type ImportResult } from '@/lib/utils/source-import-utils'; interface LinkImportTabProps { onImport: (result: ImportResult) => Promise | boolean; } export function LinkImportTab({ onImport }: LinkImportTabProps) { const [url, setUrl] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [preview, setPreview] = useState(null); const [success, setSuccess] = useState(false); const handleFetch = async () => { if (!url.trim()) return; setLoading(true); setError(''); setPreview(null); setSuccess(false); try { // In a real app, this might need a crossover/cors proxy if target server doesn't support CORS // For now, we assume the user provides a CORS-friendly URL (like GitHub raw, etc) const result = await fetchSourcesFromUrl(url); setPreview(result); } catch (err: unknown) { console.error(err); setError(err instanceof Error ? err.message : '获取链接失败,请检查URL是否正确或是否存在跨域限制'); } finally { setLoading(false); } }; const handleConfirmImport = async () => { if (!preview) return; try { const result = await onImport(preview); if (result) { setSuccess(true); setPreview(null); setUrl(''); } else { setError('导入处理失败'); } } catch { setError('导入过程发生错误'); } }; return (
setUrl(e.target.value)} placeholder="https://example.com/sources.json" onKeyDown={(e) => e.key === 'Enter' && handleFetch()} disabled={loading || success} className="flex-1 bg-[var(--glass-bg)] border border-[var(--glass-border)] rounded-[var(--radius-2xl)] px-4 py-3 text-[var(--text-color)] placeholder:text-[var(--text-color-secondary)] focus:outline-none focus:border-[var(--accent-color)] focus:ring-2 focus:ring-[color-mix(in_srgb,var(--accent-color)_30%,transparent)] transition-all" />

支持 JSON 配置文件格式的单个或多个源配置链接

{error && (
{error}
)} {preview && (

解析成功

普通源 {preview.normalSources.length}
成人源 {preview.premiumSources.length}
)} {success && (
导入成功!正在刷新...
)}
); }