存至私人影库增加下载方式

This commit is contained in:
mtvpls
2026-05-21 23:46:26 +08:00
parent e44f565112
commit a24de1346f
8 changed files with 159 additions and 16 deletions
+64 -8
View File
@@ -6,13 +6,19 @@ import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { AdminConfig } from '@/lib/admin.types';
import { AnimeSubscription } from '@/types/anime-subscription';
import { AnimeSubscription, AnimeSubscriptionDownloadTool } from '@/types/anime-subscription';
interface AnimeSubscriptionComponentProps {
config: AdminConfig | null;
refreshConfig: () => Promise<void>;
}
const downloadToolOptions: Array<{ value: AnimeSubscriptionDownloadTool; label: string }> = [
{ value: 'aria2', label: 'aria2' },
{ value: 'qBittorrent', label: 'qBittorrent' },
{ value: 'Transmission', label: 'Transmission' },
];
// Switch 组件
const Switch = ({ checked, onChange, disabled }: { checked: boolean; onChange: (checked: boolean) => void; disabled?: boolean }) => (
<button
@@ -141,6 +147,7 @@ export default function AnimeSubscriptionComponent({
refreshConfig,
}: AnimeSubscriptionComponentProps) {
const [enabled, setEnabled] = useState(false);
const [downloadTool, setDownloadTool] = useState<AnimeSubscriptionDownloadTool>('aria2');
const [subscriptions, setSubscriptions] = useState<AnimeSubscription[]>([]);
const [loading, setLoading] = useState(false);
const [showAddForm, setShowAddForm] = useState(false);
@@ -181,6 +188,7 @@ export default function AnimeSubscriptionComponent({
useEffect(() => {
if (config?.AnimeSubscriptionConfig) {
setEnabled(config.AnimeSubscriptionConfig.Enabled || false);
setDownloadTool(config.AnimeSubscriptionConfig.DownloadTool || 'aria2');
setSubscriptions(config.AnimeSubscriptionConfig.Subscriptions || []);
}
}, [config]);
@@ -205,7 +213,7 @@ export default function AnimeSubscriptionComponent({
const response = await fetch('/api/admin/anime-subscription/toggle', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled: newEnabled }),
body: JSON.stringify({ enabled: newEnabled, downloadTool }),
});
if (!response.ok) {
@@ -225,6 +233,35 @@ export default function AnimeSubscriptionComponent({
}
};
const handleDownloadToolChange = async (newDownloadTool: AnimeSubscriptionDownloadTool) => {
const previousDownloadTool = downloadTool;
setDownloadTool(newDownloadTool);
try {
setLoading(true);
const response = await fetch('/api/admin/anime-subscription/toggle', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled, downloadTool: newDownloadTool }),
});
if (!response.ok) {
throw new Error('保存下载方式失败');
}
await refreshConfig();
} catch (error) {
setDownloadTool(previousDownloadTool);
showAlert({
type: 'error',
title: '保存失败',
message: error instanceof Error ? error.message : '保存下载方式失败',
});
} finally {
setLoading(false);
}
};
// 开始添加
const handleAdd = () => {
resetForm();
@@ -408,12 +445,31 @@ export default function AnimeSubscriptionComponent({
return (
<div className='space-y-6'>
{/* 顶部控制 */}
<div className='flex items-center justify-between'>
<div className='flex items-center gap-3'>
<span className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</span>
<Switch checked={enabled} onChange={handleToggleEnabled} disabled={loading} />
<div className='flex flex-col gap-4 md:flex-row md:items-center md:justify-between'>
<div className='flex flex-col gap-4 sm:flex-row sm:items-center sm:gap-6'>
<div className='flex items-center gap-3'>
<span className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</span>
<Switch checked={enabled} onChange={handleToggleEnabled} disabled={loading} />
</div>
<div className='flex items-center gap-3'>
<label className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</label>
<select
value={downloadTool}
onChange={(e) => handleDownloadToolChange(e.target.value as AnimeSubscriptionDownloadTool)}
disabled={loading}
className='min-w-40 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-green-500 disabled:opacity-50'
>
{downloadToolOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</div>
</div>
<button
onClick={handleAdd}