Add recent opened room in search
authorAjay Bura <ajbura@gmail.com>
Sat, 11 Dec 2021 04:25:38 +0000 (09:55 +0530)
committerAjay Bura <ajbura@gmail.com>
Sat, 11 Dec 2021 04:25:38 +0000 (09:55 +0530)
Signed-off-by: Ajay Bura <ajbura@gmail.com>
src/app/organisms/search/Search.jsx
src/client/state/navigation.js

index 56e5e3c0cb7024306de29eb82a3798e8f94844f4..b03c8016186844299816f58ee33b60577bbd4c3f 100644 (file)
@@ -13,7 +13,6 @@ import IconButton from '../../atoms/button/IconButton';
 import Input from '../../atoms/input/Input';
 import RawModal from '../../atoms/modal/RawModal';
 import ScrollView from '../../atoms/scroll/ScrollView';
-import Divider from '../../atoms/divider/Divider';
 import RoomSelector from '../../molecules/room-selector/RoomSelector';
 
 import SearchIC from '../../../../public/res/ic/outlined/search.svg';
@@ -51,6 +50,35 @@ function useVisiblityToggle(setResult) {
   return [isOpen, requestClose];
 }
 
+function mapRoomIds(roomIds, type) {
+  const mx = initMatrix.matrixClient;
+  const { directs, roomIdToParents } = initMatrix.roomList;
+
+  return roomIds.map((roomId) => {
+    let roomType = type;
+
+    if (!roomType) {
+      roomType = directs.has(roomId) ? 'direct' : 'room';
+    }
+
+    const room = mx.getRoom(roomId);
+    const parentSet = roomIdToParents.get(roomId);
+    const parentNames = parentSet
+      ? [...parentSet].map((parentId) => mx.getRoom(parentId).name)
+      : undefined;
+
+    const parents = parentNames ? parentNames.join(', ') : null;
+
+    return ({
+      type: roomType,
+      name: room.name,
+      parents,
+      roomId,
+      room,
+    });
+  });
+}
+
 function Search() {
   const [result, setResult] = useState(null);
   const [asyncSearch] = useState(new AsyncSearch());
@@ -67,25 +95,6 @@ function Search() {
 
   const generateResults = (term) => {
     const prefix = term.match(/^[#@*]/)?.[0];
-    const { roomIdToParents } = initMatrix.roomList;
-
-    const mapRoomIds = (roomIds, type) => roomIds.map((roomId) => {
-      const room = mx.getRoom(roomId);
-      const parentSet = roomIdToParents.get(roomId);
-      const parentNames = parentSet
-        ? [...parentSet].map((parentId) => mx.getRoom(parentId).name)
-        : undefined;
-
-      const parents = parentNames ? parentNames.join(', ') : null;
-
-      return ({
-        type,
-        name: room.name,
-        parents,
-        roomId,
-        room,
-      });
-    });
 
     if (term.length === 1) {
       const { roomList } = initMatrix;
@@ -112,8 +121,14 @@ function Search() {
     }
   };
 
+  const loadRecentRooms = () => {
+    const { recentRooms } = navigation;
+    handleSearchResults(mapRoomIds(recentRooms).reverse(), '');
+  };
+
   const handleAfterOpen = () => {
     searchRef.current.focus();
+    loadRecentRooms();
     asyncSearch.on(asyncSearch.RESULT_SENT, handleSearchResults);
 
     if (typeof result.term === 'string') {
@@ -128,6 +143,10 @@ function Search() {
 
   const handleOnChange = () => {
     const { value } = searchRef.current;
+    if (value.length === 0) {
+      loadRecentRooms();
+      return;
+    }
     generateResults(value);
   };
 
index ea84dcad17c43489f29b3b045b9067e3821ca693..bedbed129213d7424add0faffc560e8e082ea220 100644 (file)
@@ -11,6 +11,7 @@ class Navigation extends EventEmitter {
     this.selectedSpacePath = [cons.tabs.HOME];
 
     this.selectedRoomId = null;
+    this.recentRooms = [];
   }
 
   _setSpacePath(roomId) {
@@ -26,6 +27,24 @@ class Navigation extends EventEmitter {
     this.selectedSpacePath.push(roomId);
   }
 
+  removeRecentRoom(roomId) {
+    if (typeof roomId !== 'string') return;
+    const roomIdIndex = this.recentRooms.indexOf(roomId);
+    if (roomIdIndex >= 0) {
+      this.recentRooms.splice(roomIdIndex, 1);
+    }
+  }
+
+  addRecentRoom(roomId) {
+    if (typeof roomId !== 'string') return;
+
+    this.removeRecentRoom(roomId);
+    this.recentRooms.push(roomId);
+    if (this.recentRooms.length > 10) {
+      this.recentRooms.splice(0, 1);
+    }
+  }
+
   navigate(action) {
     const actions = {
       [cons.actions.navigation.SELECT_TAB]: () => {
@@ -50,6 +69,8 @@ class Navigation extends EventEmitter {
       [cons.actions.navigation.SELECT_ROOM]: () => {
         const prevSelectedRoomId = this.selectedRoomId;
         this.selectedRoomId = action.roomId;
+        this.addRecentRoom(prevSelectedRoomId);
+        this.removeRecentRoom(this.selectedRoomId);
         this.emit(
           cons.events.navigation.ROOM_SELECTED,
           this.selectedRoomId,