[Perf] Forward Picker: Avoid redundant calculations

This commit is contained in:
Alexander Zinchuk 2021-12-14 22:40:52 +04:00
parent 0aa815066e
commit 521b7e6505

View File

@ -10,6 +10,7 @@ import { getCanPostInChat, getChatTitle, sortChatIds } from '../../modules/helpe
import searchWords from '../../util/searchWords'; import searchWords from '../../util/searchWords';
import { pick, unique } from '../../util/iteratees'; import { pick, unique } from '../../util/iteratees';
import useLang from '../../hooks/useLang'; import useLang from '../../hooks/useLang';
import useCurrentOrPrev from '../../hooks/useCurrentOrPrev';
import ChatOrUserPicker from '../common/ChatOrUserPicker'; import ChatOrUserPicker from '../common/ChatOrUserPicker';
@ -45,6 +46,10 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
const filterRef = useRef<HTMLInputElement>(null); const filterRef = useRef<HTMLInputElement>(null);
const chatIds = useMemo(() => { const chatIds = useMemo(() => {
if (!isOpen) {
return undefined;
}
const listIds = [...(activeListIds || []), ...(archivedListIds || [])]; const listIds = [...(activeListIds || []), ...(archivedListIds || [])];
let priorityIds = pinnedIds || []; let priorityIds = pinnedIds || [];
@ -68,17 +73,19 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter);
}), chatsById, undefined, priorityIds); }), chatsById, undefined, priorityIds);
}, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds]); }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, isOpen, lang, pinnedIds]);
const handleSelectUser = useCallback((userId: string) => { const handleSelectUser = useCallback((userId: string) => {
setForwardChatId({ id: userId }); setForwardChatId({ id: userId });
}, [setForwardChatId]); }, [setForwardChatId]);
const renderingChatIds = useCurrentOrPrev(chatIds)!;
return ( return (
<ChatOrUserPicker <ChatOrUserPicker
currentUserId={currentUserId} currentUserId={currentUserId}
isOpen={isOpen} isOpen={isOpen}
chatOrUserIds={chatIds} chatOrUserIds={renderingChatIds}
filterRef={filterRef} filterRef={filterRef}
filterPlaceholder={lang('ForwardTo')} filterPlaceholder={lang('ForwardTo')}
filter={filter} filter={filter}