import { FormEvent, useState, type ReactNode } from 'react' import { Loader2 } from 'lucide-react' import toast from 'react-hot-toast' import { playProfilesAPI, type PlayProfileInput } from '../api/play_profiles' import type { Library, PlayProfile } from '../types' type ProfileFormModalProps = { editing: PlayProfile | null libraries: Library[] defaultUserID: string onClose: () => void onSaved: () => void | Promise } export function ProfileFormModal({ editing, libraries, defaultUserID, onClose, onSaved, }: ProfileFormModalProps) { const [form, setForm] = useState(() => ({ user_id: editing?.user_id ?? defaultUserID, name: editing?.name ?? '', is_default: editing?.is_default ?? false, content_rating_limit: editing?.content_rating_limit ?? '', allow_adult: editing?.allow_adult ?? false, require_pin: editing?.require_pin ?? false, pin: '', preferred_subtitle_lang: editing?.preferred_subtitle_lang ?? '', preferred_audio_lang: editing?.preferred_audio_lang ?? '', autoplay_next: editing?.autoplay_next ?? true, skip_intro: editing?.skip_intro ?? false, allowed_library_ids: editing?.allowed_library_ids ?? [], })) const [saving, setSaving] = useState(false) const update = (patch: Partial) => setForm((current) => ({ ...current, ...patch })) const toggleLib = (id: string) => { const next = form.allowed_library_ids.includes(id) ? form.allowed_library_ids.filter((item) => item !== id) : [...form.allowed_library_ids, id] update({ allowed_library_ids: next }) } const onSubmit = async (event: FormEvent) => { event.preventDefault() setSaving(true) try { if (editing) { await playProfilesAPI.update(editing.id, form) } else { await playProfilesAPI.create(form) } toast.success('已保存') await onSaved() } catch (err: unknown) { const msg = (err as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '保存失败' toast.error(msg) } finally { setSaving(false) } } return (

{editing ? '编辑 Profile' : '创建 Profile'}

update({ name: event.target.value })} /> update({ is_default: value })} /> update({ allow_adult: value })} /> update({ require_pin: value })} /> {form.require_pin && ( update({ pin: event.target.value })} /> )}
update({ autoplay_next: value })} /> update({ skip_intro: value })} />
{libraries.map((library) => ( ))} {libraries.length === 0 && ( 暂无媒体库 )}
) } function Field({ label, children }: { label: string; children: ReactNode }) { return ( ) } function Toggle({ label, hint, checked, onChange, }: { label: string hint?: string checked: boolean onChange: (value: boolean) => void }) { return ( ) }