import { useCallback, useEffect, useMemo, useState } from 'react'; import CancelIcon from '@mui/icons-material/Cancel'; import WarningIcon from '@mui/icons-material/Warning'; import { Box, Button, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, FormHelperText, Grid, InputAdornment, MenuItem, TextField, Typography } from '@mui/material'; import { dialogStyle } from 'CustomTheme'; import type Schema from 'async-validator'; import type { ValidateFieldsError } from 'async-validator'; import { ValidatedTextField } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import { numberValue, updateValue } from 'utils'; import { validate } from 'validators'; import { DeviceValueUOM, DeviceValueUOM_s } from './types'; import type { DeviceValue } from './types'; interface DevicesDialogProps { open: boolean; onClose: () => void; onSave: (as: DeviceValue) => void; selectedItem: DeviceValue; writeable: boolean; validator: Schema; progress: boolean; } const DevicesDialog = ({ open, onClose, onSave, selectedItem, writeable, validator, progress }: DevicesDialogProps) => { const { LL } = useI18nContext(); const [editItem, setEditItem] = useState(selectedItem); const [fieldErrors, setFieldErrors] = useState(); const updateFormValue = useMemo(() => updateValue(setEditItem), [setEditItem]); useEffect(() => { if (open) { setFieldErrors(undefined); setEditItem(selectedItem); } }, [open, selectedItem]); const save = useCallback(async () => { try { setFieldErrors(undefined); await validate(validator, editItem); onSave(editItem); } catch (error) { setFieldErrors(error as ValidateFieldsError); } }, [validator, editItem, onSave]); const setUom = useCallback( (uom?: DeviceValueUOM) => { if (uom === undefined) { return; } switch (uom) { case DeviceValueUOM.HOURS: return LL.HOURS(); case DeviceValueUOM.MINUTES: return LL.MINUTES(); case DeviceValueUOM.SECONDS: return LL.SECONDS(); default: return DeviceValueUOM_s[uom]; } }, [LL] ); const showHelperText = useCallback((dv: DeviceValue) => { if (dv.h) return dv.h; if (dv.l) return dv.l.join(' | '); if (dv.m !== undefined && dv.x !== undefined) { return ( <> {dv.m} → {dv.x} ); } return undefined; }, []); const isCommand = useMemo( () => selectedItem.v === '' && selectedItem.c, [selectedItem.v, selectedItem.c] ); const dialogTitle = useMemo(() => { if (isCommand) return LL.RUN_COMMAND(); return writeable ? LL.CHANGE_VALUE() : LL.VALUE(0); }, [isCommand, writeable, LL]); const buttonLabel = useMemo(() => { return isCommand ? LL.EXECUTE() : LL.UPDATE(); }, [isCommand, LL]); const helperText = useMemo( () => showHelperText(editItem), [editItem, showHelperText] ); const valueLabel = LL.VALUE(0); return ( {dialogTitle} {editItem.id.slice(2)} {editItem.l ? ( {editItem.l.map((val) => ( {val} ))} ) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? ( {setUom(editItem.u)} ) } }} /> ) : ( )} {writeable && helperText && ( {helperText} )} {writeable ? ( {progress && ( )} ) : ( )} ); }; export default DevicesDialog;