11import { ChatRepresentation } from "imcore-ajax-core" ;
2- import React , { createContext , PropsWithChildren , useCallback , useState } from "react" ;
2+ import React , { createContext , PropsWithChildren , useCallback , useMemo , useState } from "react" ;
33import { useSelector } from "react-redux" ;
44import { selectChats } from "../app/reducers/chats" ;
55import { selectHandleIDToContact } from "../app/reducers/contacts" ;
@@ -21,19 +21,11 @@ export function ChatSearchProvider({ children }: PropsWithChildren<{}>) {
2121 const allChats = Object . values ( useSelector ( selectChats ) ) ;
2222 const contacts = useSelector ( selectHandleIDToContact ) ;
2323
24- const [ searchCriteria , internalSetSearchCriteria ] = useState < string > ( ) ;
25- const [ chatResults , setChatResults ] = useState < ChatRepresentation [ ] > ( ) ;
24+ const [ searchCriteria , setSearchCriteria ] = useState < string > ( ) ;
2625
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 ] ) ;
3729
3830 const clearSearchCriteria = useCallback ( ( ) => {
3931
0 commit comments