'use client'; import { useState } from 'react'; import type { SourceSubscription } from '@/lib/types'; import { createSubscription } from '@/lib/utils/source-import-utils'; interface SubscriptionImportTabProps { subscriptions: SourceSubscription[]; onAdd: (subscription: SourceSubscription) => Promise | boolean; onRemove: (id: string) => void; onRefresh: (subscription: SourceSubscription) => Promise; } export function SubscriptionImportTab({ subscriptions, onAdd, onRemove, onRefresh }: SubscriptionImportTabProps) { const [url, setUrl] = useState(''); const [name, setName] = useState(''); const [loading, setLoading] = useState(false); const [refreshingIds, setRefreshingIds] = useState>(new Set()); const [error, setError] = useState(''); const handleAddKeydown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') handleAdd(); }; const handleAdd = async () => { if (!url.trim() || !name.trim()) { setError("请输入订阅名称和链接"); return; } setLoading(true); setError(''); try { const newSub = createSubscription(name, url); // Immediately try to fetch (test connection) // The parent handler should assume responsibility for fetching content // but here we just pass the object await onAdd(newSub); setUrl(''); setName(''); } catch (err: unknown) { setError(err instanceof Error ? err.message : '添加订阅失败'); } finally { setLoading(false); } }; const handleRefresh = async (sub: SourceSubscription) => { setRefreshingIds(prev => new Set(prev).add(sub.id)); try { await onRefresh(sub); } finally { setRefreshingIds(prev => { const next = new Set(prev); next.delete(sub.id); return next; }); } }; return (
{/* Add New Subscription */}

添加新订阅

setName(e.target.value)} placeholder="订阅名称 (例如: 每日更新源)" onKeyDown={handleAddKeydown} className="w-full bg-[color-mix(in_srgb,var(--bg-color)_50%,transparent)] border border-[var(--glass-border)] rounded-[var(--radius-2xl)] px-4 py-2 text-[var(--text-color)] placeholder:text-[var(--text-color-secondary)] focus:outline-none focus:border-[var(--accent-color)]" />
setUrl(e.target.value)} placeholder="订阅链接 (URL)" onKeyDown={handleAddKeydown} className="flex-1 bg-[color-mix(in_srgb,var(--bg-color)_50%,transparent)] border border-[var(--glass-border)] rounded-[var(--radius-2xl)] px-4 py-2 text-[var(--text-color)] placeholder:text-[var(--text-color-secondary)] focus:outline-none focus:border-[var(--accent-color)]" />
{error &&

{error}

}
{/* Subscription List */}
{subscriptions.length === 0 && (
暂无订阅
)} {subscriptions.map(sub => (
{sub.name}

{sub.url}

上次更新: {new Date(sub.lastUpdated).toLocaleString()}

))}
); }