Fix crash on room create

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2021-11-23 16:24:12 +05:30
parent e1e8ca9633
commit b6485f91ae
3 changed files with 39 additions and 17 deletions

View file

@ -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,9 +93,9 @@ function CreateRoom({ isOpen, onRequestClose }) {
updateCreatingError('ERROR: Room address is already in use');
updateIsValidAddress(false);
} else updateCreatingError(e.message);
}
updateIsCreatingRoom(false);
}
}
function validateAddress(e) {
const myAddress = e.target.value;

View file

@ -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);
}
}

View file

@ -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
</MenuItem>
<MenuItem iconSrc={LeaveArrowIC} variant="danger" onClick={handleLeaveClick}>Leave</MenuItem>
<MenuItem iconSrc={LeaveArrowIC} variant="danger" onClick={() => handleLeaveClick(toggleMenu)}>Leave</MenuItem>
<MenuHeader>Notification</MenuHeader>
<MenuItem
variant={notifState === cons.notifs.DEFAULT ? 'positive' : 'surface'}