import React, { useContext } from 'react'; import { ButtonComponent } from './ButtonComponent'; import { NumberComponent } from './NumberComponent'; import { SliderComponent } from './SliderComponent'; import { EnumComponent } from './EnumComponent'; import { MethodComponent } from './MethodComponent'; import { AsyncMethodComponent } from './AsyncMethodComponent'; import { StringComponent } from './StringComponent'; import { ListComponent } from './ListComponent'; import { DataServiceComponent, DataServiceJSON } from './DataServiceComponent'; import { DeviceConnectionComponent } from './DeviceConnection'; import { ImageComponent } from './ImageComponent'; import { ColouredEnumComponent } from './ColouredEnumComponent'; import { LevelName } from './NotificationsComponent'; import { getIdFromFullAccessPath } from '../utils/stringUtils'; import { WebSettingsContext } from '../WebSettings'; import { updateValue } from '../socket'; type AttributeType = | 'str' | 'bool' | 'float' | 'int' | 'Quantity' | 'list' | 'method' | 'DataService' | 'DeviceConnection' | 'Enum' | 'NumberSlider' | 'Image' | 'ColouredEnum'; type ValueType = boolean | string | number | Record; export type SerializedValue = { type: AttributeType; full_access_path: string; name?: string; value?: ValueType | ValueType[]; readonly: boolean; doc?: string | null; async?: boolean; frontend_render?: boolean; enum?: Record; }; type GenericComponentProps = { attribute: SerializedValue; name: string; parentPath: string; isInstantUpdate: boolean; addNotification: (message: string, levelname?: LevelName) => void; }; export const GenericComponent = React.memo( ({ attribute, name, parentPath, isInstantUpdate, addNotification }: GenericComponentProps) => { const fullAccessPath = [parentPath, name].filter((element) => element).join('.'); const id = getIdFromFullAccessPath(fullAccessPath); const webSettings = useContext(WebSettingsContext); let displayName = name; if (webSettings[fullAccessPath]) { if (webSettings[fullAccessPath].display === false) { return null; } if (webSettings[fullAccessPath].displayName) { displayName = webSettings[fullAccessPath].displayName; } } function changeCallback( value: SerializedValue, callback: (ack: unknown) => void = undefined ) { updateValue(value, callback); } if (attribute.type === 'bool') { return ( ); } else if (attribute.type === 'float' || attribute.type === 'int') { return ( ); } else if (attribute.type === 'Quantity') { return ( ); } else if (attribute.type === 'NumberSlider') { return ( ); } else if (attribute.type === 'Enum') { return ( ); } else if (attribute.type === 'method') { if (!attribute.async) { return ( ); } else { return ( } addNotification={addNotification} displayName={displayName} id={id} render={attribute.frontend_render} /> ); } } else if (attribute.type === 'str') { return ( ); } else if (attribute.type === 'DataService') { return ( ); } else if (attribute.type === 'DeviceConnection') { return ( ); } else if (attribute.type === 'list') { return ( ); } else if (attribute.type === 'Image') { return ( ); } else if (attribute.type === 'ColouredEnum') { return ( ); } else { return
{name}
; } } );