import { memo, useEffect, useState } from 'react'; import CancelIcon from '@mui/icons-material/Cancel'; import CloseIcon from '@mui/icons-material/Close'; import DoneIcon from '@mui/icons-material/Done'; import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, Grid, TextField, Typography } from '@mui/material'; import { dialogStyle } from 'CustomTheme'; import { useI18nContext } from 'i18n/i18n-react'; import { numberValue, updateValue } from 'utils'; import EntityMaskToggle from './EntityMaskToggle'; import { DeviceEntityMask } from './types'; import type { DeviceEntity } from './types'; interface SettingsCustomizationsDialogProps { open: boolean; onClose: () => void; onSave: (di: DeviceEntity) => void; selectedItem: DeviceEntity; } interface LabelValueProps { label: string; value: React.ReactNode; } const LabelValue = memo(({ label, value }: LabelValueProps) => ( {label}:  {value} )); LabelValue.displayName = 'LabelValue'; const ICON_SIZE = 16; const CustomizationsDialog = ({ open, onClose, onSave, selectedItem }: SettingsCustomizationsDialogProps) => { const { LL } = useI18nContext(); const [editItem, setEditItem] = useState(selectedItem); const [error, setError] = useState(false); const updateFormValue = updateValue( setEditItem as unknown as React.Dispatch< React.SetStateAction> > ); const isWriteableNumber = typeof editItem.v === 'number' && editItem.w && !(editItem.m & DeviceEntityMask.DV_READONLY); useEffect(() => { if (open) { setError(false); setEditItem(selectedItem); } }, [open, selectedItem]); const handleClose = ( _event: React.SyntheticEvent, reason: 'backdropClick' | 'escapeKeyDown' ) => { if (reason !== 'backdropClick') { onClose(); } }; const save = () => { if ( isWriteableNumber && editItem.mi && editItem.ma && editItem.mi > editItem.ma ) { setError(true); } else { onSave(editItem); } }; const updateDeviceEntity = (updatedItem: DeviceEntity) => { setEditItem((prev) => ({ ...prev, m: updatedItem.m })); }; return ( {`${LL.EDIT()} ${LL.ENTITY()}`} ) : ( ) } /> {isWriteableNumber && ( <> )} {error && ( Error: Check min and max values )} ); }; export default CustomizationsDialog;