From 9b5f42cda97bc98a4ed5dc0a28200ec735bfcb09 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Mon, 25 Oct 2021 14:25:06 +0530 Subject: [PATCH] Fix profile picture inconsistency (#104, #147) Signed-off-by: Ajay Bura --- src/app/organisms/navigation/SideBar.jsx | 28 +++++++++++++++++-- .../profile-editor/ProfileEditor.jsx | 10 +++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/app/organisms/navigation/SideBar.jsx b/src/app/organisms/navigation/SideBar.jsx index cd6de37..294431e 100644 --- a/src/app/organisms/navigation/SideBar.jsx +++ b/src/app/organisms/navigation/SideBar.jsx @@ -24,6 +24,28 @@ import PowerIC from '../../../../public/res/ic/outlined/power.svg'; function ProfileAvatarMenu() { const mx = initMatrix.matrixClient; + const [profile, setProfile] = useState({ + avatarUrl: null, + displayName: mx.getUser(mx.getUserId()).displayName, + }); + + useEffect(() => { + const user = mx.getUser(mx.getUserId()); + const setNewProfile = (avatarUrl, displayName) => setProfile({ + avatarUrl: avatarUrl || null, + displayName: displayName || profile.displayName, + }); + const onAvatarChange = (event, myUser) => { + setNewProfile(myUser.avatarUrl, myUser.displayName); + }; + mx.getProfileInfo(mx.getUserId()).then((info) => { + setNewProfile(info.avatar_url, info.displayname); + }); + user.on('User.avatarUrl', onAvatarChange); + return () => { + user.removeListener('User.avatarUrl', onAvatarChange); + }; + }, []); return ( ( )} /> diff --git a/src/app/organisms/profile-editor/ProfileEditor.jsx b/src/app/organisms/profile-editor/ProfileEditor.jsx index 7125f44..82e6579 100644 --- a/src/app/organisms/profile-editor/ProfileEditor.jsx +++ b/src/app/organisms/profile-editor/ProfileEditor.jsx @@ -1,4 +1,4 @@ -import React, { useState, useRef } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import initMatrix from '../../../client/initMatrix'; @@ -17,11 +17,17 @@ function ProfileEditor({ const mx = initMatrix.matrixClient; const displayNameRef = useRef(null); const bgColor = colorMXID(userId); - const [avatarSrc, setAvatarSrc] = useState(mx.mxcUrlToHttp(mx.getUser(mx.getUserId()).avatarUrl, 80, 80, 'crop') || null); + const [avatarSrc, setAvatarSrc] = useState(null); const [disabled, setDisabled] = useState(true); let username = mx.getUser(mx.getUserId()).displayName; + useEffect(() => { + mx.getProfileInfo(mx.getUserId()).then((info) => { + setAvatarSrc(info.avatar_url ? mx.mxcUrlToHttp(info.avatar_url, 80, 80, 'crop') : null); + }); + }, [userId]); + // Sets avatar URL and updates the avatar component in profile editor to reflect new upload function handleAvatarUpload(url) { if (url === null) {