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 PropTypes from 'prop-types';
import './CreateRoom.scss'; import './CreateRoom.scss';
import initMatrix from '../../../client/initMatrix'; import initMatrix from '../../../client/initMatrix';
import cons from '../../../client/state/cons';
import { isRoomAliasAvailable } from '../../../util/matrixUtil'; import { isRoomAliasAvailable } from '../../../util/matrixUtil';
import * as roomActions from '../../../client/action/room'; import * as roomActions from '../../../client/action/room';
import { selectRoom } from '../../../client/action/navigation'; import { selectRoom } from '../../../client/action/navigation';
@ -51,6 +52,20 @@ function CreateRoom({ isOpen, onRequestClose }) {
setRoleIndex(0); 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() { async function createRoom() {
if (isCreatingRoom) return; if (isCreatingRoom) return;
updateIsCreatingRoom(true); updateIsCreatingRoom(true);
@ -67,13 +82,9 @@ function CreateRoom({ isOpen, onRequestClose }) {
const powerLevel = roleIndex === 1 ? 101 : undefined; const powerLevel = roleIndex === 1 ? 101 : undefined;
try { try {
const result = await roomActions.create({ await roomActions.create({
name, topic, isPublic, roomAlias, isEncrypted, powerLevel, name, topic, isPublic, roomAlias, isEncrypted, powerLevel,
}); });
resetForm();
selectRoom(result.room_id);
onRequestClose();
} catch (e) { } catch (e) {
if (e.message === 'M_UNKNOWN: Invalid characters in room alias') { if (e.message === 'M_UNKNOWN: Invalid characters in room alias') {
updateCreatingError('ERROR: Invalid characters in room address'); updateCreatingError('ERROR: Invalid characters in room address');
@ -82,8 +93,8 @@ function CreateRoom({ isOpen, onRequestClose }) {
updateCreatingError('ERROR: Room address is already in use'); updateCreatingError('ERROR: Room address is already in use');
updateIsValidAddress(false); updateIsValidAddress(false);
} else updateCreatingError(e.message); } else updateCreatingError(e.message);
updateIsCreatingRoom(false);
} }
updateIsCreatingRoom(false);
} }
function validateAddress(e) { function validateAddress(e) {

View file

@ -97,8 +97,20 @@ function ProfileFooter({ roomId, userId, onRequestClose }) {
const myPowerlevel = room.getMember(mx.getUserId()).powerLevel; const myPowerlevel = room.getMember(mx.getUserId()).powerLevel;
const canIKick = room.currentState.hasSufficientPowerLevelFor('kick', myPowerlevel); const canIKick = room.currentState.hasSufficientPowerLevelFor('kick', myPowerlevel);
const onCreated = (dmRoomId) => {
if (isMountedRef.current === false) return;
setIsCreatingDM(false);
selectRoom(dmRoomId);
onRequestClose();
};
useEffect(() => () => { useEffect(() => () => {
isMountedRef.current = false; isMountedRef.current = false;
const { roomList } = initMatrix;
roomList.on(cons.events.roomList.ROOM_CREATED, onCreated);
return () => {
roomList.removeListener(cons.events.roomList.ROOM_CREATED, onCreated);
};
}, []); }, []);
useEffect(() => { useEffect(() => {
setIsUserIgnored(initMatrix.matrixClient.isUserIgnored(userId)); setIsUserIgnored(initMatrix.matrixClient.isUserIgnored(userId));
@ -113,7 +125,7 @@ function ProfileFooter({ roomId, userId, onRequestClose }) {
for (let i = 0; i < directIds.length; i += 1) { for (let i = 0; i < directIds.length; i += 1) {
const dRoom = mx.getRoom(directIds[i]); const dRoom = mx.getRoom(directIds[i]);
const roomMembers = dRoom.getMembers(); const roomMembers = dRoom.getMembers();
if (roomMembers.length <= 2 && dRoom.currentState.members[userId]) { if (roomMembers.length <= 2 && dRoom.getMember(userId)) {
selectRoom(directIds[i]); selectRoom(directIds[i]);
onRequestClose(); onRequestClose();
return; return;
@ -123,17 +135,13 @@ function ProfileFooter({ roomId, userId, onRequestClose }) {
// Create new DM // Create new DM
try { try {
setIsCreatingDM(true); setIsCreatingDM(true);
const result = await roomActions.create({ await roomActions.create({
isEncrypted: true, isEncrypted: true,
isDirect: true, isDirect: true,
invite: [userId], invite: [userId],
}); });
if (isMountedRef.current === false) return;
setIsCreatingDM(false);
selectRoom(result.room_id);
onRequestClose();
} catch { } catch {
if (isMountedRef.current === false) return;
setIsCreatingDM(false); setIsCreatingDM(false);
} }
} }

View file

@ -147,8 +147,11 @@ function RoomOptions() {
}, []); }, []);
const handleInviteClick = () => openInviteUser(roomId); const handleInviteClick = () => openInviteUser(roomId);
const handleLeaveClick = () => { const handleLeaveClick = (toggleMenu) => {
if (confirm('Are you really want to leave this room?')) roomActions.leave(roomId); if (confirm('Are you really want to leave this room?')) {
roomActions.leave(roomId);
toggleMenu();
}
}; };
function setNotif(nState, currentNState) { function setNotif(nState, currentNState) {
@ -172,7 +175,7 @@ function RoomOptions() {
> >
Invite Invite
</MenuItem> </MenuItem>
<MenuItem iconSrc={LeaveArrowIC} variant="danger" onClick={handleLeaveClick}>Leave</MenuItem> <MenuItem iconSrc={LeaveArrowIC} variant="danger" onClick={() => handleLeaveClick(toggleMenu)}>Leave</MenuItem>
<MenuHeader>Notification</MenuHeader> <MenuHeader>Notification</MenuHeader>
<MenuItem <MenuItem
variant={notifState === cons.notifs.DEFAULT ? 'positive' : 'surface'} variant={notifState === cons.notifs.DEFAULT ? 'positive' : 'surface'}