import { type FormEvent, type ReactNode, useState } from 'react' import { Loader2 } from 'lucide-react' import toast from 'react-hot-toast' import { downloadClientsAPI, type DownloadClient, type DownloadClientInput, type DownloadClientType, } from '../api/download_clients' import { apiErrorMessage } from './downloadClientPageModel' export function ClientFormModal({ editing, onClose, onSaved, }: { editing: DownloadClient | null onClose: () => void onSaved: () => void | Promise }) { const [form, setForm] = useState(() => ({ name: editing?.name ?? '', type: editing?.type ?? 'qbittorrent', host: editing?.host ?? '', username: editing?.username ?? '', password: '', is_default: editing?.is_default ?? false, enabled: editing?.enabled ?? true, })) const [saving, setSaving] = useState(false) const onSubmit = async (e: FormEvent) => { e.preventDefault() if (saving) return setSaving(true) try { if (editing) await downloadClientsAPI.update(editing.id, form) else await downloadClientsAPI.create(form) toast.success('已保存') setSaving(false) onSaved() } catch (err: unknown) { const msg = apiErrorMessage(err, '保存失败') toast.error(msg) setSaving(false) } } const update = (k: K, v: DownloadClientInput[K]) => setForm((f) => ({ ...f, [k]: v })) const placeholder = ( { qbittorrent: 'http://127.0.0.1:8080', aria2: 'http://127.0.0.1:6800/jsonrpc', transmission: 'http://127.0.0.1:9091/transmission/rpc', } as Record )[form.type] return (

{editing ? '编辑下载器' : '添加下载器'}

update('name', e.target.value)} /> update('host', e.target.value)} /> {form.type !== 'aria2' && ( <> update('username', e.target.value)} /> update('password', e.target.value)} /> )} {form.type === 'aria2' && ( update('password', e.target.value)} /> )}
) } function Field({ label, children }: { label: string; children: ReactNode }) { return ( ) }