"use client"; import { useEffect, useState } from "react"; import PathConfigEditor from "./PathConfigEditor"; import SlideInForm from "./SlideInForm"; interface DatasetConfig { [path: string]: any; } interface DatasetCrudProps { datasetName?: string; datasetConfig?: DatasetConfig; onSave: (name: string, config: DatasetConfig) => void; onClose: () => void; onEnabledChange?: (enabled: boolean) => void; isOpen: boolean; } export default function DatasetCrud({ datasetName = "", datasetConfig = {}, onSave, onClose, onEnabledChange, isOpen }: DatasetCrudProps) { const [name, setName] = useState(datasetName); const [config, setConfig] = useState(datasetConfig); const [newPath, setNewPath] = useState(""); const [enabled, setEnabled] = useState(true); const isEditing = !!datasetName; useEffect(() => { setName(datasetName); setConfig(datasetConfig); // Set enabled state from existing config, defaulting to true setEnabled( datasetConfig.enabled !== undefined ? datasetConfig.enabled : true ); }, [datasetName, datasetConfig]); const handleEnabledChange = (newEnabled: boolean) => { setEnabled(newEnabled); if (onEnabledChange) { onEnabledChange(newEnabled); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!name.trim()) return; // Note: enabled is handled separately via onEnabledChange // so we don't include it in the config here onSave(name, config); }; const addPath = () => { if (!newPath.trim()) return; setConfig({ ...config, [newPath]: {} }); setNewPath(""); }; const removePath = (path: string) => { const newConfig = { ...config }; delete newConfig[path]; setConfig(newConfig); }; const updatePathConfig = (path: string, pathConfig: any) => { try { const parsedConfig = typeof pathConfig === "string" ? JSON.parse(pathConfig) : pathConfig; setConfig({ ...config, [path]: parsedConfig }); } catch { // Invalid JSON, ignore } }; return ( } >
{/* Dataset Name */}
setName(e.target.value)} placeholder="e.g., pr0n, kids, movies" className="block w-full rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" required />
{/* Enabled Toggle */}
{enabled ? "Enabled" : "Disabled"}

When disabled, this dataset will not be monitored for new files.

{/* Paths Configuration */}

Add paths to watch for this dataset. Each path can have its own configuration.

{/* Add new path */}
setNewPath(e.target.value)} placeholder="/path/to/watch" className="flex-1 rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" />
{/* Existing paths */} {Object.entries(config).filter(([key]) => key !== "enabled").length > 0 ? (
{Object.entries(config) .filter(([key]) => key !== "enabled") .map(([path, pathConfig]) => (
{path}
updatePathConfig(path, newConfig) } />
))}
) : (

No paths configured. Add a path above.

)}
); }