From 34bb5f9928e88bec88ed0e3125f959b27dde7f56 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Sun, 9 Jan 2022 10:29:06 +0530 Subject: [PATCH] Fix error on room leave Signed-off-by: Ajay Bura --- src/app/organisms/emoji-board/EmojiBoard.jsx | 44 ++++++++++---------- src/client/state/navigation.js | 2 + 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/app/organisms/emoji-board/EmojiBoard.jsx b/src/app/organisms/emoji-board/EmojiBoard.jsx index a9203871..7a13b690 100644 --- a/src/app/organisms/emoji-board/EmojiBoard.jsx +++ b/src/app/organisms/emoji-board/EmojiBoard.jsx @@ -188,29 +188,29 @@ function EmojiBoard({ onSelect }) { const [availableEmojis, setAvailableEmojis] = useState([]); - // This should be called whenever the room changes, so that we can switch out the emoji - // for whatever packs are relevant to this room - function updateAvailableEmoji(selectedRoomId) { - // Retrieve the packs for the new room - const packs = getRelevantPacks( - initMatrix.matrixClient.getRoom(selectedRoomId), - ) - // Remove packs that aren't marked as emoji packs - .filter((pack) => pack.usage.indexOf('emoticon') !== -1) - // Remove packs without emojis - .filter((pack) => pack.getEmojis().length !== 0); - - // Set an index for each pack so that we know where to jump when the user uses the nav - for (let i = 0; i < packs.length; i += 1) { - packs[i].packIndex = i; - } - - // Update the component state - setAvailableEmojis(packs); - } - - // Register the above function as an event listener useEffect(() => { + const updateAvailableEmoji = (selectedRoomId) => { + if (!selectedRoomId) { + setAvailableEmojis([]); + return; + } + // Retrieve the packs for the new room + // Remove packs that aren't marked as emoji packs + // Remove packs without emojis + const packs = getRelevantPacks( + initMatrix.matrixClient.getRoom(selectedRoomId), + ) + .filter((pack) => pack.usage.indexOf('emoticon') !== -1) + .filter((pack) => pack.getEmojis().length !== 0); + + // Set an index for each pack so that we know where to jump when the user uses the nav + for (let i = 0; i < packs.length; i += 1) { + packs[i].packIndex = i; + } + + setAvailableEmojis(packs); + }; + navigation.on(cons.events.navigation.ROOM_SELECTED, updateAvailableEmoji); return () => { navigation.removeListener(cons.events.navigation.ROOM_SELECTED, updateAvailableEmoji); diff --git a/src/client/state/navigation.js b/src/client/state/navigation.js index fb8873ef..c8c7b2ae 100644 --- a/src/client/state/navigation.js +++ b/src/client/state/navigation.js @@ -73,6 +73,8 @@ class Navigation extends EventEmitter { this.emit(cons.events.navigation.SPACE_SELECTED, this.selectedSpaceId); }, [cons.actions.navigation.SELECT_ROOM]: () => { + if (this.selectedRoomId === action.roomId) return; + const prevSelectedRoomId = this.selectedRoomId; this.selectedRoomId = action.roomId; this.removeRecentRoom(prevSelectedRoomId);