-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: issue: https://linear.app/comm/issue/ENG-3462/add-modal-for-searching-messages This diff is only refactoring Test Plan: Checked that message search modal still works Reviewers: kamil, kuba, michal Reviewed By: michal Subscribers: ashoat, tomek Differential Revision: https://phab.comm.dev/D8290
- Loading branch information
1 parent
e83333f
commit 5dfc71e
Showing
2 changed files
with
73 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// @flow | ||
|
||
import * as React from 'react'; | ||
|
||
import { type ChatMessageInfoItem } from 'lib/selectors/chat-selectors.js'; | ||
import { messageListData } from 'lib/selectors/chat-selectors.js'; | ||
import { | ||
createMessageInfo, | ||
modifyItemForResultScreen, | ||
} from 'lib/shared/message-utils.js'; | ||
import type { RawMessageInfo } from 'lib/types/message-types.js'; | ||
import type { ThreadInfo } from 'lib/types/thread-types.js'; | ||
|
||
import { useSelector } from '../../redux/redux-utils.js'; | ||
|
||
function useParseSearchResults( | ||
threadInfo: ThreadInfo, | ||
searchResults: $ReadOnlyArray<RawMessageInfo>, | ||
): $ReadOnlyArray<ChatMessageInfoItem> { | ||
const userInfos = useSelector(state => state.userStore.userInfos); | ||
|
||
const translatedSearchResults = React.useMemo(() => { | ||
const threadInfos = { [threadInfo.id]: threadInfo }; | ||
return searchResults | ||
.map(rawMessageInfo => | ||
createMessageInfo(rawMessageInfo, null, userInfos, threadInfos), | ||
) | ||
.filter(Boolean); | ||
}, [searchResults, threadInfo, userInfos]); | ||
|
||
const chatMessageInfos = useSelector( | ||
messageListData(threadInfo.id, translatedSearchResults), | ||
); | ||
|
||
const filteredChatMessageInfos = React.useMemo(() => { | ||
if (!chatMessageInfos) { | ||
return []; | ||
} | ||
|
||
const idSet = new Set(translatedSearchResults.map(item => item.id)); | ||
|
||
const chatMessageInfoItems = chatMessageInfos.filter( | ||
item => item.messageInfo && idSet.has(item.messageInfo.id), | ||
); | ||
|
||
const uniqueChatMessageInfoItemsMap = new Map(); | ||
chatMessageInfoItems.forEach( | ||
item => | ||
item.messageInfo && | ||
item.messageInfo.id && | ||
uniqueChatMessageInfoItemsMap.set(item.messageInfo.id, item), | ||
); | ||
|
||
const sortedChatMessageInfoItems = []; | ||
for (let i = 0; i < translatedSearchResults.length; i++) { | ||
sortedChatMessageInfoItems.push( | ||
uniqueChatMessageInfoItemsMap.get(translatedSearchResults[i].id), | ||
); | ||
} | ||
|
||
return sortedChatMessageInfoItems.filter(Boolean); | ||
}, [chatMessageInfos, translatedSearchResults]); | ||
|
||
return React.useMemo( | ||
() => filteredChatMessageInfos.map(item => modifyItemForResultScreen(item)), | ||
[filteredChatMessageInfos], | ||
); | ||
} | ||
|
||
export { useParseSearchResults }; |