Fix error on room leave
authorAjay Bura <ajbura@gmail.com>
Sun, 9 Jan 2022 04:59:06 +0000 (10:29 +0530)
committerAjay Bura <ajbura@gmail.com>
Sun, 9 Jan 2022 04:59:06 +0000 (10:29 +0530)
Signed-off-by: Ajay Bura <ajbura@gmail.com>
src/app/organisms/emoji-board/EmojiBoard.jsx
src/client/state/navigation.js

index a920387175985fd2c25e8700ae61bca69aa7c65d..7a13b6905ec91e4b64ba9c3a7cc5a477f6994ee9 100644 (file)
@@ -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);
+  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;
-    }
+      // 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);
-  }
+      setAvailableEmojis(packs);
+    };
 
-  // Register the above function as an event listener
-  useEffect(() => {
     navigation.on(cons.events.navigation.ROOM_SELECTED, updateAvailableEmoji);
     return () => {
       navigation.removeListener(cons.events.navigation.ROOM_SELECTED, updateAvailableEmoji);
index fb8873ef5f1d6b4a6430e1f2c2202ec47195c153..c8c7b2ae82bd11918f6a2f382e6af6d305cde9ae 100644 (file)
@@ -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);