"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 [isJsonMode, setIsJsonMode] = useState(false); 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 } }; const handleJsonChange = (jsonValue: string) => { try { const parsedConfig = JSON.parse(jsonValue); setConfig(parsedConfig); } catch { // Invalid JSON, ignore for now } }; return ( } >
{/* JSON Mode Toggle */}
JSON Mode
{isJsonMode ? ( <> {/* JSON Editor Mode */}