feat: Implement custom source editing and refactor source management to use full

This commit is contained in:
kuekhaoyang
2025-11-22 20:57:38 +08:00
parent 976bd3bc12
commit 64636a3da9
11 changed files with 180 additions and 55 deletions
+26 -12
View File
@@ -12,20 +12,26 @@ interface UseAddSourceFormProps {
existingIds: string[];
onAdd: (source: VideoSource) => void;
onClose: () => void;
initialValues?: VideoSource | null;
}
export function useAddSourceForm({ isOpen, existingIds, onAdd, onClose }: UseAddSourceFormProps) {
export function useAddSourceForm({ isOpen, existingIds, onAdd, onClose, initialValues }: UseAddSourceFormProps) {
const [name, setName] = useState('');
const [url, setUrl] = useState('');
const [error, setError] = useState('');
useEffect(() => {
if (isOpen) {
setName('');
setUrl('');
if (initialValues) {
setName(initialValues.name);
setUrl(initialValues.baseUrl);
} else {
setName('');
setUrl('');
}
setError('');
}
}, [isOpen]);
}, [isOpen, initialValues]);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
@@ -43,20 +49,28 @@ export function useAddSourceForm({ isOpen, existingIds, onAdd, onClose }: UseAdd
return;
}
const id = name.toLowerCase().replace(/[^a-z0-9]/g, '-');
if (existingIds.includes(id)) {
setError('此源名称已存在');
return;
let id = initialValues?.id;
// Only generate new ID if not editing or if name changed (optional, maybe keep ID stable?)
// For now, let's keep ID stable if editing, unless we want to allow re-generating ID.
// But if we re-generate ID, we lose history/preferences for that ID.
// So better to keep ID if editing.
if (!id) {
id = name.toLowerCase().replace(/[^a-z0-9]/g, '-');
if (existingIds.includes(id)) {
setError('此源名称已存在');
return;
}
}
const newSource: VideoSource = {
id,
name: name.trim(),
baseUrl: url.trim(),
searchPath: '',
detailPath: '',
enabled: true,
priority: existingIds.length + 1,
searchPath: initialValues?.searchPath || '',
detailPath: initialValues?.detailPath || '',
enabled: initialValues?.enabled ?? true,
priority: initialValues?.priority || existingIds.length + 1,
};
onAdd(newSource);