Refactor code of profile viewer

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2022-01-12 16:46:56 +05:30
parent 57fc8b2f1a
commit e38ddebfb6

View file

@ -230,20 +230,10 @@ ProfileFooter.propTypes = {
onRequestClose: PropTypes.func.isRequired, onRequestClose: PropTypes.func.isRequired,
}; };
function ProfileViewer() { function useToggleDialog() {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [roomId, setRoomId] = useState(null); const [roomId, setRoomId] = useState(null);
const [userId, setUserId] = useState(null); const [userId, setUserId] = useState(null);
const [, forceUpdate] = useForceUpdate();
const mx = initMatrix.matrixClient;
const room = roomId ? mx.getRoom(roomId) : null;
let username = '';
if (room !== null) {
const roomMember = room.getMember(userId);
if (roomMember) username = getUsernameOfRoomMember(roomMember);
else username = getUsername(userId);
}
useEffect(() => { useEffect(() => {
const loadProfile = (uId, rId) => { const loadProfile = (uId, rId) => {
@ -257,6 +247,19 @@ function ProfileViewer() {
}; };
}, []); }, []);
const closeDialog = () => setIsOpen(false);
const afterClose = () => {
setUserId(null);
setRoomId(null);
};
return [isOpen, roomId, userId, closeDialog, afterClose];
}
function useRerenderOnRoleChange(roomId, userId) {
const mx = initMatrix.matrixClient;
const [, forceUpdate] = useForceUpdate();
useEffect(() => { useEffect(() => {
const handlePowerLevelChange = (mEvent, member) => { const handlePowerLevelChange = (mEvent, member) => {
if (mEvent.getRoomId() === roomId && member.userId === userId) { if (mEvent.getRoomId() === roomId && member.userId === userId) {
@ -268,17 +271,27 @@ function ProfileViewer() {
mx.removeListener('RoomMember.powerLevel', handlePowerLevelChange); mx.removeListener('RoomMember.powerLevel', handlePowerLevelChange);
}; };
}, [roomId, userId]); }, [roomId, userId]);
}
const handleAfterClose = () => { function ProfileViewer() {
setUserId(null); const [isOpen, roomId, userId, closeDialog, handleAfterClose] = useToggleDialog();
setRoomId(null); useRerenderOnRoleChange(roomId, userId);
};
function renderProfile() { const mx = initMatrix.matrixClient;
const member = room.getMember(userId) || mx.getUser(userId) || {}; const room = mx.getRoom(roomId);
const avatarMxc = member.getMxcAvatarUrl?.() || member.avatarUrl;
const powerLevel = member.powerLevel || 0; let [roomMember, username] = [null, null];
if (roomId) {
roomMember = room.getMember(userId);
username = roomMember ? getUsernameOfRoomMember(roomMember) : getUsername(userId);
}
const renderProfile = () => {
const avatarMxc = roomMember.getMxcAvatarUrl?.() || mx.getMember(userId).avatarUrl;
const avatarUrl = avatarMxc ? mx.mxcUrlToHttp(avatarMxc, 80, 80, 'crop') : null;
const powerLevel = roomMember.powerLevel || 0;
const myPowerLevel = room.getMember(mx.getUserId())?.powerLevel || 0; const myPowerLevel = room.getMember(mx.getUserId())?.powerLevel || 0;
const canChangeRole = ( const canChangeRole = (
@ -295,6 +308,7 @@ function ProfileViewer() {
roomActions.setPowerLevel(roomId, userId, newPowerLevel); roomActions.setPowerLevel(roomId, userId, newPowerLevel);
} }
}; };
const handlePowerSelector = (e) => { const handlePowerSelector = (e) => {
openReusableContextMenu( openReusableContextMenu(
'bottom', 'bottom',
@ -315,12 +329,7 @@ function ProfileViewer() {
return ( return (
<div className="profile-viewer"> <div className="profile-viewer">
<div className="profile-viewer__user"> <div className="profile-viewer__user">
<Avatar <Avatar imageSrc={avatarUrl} text={username} bgColor={colorMXID(userId)} size="large" />
imageSrc={!avatarMxc ? null : mx.mxcUrlToHttp(avatarMxc, 80, 80, 'crop')}
text={username}
bgColor={colorMXID(userId)}
size="large"
/>
<div className="profile-viewer__user__info"> <div className="profile-viewer__user__info">
<Text variant="s1" weight="medium">{twemojify(username)}</Text> <Text variant="s1" weight="medium">{twemojify(username)}</Text>
<Text variant="b2">{twemojify(userId)}</Text> <Text variant="b2">{twemojify(userId)}</Text>
@ -340,12 +349,12 @@ function ProfileViewer() {
<ProfileFooter <ProfileFooter
roomId={roomId} roomId={roomId}
userId={userId} userId={userId}
onRequestClose={() => setIsOpen(false)} onRequestClose={closeDialog}
/> />
)} )}
</div> </div>
); );
} };
return ( return (
<Dialog <Dialog
@ -353,8 +362,8 @@ function ProfileViewer() {
isOpen={isOpen} isOpen={isOpen}
title={`${username} in ${room?.name ?? ''}`} title={`${username} in ${room?.name ?? ''}`}
onAfterClose={handleAfterClose} onAfterClose={handleAfterClose}
onRequestClose={() => setIsOpen(false)} onRequestClose={closeDialog}
contentOptions={<IconButton src={CrossIC} onClick={() => setIsOpen(false)} tooltip="Close" />} contentOptions={<IconButton src={CrossIC} onClick={closeDialog} tooltip="Close" />}
> >
{roomId ? renderProfile() : <div />} {roomId ? renderProfile() : <div />}
</Dialog> </Dialog>