import { Box, Button, FormControl, FormHelperText, Input, Modal, ModalDialog, Option, Select, Textarea, Typography, } from '@mui/joy' import { useEffect, useState } from 'react' function CreateThingModal({ isOpen, onClose, onSave, currentThing }) { const [name, setName] = useState(currentThing?.name || '') const [type, setType] = useState(currentThing?.type || 'number') const [state, setState] = useState(currentThing?.state || '') const [errors, setErrors] = useState({}) useEffect(() => { if (type === 'boolean') { if (state !== 'true' && state !== 'false') { setState('false') } } else if (type === 'number') { if (isNaN(state)) { setState(0) } } }, [type]) const isValid = () => { const newErrors = {} if (!name || name.trim() === '') { newErrors.name = 'Name is required' } if (type === 'number' && isNaN(state)) { newErrors.state = 'State must be a number' } if (type === 'boolean' && !['true', 'false'].includes(state)) { newErrors.state = 'State must be true or false' } if ((type === 'text' && !state) || state.trim() === '') { newErrors.state = 'State is required' } setErrors(newErrors) return Object.keys(newErrors).length === 0 } const handleSave = () => { if (!isValid()) { return } onSave({ name, type, id: currentThing?.id, state: state || null }) onClose() } return ( {/* */} {currentThing?.id ? 'Edit' : 'Create'} Thing Name