From f291c74f1371ce4b18b709f3c0e6f90b5d070c04 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Fri, 18 Mar 2022 15:47:05 +0530 Subject: [PATCH] Optimize generateResults function codes Signed-off-by: Ajay Bura --- src/app/organisms/search/Search.jsx | 44 +++++++++++++---------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/app/organisms/search/Search.jsx b/src/app/organisms/search/Search.jsx index d73dc516..1eddbf67 100644 --- a/src/app/organisms/search/Search.jsx +++ b/src/app/organisms/search/Search.jsx @@ -91,33 +91,27 @@ function Search() { const generateResults = (term) => { const prefix = term.match(/^[#@*]/)?.[0]; - if (term.length === 1) { - const { roomList } = initMatrix; - const spaceIds = [...roomList.spaces]; - const roomIds = [...roomList.rooms]; - const directIds = [...roomList.directs]; - - if (prefix === '*') { - const mappedIds = mapRoomIds(spaceIds.sort(roomIdByActivity)); - asyncSearch.setup(mappedIds, { keys: 'name', isContain: true, limit: 20 }); - handleSearchResults(mappedIds, '*'); - } else if (prefix === '#') { - const mappedIds = mapRoomIds(roomIds.sort(roomIdByActivity)); - asyncSearch.setup(mappedIds, { keys: 'name', isContain: true, limit: 20 }); - handleSearchResults(mappedIds, '#'); - } else if (prefix === '@') { - const mappedIds = mapRoomIds(directIds.sort(roomIdByActivity)); - asyncSearch.setup(mappedIds, { keys: 'name', isContain: true, limit: 20 }); - handleSearchResults(mappedIds, '@'); - } else { - const ids = roomIds.concat(directIds, spaceIds); - const mappedIds = mapRoomIds(ids.sort(roomIdByActivity)); - asyncSearch.setup(mappedIds, { keys: 'name', isContain: true, limit: 20 }); - asyncSearch.search(term); - } - } else { + if (term.length > 1) { asyncSearch.search(prefix ? term.slice(1) : term); + return; } + + const { spaces, rooms, directs } = initMatrix.roomList; + let ids = null; + + if (prefix) { + if (prefix === '#') ids = [...rooms]; + else if (prefix === '@') ids = [...directs]; + else ids = [...spaces]; + } else { + ids = [...rooms].concat([...directs], [...spaces]); + } + + ids.sort(roomIdByActivity); + const mappedIds = mapRoomIds(ids); + asyncSearch.setup(mappedIds, { keys: 'name', isContain: true, limit: 20 }); + if (prefix) handleSearchResults(mappedIds, prefix); + else asyncSearch.search(term); }; const loadRecentRooms = () => {