Update AvatarImageView

This commit is contained in:
Bill 2021-06-29 15:20:12 -04:00
parent b7e822678f
commit 7bce02f637

View File

@ -1,5 +1,5 @@
import { AvatarScaleType, AvatarSetType } from 'nitro-renderer'; import { AvatarScaleType, AvatarSetType } from 'nitro-renderer';
import { FC, useCallback, useEffect, useState } from 'react'; import { FC, useEffect, useState } from 'react';
import { GetAvatarRenderManager } from '../../../api'; import { GetAvatarRenderManager } from '../../../api';
import { AvatarImageViewProps } from './AvatarImageView.types'; import { AvatarImageViewProps } from './AvatarImageView.types';
@ -8,39 +8,32 @@ export const AvatarImageView: FC<AvatarImageViewProps> = props =>
const { figure = '', gender = 'M', headOnly = false, direction = 0, scale = 1 } = props; const { figure = '', gender = 'M', headOnly = false, direction = 0, scale = 1 } = props;
const [ avatarUrl, setAvatarUrl ] = useState<string>(null); const [ avatarUrl, setAvatarUrl ] = useState<string>(null);
const [ randomValue, setRandomValue ] = useState(-1);
const getUserImageUrl = useCallback(() => useEffect(() =>
{ {
let url = null; if(randomValue) {}
const avatarImage = GetAvatarRenderManager().createAvatarImage(figure, AvatarScaleType.LARGE, gender, { const avatarImage = GetAvatarRenderManager().createAvatarImage(figure, AvatarScaleType.LARGE, gender, {
resetFigure: (figure) => setAvatarUrl(getUserImageUrl()), resetFigure: figure => setRandomValue(Math.random()),
dispose: () => {}, dispose: () => {},
disposed: false disposed: false
}, null); }, null);
if(avatarImage) if(!avatarImage) return;
{
let setType = AvatarSetType.FULL; let setType = AvatarSetType.FULL;
if(headOnly) setType = AvatarSetType.HEAD; if(headOnly) setType = AvatarSetType.HEAD;
avatarImage.setDirection(setType, direction); avatarImage.setDirection(setType, direction);
const image = avatarImage.getCroppedImage(setType); const image = avatarImage.getCroppedImage(setType);
if(image) url = image.src; if(image) setAvatarUrl(image.src);
avatarImage.dispose(); avatarImage.dispose();
} }, [ figure, gender, direction, headOnly, randomValue ]);
return url;
}, [ figure, gender, direction, headOnly ]);
useEffect(() =>
{
setAvatarUrl(getUserImageUrl());
}, [ getUserImageUrl ]);
const url = `url('${ avatarUrl }')`; const url = `url('${ avatarUrl }')`;