Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Implement filtering in New Chat page #40350

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/pages/NewChatPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import isEmpty from 'lodash/isEmpty';
import reject from 'lodash/reject';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import KeyboardAvoidingView from '@components/KeyboardAvoidingView';
Expand Down Expand Up @@ -32,7 +31,6 @@ import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Beta} from '@src/types/onyx';
import type {SelectedParticipant} from '@src/types/onyx/NewGroupChatDraft';

type NewChatPageProps = {
Expand All @@ -52,12 +50,12 @@ function useOptions({isGroupChat}: NewChatPageProps) {
shouldInitialize: didScreenTransitionEnd,
});

const options = useMemo(() => {
const defaultOptions = useMemo(() => {
const filteredOptions = OptionsListUtils.getFilteredOptions(
listOptions.reports ?? [],
listOptions.personalDetails ?? [],
(betas ?? []) as OnyxEntry<Beta[]>,
debouncedSearchTerm,
betas ?? [],
'',
selectedOptions,
isGroupChat ? excludedGroupEmails : [],
false,
Expand All @@ -71,19 +69,33 @@ function useOptions({isGroupChat}: NewChatPageProps) {
true,
undefined,
undefined,
CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
0,
undefined,
true,
);
return filteredOptions;
}, [betas, isGroupChat, listOptions.personalDetails, listOptions.reports, selectedOptions]);

const headerMessage = OptionsListUtils.getHeaderMessage(
filteredOptions.personalDetails.length + filteredOptions.recentReports.length !== 0,
!!filteredOptions.userToInvite,
debouncedSearchTerm.trim(),
selectedOptions.some((participant) => participant?.searchText?.toLowerCase?.().includes(debouncedSearchTerm.trim().toLowerCase())),
);
return {...filteredOptions, headerMessage};
}, [betas, debouncedSearchTerm, isGroupChat, listOptions.personalDetails, listOptions.reports, selectedOptions]);
const options = useMemo(() => {
const filteredOptions = OptionsListUtils.filterOptions(defaultOptions, debouncedSearchTerm, {
selectedOptions,
excludeLogins: isGroupChat ? excludedGroupEmails : [],
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
});

return filteredOptions;
}, [debouncedSearchTerm, defaultOptions, isGroupChat, selectedOptions]);

const headerMessage = useMemo(
() =>
OptionsListUtils.getHeaderMessage(
options.personalDetails.length + options.recentReports.length !== 0,
!!options.userToInvite,
debouncedSearchTerm.trim(),
selectedOptions.some((participant) => participant?.searchText?.toLowerCase?.().includes(debouncedSearchTerm.trim().toLowerCase())),
),
[debouncedSearchTerm, options.personalDetails.length, options.recentReports.length, options.userToInvite, selectedOptions],
);

useEffect(() => {
if (!debouncedSearchTerm.length) {
Expand Down Expand Up @@ -120,7 +132,16 @@ function useOptions({isGroupChat}: NewChatPageProps) {
setSelectedOptions(newSelectedOptions);
}, [newGroupDraft?.participants, listOptions.personalDetails, betas, personalData.accountID]);

return {...options, searchTerm, debouncedSearchTerm, setSearchTerm, areOptionsInitialized: areOptionsInitialized && didScreenTransitionEnd, selectedOptions, setSelectedOptions};
return {
...options,
searchTerm,
debouncedSearchTerm,
setSearchTerm,
areOptionsInitialized: areOptionsInitialized && didScreenTransitionEnd,
selectedOptions,
setSelectedOptions,
headerMessage,
};
}

function NewChatPage({isGroupChat}: NewChatPageProps) {
Expand Down
Loading