diff --git a/src/components/SmartAvatar/SmartAvatar.component.tsx b/src/components/SmartAvatar/SmartAvatar.component.tsx new file mode 100644 index 00000000..c5fd44a3 --- /dev/null +++ b/src/components/SmartAvatar/SmartAvatar.component.tsx @@ -0,0 +1,35 @@ +import React, { FC, useMemo } from 'react'; +import { useDeviceDetector, DeviceDetectorHookProps } from 'src/hooks/useDeviceDetector/use-device-detector.hook'; +import qs from 'qs'; +import { Avatar, AvatarProps } from '../Avatar'; + +export interface SmartAvatarProps extends AvatarProps, DeviceDetectorHookProps {} + +/** + * Detects the device performance and adjusts the avatar props accordingly + */ + +const SmartAvatar: FC = (props) => { + const deviceDetector = useDeviceDetector(props); + + const updatedProps: AvatarProps = useMemo(() => { + if (props.modelSrc instanceof Blob) { + return props; + } + const [modelUrl, originalQueryParams] = props.modelSrc.split('?'); + + const queryStringParams = { + ...qs.parse(originalQueryParams), + ...qs.parse(deviceDetector?.toQueryString() || '') + }; + + return { + ...props, + modelSrc: `${modelUrl}?${qs.stringify(queryStringParams)}` + }; + }, [props, deviceDetector]); + + return ; +}; + +export default SmartAvatar;