Left Search: Filter local chats by name (#3977)

This commit is contained in:
Alexander Zinchuk 2023-12-04 14:37:52 +01:00
parent c0e5b94fcb
commit 1ca211978c

View File

@ -8,6 +8,7 @@ import type { ApiChat, ApiMessage } from '../../../api/types';
import { LoadMoreDirection } from '../../../types'; import { LoadMoreDirection } from '../../../types';
import { import {
filterChatsByName,
filterUsersByName, filterUsersByName,
sortChatIds, sortChatIds,
} from '../../../global/helpers'; } from '../../../global/helpers';
@ -40,7 +41,7 @@ export type OwnProps = {
type StateProps = { type StateProps = {
currentUserId?: string; currentUserId?: string;
localContactIds?: string[]; contactIds?: string[];
accountChatIds?: string[]; accountChatIds?: string[];
accountUserIds?: string[]; accountUserIds?: string[];
globalChatIds?: string[]; globalChatIds?: string[];
@ -61,7 +62,7 @@ const ChatResults: FC<OwnProps & StateProps> = ({
searchDate, searchDate,
dateSearchQuery, dateSearchQuery,
currentUserId, currentUserId,
localContactIds, contactIds,
accountChatIds, accountChatIds,
accountUserIds, accountUserIds,
globalChatIds, globalChatIds,
@ -123,22 +124,30 @@ const ChatResults: FC<OwnProps & StateProps> = ({
const contactIdsWithMe = [ const contactIdsWithMe = [
...(currentUserId ? [currentUserId] : []), ...(currentUserId ? [currentUserId] : []),
...(localContactIds || []), ...(contactIds || []),
]; ];
// No need for expensive global updates on users, so we avoid them // No need for expensive global updates on users, so we avoid them
const usersById = getGlobal().users.byId; const usersById = getGlobal().users.byId;
const foundLocalContactIds = filterUsersByName( const localContactIds = filterUsersByName(
contactIdsWithMe, usersById, searchQuery, currentUserId, lang('SavedMessages'), contactIdsWithMe, usersById, searchQuery, currentUserId, lang('SavedMessages'),
); );
const localChatIds = filterChatsByName(lang, Object.keys(chatsById), chatsById, searchQuery, currentUserId);
const localPeerIds = unique([
...localContactIds,
...localChatIds,
]);
const accountPeerIds = unique([
...(accountChatIds ?? []),
...(accountUserIds ?? []),
].filter((accountPeerId) => !localPeerIds.includes(accountPeerId)));
return [ return [
...sortChatIds(unique([ ...sortChatIds(localPeerIds, chatsById, undefined, currentUserId ? [currentUserId] : undefined),
...(foundLocalContactIds || []), ...sortChatIds(accountPeerIds, chatsById),
...(accountChatIds || []),
...(accountUserIds || []),
]), chatsById, undefined, currentUserId ? [currentUserId] : undefined),
]; ];
}, [searchQuery, currentUserId, localContactIds, lang, accountChatIds, accountUserIds, chatsById]); }, [searchQuery, currentUserId, contactIds, lang, accountChatIds, accountUserIds, chatsById]);
useHorizontalScroll(chatSelectionRef, !localResults.length, true); useHorizontalScroll(chatSelectionRef, !localResults.length, true);
@ -302,12 +311,12 @@ export default memo(withGlobal<OwnProps>(
(global): StateProps => { (global): StateProps => {
const { byId: chatsById } = global.chats; const { byId: chatsById } = global.chats;
const { userIds: localContactIds } = global.contactList || {}; const { userIds: contactIds } = global.contactList || {};
const { const {
currentUserId, messages, currentUserId, messages,
} = global; } = global;
if (!localContactIds) { if (!contactIds) {
return { return {
chatsById, chatsById,
}; };
@ -323,7 +332,7 @@ export default memo(withGlobal<OwnProps>(
return { return {
currentUserId, currentUserId,
localContactIds, contactIds,
accountChatIds, accountChatIds,
accountUserIds, accountUserIds,
globalChatIds, globalChatIds,