1
1
import { ChatRepresentation } from "imcore-ajax-core" ;
2
- import React , { createContext , PropsWithChildren , useCallback , useState } from "react" ;
2
+ import React , { createContext , PropsWithChildren , useCallback , useMemo , useState } from "react" ;
3
3
import { useSelector } from "react-redux" ;
4
4
import { selectChats } from "../app/reducers/chats" ;
5
5
import { selectHandleIDToContact } from "../app/reducers/contacts" ;
@@ -21,19 +21,11 @@ export function ChatSearchProvider({ children }: PropsWithChildren<{}>) {
21
21
const allChats = Object . values ( useSelector ( selectChats ) ) ;
22
22
const contacts = useSelector ( selectHandleIDToContact ) ;
23
23
24
- const [ searchCriteria , internalSetSearchCriteria ] = useState < string > ( ) ;
25
- const [ chatResults , setChatResults ] = useState < ChatRepresentation [ ] > ( ) ;
24
+ const [ searchCriteria , setSearchCriteria ] = useState < string > ( ) ;
26
25
27
- const setSearchCriteria = ( searchCriteria : string | undefined ) => {
28
- internalSetSearchCriteria ( searchCriteria ) ;
29
-
30
- if ( ! searchCriteria ) setChatResults ( allChats . slice ( ) ) ;
31
- else {
32
- setChatResults ( allChats . filter ( chat => {
33
- return [ chat . displayName , chat . id , chat . lastMessage , chat . participants , chat . participants . map ( participant => contacts [ participant ] ?. fullName ) ] . flat ( ) . some ( text => text ?. toLowerCase ( ) . includes ( searchCriteria ) ) ;
34
- } ) ) ;
35
- } ;
36
- } ;
26
+ const chatResults = useMemo ( ( ) => ! searchCriteria ? allChats : allChats . filter ( chat => {
27
+ return [ chat . displayName , chat . id , chat . lastMessage , chat . participants , chat . participants . map ( participant => contacts [ participant ] ?. fullName ) ] . flat ( ) . some ( text => text ?. toLowerCase ( ) . includes ( searchCriteria ) ) ;
28
+ } ) , [ allChats , searchCriteria ] ) ;
37
29
38
30
const clearSearchCriteria = useCallback ( ( ) => {
39
31
0 commit comments