From b6485f91ae5a636356529749d08a72c3e8a4b0f2 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Tue, 23 Nov 2021 16:24:12 +0530 Subject: [PATCH] Fix crash on room create Signed-off-by: Ajay Bura --- src/app/organisms/create-room/CreateRoom.jsx | 25 +++++++++++++------ .../profile-viewer/ProfileViewer.jsx | 22 ++++++++++------ src/app/organisms/room-optons/RoomOptions.jsx | 9 ++++--- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/app/organisms/create-room/CreateRoom.jsx b/src/app/organisms/create-room/CreateRoom.jsx index 22ec87f..6c4968a 100644 --- a/src/app/organisms/create-room/CreateRoom.jsx +++ b/src/app/organisms/create-room/CreateRoom.jsx @@ -1,8 +1,9 @@ -import React, { useState, useRef } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import './CreateRoom.scss'; import initMatrix from '../../../client/initMatrix'; +import cons from '../../../client/state/cons'; import { isRoomAliasAvailable } from '../../../util/matrixUtil'; import * as roomActions from '../../../client/action/room'; import { selectRoom } from '../../../client/action/navigation'; @@ -51,6 +52,20 @@ function CreateRoom({ isOpen, onRequestClose }) { setRoleIndex(0); } + const onCreated = (roomId) => { + resetForm(); + selectRoom(roomId); + onRequestClose(); + }; + + useEffect(() => { + const { roomList } = initMatrix; + roomList.on(cons.events.roomList.ROOM_CREATED, onCreated); + return () => { + roomList.removeListener(cons.events.roomList.ROOM_CREATED, onCreated); + }; + }, []); + async function createRoom() { if (isCreatingRoom) return; updateIsCreatingRoom(true); @@ -67,13 +82,9 @@ function CreateRoom({ isOpen, onRequestClose }) { const powerLevel = roleIndex === 1 ? 101 : undefined; try { - const result = await roomActions.create({ + await roomActions.create({ name, topic, isPublic, roomAlias, isEncrypted, powerLevel, }); - - resetForm(); - selectRoom(result.room_id); - onRequestClose(); } catch (e) { if (e.message === 'M_UNKNOWN: Invalid characters in room alias') { updateCreatingError('ERROR: Invalid characters in room address'); @@ -82,8 +93,8 @@ function CreateRoom({ isOpen, onRequestClose }) { updateCreatingError('ERROR: Room address is already in use'); updateIsValidAddress(false); } else updateCreatingError(e.message); + updateIsCreatingRoom(false); } - updateIsCreatingRoom(false); } function validateAddress(e) { diff --git a/src/app/organisms/profile-viewer/ProfileViewer.jsx b/src/app/organisms/profile-viewer/ProfileViewer.jsx index b9fe448..41a43fc 100644 --- a/src/app/organisms/profile-viewer/ProfileViewer.jsx +++ b/src/app/organisms/profile-viewer/ProfileViewer.jsx @@ -97,8 +97,20 @@ function ProfileFooter({ roomId, userId, onRequestClose }) { const myPowerlevel = room.getMember(mx.getUserId()).powerLevel; const canIKick = room.currentState.hasSufficientPowerLevelFor('kick', myPowerlevel); + const onCreated = (dmRoomId) => { + if (isMountedRef.current === false) return; + setIsCreatingDM(false); + selectRoom(dmRoomId); + onRequestClose(); + }; + useEffect(() => () => { isMountedRef.current = false; + const { roomList } = initMatrix; + roomList.on(cons.events.roomList.ROOM_CREATED, onCreated); + return () => { + roomList.removeListener(cons.events.roomList.ROOM_CREATED, onCreated); + }; }, []); useEffect(() => { setIsUserIgnored(initMatrix.matrixClient.isUserIgnored(userId)); @@ -113,7 +125,7 @@ function ProfileFooter({ roomId, userId, onRequestClose }) { for (let i = 0; i < directIds.length; i += 1) { const dRoom = mx.getRoom(directIds[i]); const roomMembers = dRoom.getMembers(); - if (roomMembers.length <= 2 && dRoom.currentState.members[userId]) { + if (roomMembers.length <= 2 && dRoom.getMember(userId)) { selectRoom(directIds[i]); onRequestClose(); return; @@ -123,17 +135,13 @@ function ProfileFooter({ roomId, userId, onRequestClose }) { // Create new DM try { setIsCreatingDM(true); - const result = await roomActions.create({ + await roomActions.create({ isEncrypted: true, isDirect: true, invite: [userId], }); - - if (isMountedRef.current === false) return; - setIsCreatingDM(false); - selectRoom(result.room_id); - onRequestClose(); } catch { + if (isMountedRef.current === false) return; setIsCreatingDM(false); } } diff --git a/src/app/organisms/room-optons/RoomOptions.jsx b/src/app/organisms/room-optons/RoomOptions.jsx index 0c89008..dbacc54 100644 --- a/src/app/organisms/room-optons/RoomOptions.jsx +++ b/src/app/organisms/room-optons/RoomOptions.jsx @@ -147,8 +147,11 @@ function RoomOptions() { }, []); const handleInviteClick = () => openInviteUser(roomId); - const handleLeaveClick = () => { - if (confirm('Are you really want to leave this room?')) roomActions.leave(roomId); + const handleLeaveClick = (toggleMenu) => { + if (confirm('Are you really want to leave this room?')) { + roomActions.leave(roomId); + toggleMenu(); + } }; function setNotif(nState, currentNState) { @@ -172,7 +175,7 @@ function RoomOptions() { > Invite - Leave + handleLeaveClick(toggleMenu)}>Leave Notification