Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions src/components/SelectionList/components/ListCheckbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function ListCheckbox<TItem extends ListItem>({
style,
containerStyle,
shouldStopMouseDownPropagation,
shouldUseOptimisticSelection,
testID,
}: ListSelectionButtonProps<TItem>) {
return (
Expand All @@ -32,6 +33,7 @@ function ListCheckbox<TItem extends ListItem>({
style={style}
containerStyle={containerStyle}
shouldStopMouseDownPropagation={shouldStopMouseDownPropagation}
shouldUseOptimisticSelection={shouldUseOptimisticSelection}
testID={testID}
/>
);
Expand Down
27 changes: 24 additions & 3 deletions src/components/SelectionList/components/ListSelectionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CONST from '@src/CONST';

import type {StyleProp, ViewStyle} from 'react-native';

import React from 'react';
import React, {useState} from 'react';

type ListSelectionButtonProps<TItem extends ListItem> = {
/** The item to render the selection button for */
Expand All @@ -29,6 +29,9 @@ type ListSelectionButtonProps<TItem extends ListItem> = {
/** Whether to stop mouse down event propagation */
shouldStopMouseDownPropagation?: boolean;

/** Paint the checkmark on press before the parent's selection update lands. Opt-in for pages that defer that update. */
shouldUseOptimisticSelection?: boolean;

/** Test ID */
testID?: string;

Expand All @@ -45,18 +48,36 @@ function ListSelectionButton<TItem extends ListItem>({
style,
containerStyle,
shouldStopMouseDownPropagation = true,
shouldUseOptimisticSelection = false,
testID,
tabIndex,
}: ListSelectionButtonProps<TItem> & {role: typeof CONST.ROLE.CHECKBOX | typeof CONST.ROLE.RADIO}) {
const label = accessibilityLabel ?? item.text ?? '';

const isCheckedProp = item.isSelected ?? false;
// Optimistic checkmark, only when opted in. Drop it once item.isSelected catches up, and reset on keyForList change
// so a recycled FlashList row doesn't keep the previous row's checkmark. Off by default, so other lists are unchanged.
const [prevItem, setPrevItem] = useState({key: item.keyForList, checked: isCheckedProp});
const [optimisticChecked, setOptimisticChecked] = useState<boolean | null>(null);
if (shouldUseOptimisticSelection && (prevItem.key !== item.keyForList || prevItem.checked !== isCheckedProp)) {
setPrevItem({key: item.keyForList, checked: isCheckedProp});
setOptimisticChecked(null);
}
const isChecked = shouldUseOptimisticSelection ? (optimisticChecked ?? isCheckedProp) : isCheckedProp;

return (
<SelectionButton
shouldSelectOnPressEnter
role={role}
accessibilityLabel={label}
isChecked={item.isSelected ?? false}
onPress={() => onSelectRow(item)}
isChecked={isChecked}
onPress={() => {
// A radio only ever selects (paint checked, never flip off); a checkbox toggles.
if (shouldUseOptimisticSelection) {
setOptimisticChecked(role === CONST.ROLE.RADIO ? true : !isChecked);
}
onSelectRow(item);
}}
disabled={disabled}
style={style}
containerStyle={containerStyle}
Expand Down
98 changes: 72 additions & 26 deletions src/pages/NewChatPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ import type {Ref} from 'react';

import {useFocusEffect} from '@react-navigation/native';
import {guidedSetupAndTourStatusSelector} from '@selectors/Onboarding';
import React, {useEffect, useImperativeHandle, useRef, useState} from 'react';
import reject from 'lodash/reject';
import React, {startTransition, useEffect, useImperativeHandle, useRef, useState} from 'react';
import {Keyboard} from 'react-native';

import useGroupChatDraftParticipantSync from './useGroupChatDraftParticipantSync';

const excludedGroupEmails = new Set<string>(CONST.EXPENSIFY_EMAILS.filter((value) => value !== CONST.EMAIL.CONCIERGE));

function isSameSelectedOption(selectedOption: OptionData, option: ListItem & Partial<OptionData>): boolean {
if (selectedOption.login || option.login) {
return selectedOption.login === option.login;
}
if (selectedOption.accountID || option.accountID) {
return selectedOption.accountID === option.accountID;
}
return !!selectedOption.reportID && selectedOption.reportID === option.reportID;
}

type NewChatPageRef = {
focus?: () => void;
};
Expand Down Expand Up @@ -108,26 +119,31 @@ function NewChatPage({ref}: NewChatPageProps) {
setGroupDraft({participants});
};

const {searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, selectedOptions, setSelectedOptions, toggleSelection, areOptionsInitialized, onListEndReached} =
useSearchSelector({
selectionMode: CONST.SEARCH_SELECTOR.SELECTION_MODE_MULTI,
searchContext: CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_GENERAL,
includeUserToInvite: true,
includeSelfDM: true,
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
enablePhoneContacts: true,
// Keep selected options in place (marked with isSelected) so they stay put when toggled instead of jumping to a separate section.
shouldKeepSelectedInAvailableOptions: true,
shouldInitialize: didScreenTransitionEnd,
onSelectionChange: updateGroupDraft,
getValidOptionsConfig: {
includeP2P: true,
shouldAlwaysIncludeDM: true,
},
});
const {searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, selectedOptions, setSelectedOptions, areOptionsInitialized, onListEndReached} = useSearchSelector({
selectionMode: CONST.SEARCH_SELECTOR.SELECTION_MODE_MULTI,
searchContext: CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_GENERAL,
includeUserToInvite: true,
includeSelfDM: true,
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
enablePhoneContacts: true,
// Keep selected options in place (marked with isSelected) so they stay put when toggled instead of jumping to a separate section.
shouldKeepSelectedInAvailableOptions: true,
shouldInitialize: didScreenTransitionEnd,
onSelectionChange: updateGroupDraft,
getValidOptionsConfig: {
includeP2P: true,
shouldAlwaysIncludeDM: true,
},
});

useGroupChatDraftParticipantSync(areOptionsInitialized, allPersonalDetails, loginList, currentUserEmail, currentUserAccountID, translate, selectedOptions, setSelectedOptions);

// Keep the latest selection in a ref so fast toggles build on it while the deferred state update catches up.
const latestSelectedOptionsRef = useRef(selectedOptions);
useEffect(() => {
latestSelectedOptionsRef.current = selectedOptions;
}, [selectedOptions]);

useFocusEffect(() => {
focusTimeoutRef.current = setTimeout(() => {
setDidScreenTransitionEnd(true);
Expand Down Expand Up @@ -202,14 +218,33 @@ function NewChatPage({ref}: NewChatPageProps) {
* Removes a selected option from list if already selected. If not already selected add this option to the list.
*/
const toggleOption = (option: ListItem & Partial<OptionData>) => {
const currentSelectedOptions = latestSelectedOptionsRef.current;
const isOptionInList = currentSelectedOptions.some((selectedOption) => isSameSelectedOption(selectedOption, option));
const shouldRemoveOption = !!option.isSelected && isOptionInList;

let newSelectedOptions: OptionData[];

if (shouldRemoveOption) {
newSelectedOptions = reject(currentSelectedOptions, (selectedOption) => isSameSelectedOption(selectedOption, option));
} else if (isOptionInList) {
newSelectedOptions = currentSelectedOptions;
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
newSelectedOptions = [...currentSelectedOptions, {...option, isSelected: true, reportID: option.reportID, keyForList: `${option.keyForList ?? option.reportID}`} as OptionData];
}

latestSelectedOptionsRef.current = newSelectedOptions;
Comment thread
David-2002065 marked this conversation as resolved.
Resolved

selectionListRef.current?.clearInputAfterSelect();
if (!canUseTouchScreen()) {
selectionListRef.current?.focusTextInput();
}

// The row item is a fully-built option coming from the search selector, so it is safe to treat it as an OptionData.
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
toggleSelection(option as OptionData);
// Defer the heavy list re-render so the tapped checkbox paints first.
startTransition(() => {
setSelectedOptions(newSelectedOptions);
updateGroupDraft(newSelectedOptions);
});
};

/**
Expand All @@ -218,7 +253,13 @@ function NewChatPage({ref}: NewChatPageProps) {
* or navigates to the existing chat if one with those participants already exists.
*/
const selectOption = (option?: OptionWithKey) => {
const latestSelectedOptions = latestSelectedOptionsRef.current;

if (option?.isSelfDM) {
// Keep the self DM inert while a group selection is pending.
if (latestSelectedOptions.length > 0) {
return;
}
if (!option.reportID) {
Navigation.dismissModal();
return;
Expand All @@ -227,7 +268,7 @@ function NewChatPage({ref}: NewChatPageProps) {
return;
}

if (selectedOptions.length && option) {
if (latestSelectedOptions.length && option) {
Comment thread
David-2002065 marked this conversation as resolved.
Resolved
// Prevent excluded emails from being added to groups
if (option?.login && excludedGroupEmails.has(option.login)) {
return;
Expand All @@ -249,8 +290,8 @@ function NewChatPage({ref}: NewChatPageProps) {

if (option?.login) {
login = option.login;
} else if (selectedOptions.length === 1) {
login = selectedOptions.at(0)?.login ?? '';
} else if (latestSelectedOptions.length === 1) {
login = latestSelectedOptions.at(0)?.login ?? '';
Comment thread
David-2002065 marked this conversation as resolved.
Resolved
}
if (!login) {
Log.warn('Tried to create chat with empty login');
Expand Down Expand Up @@ -284,6 +325,7 @@ function NewChatPage({ref}: NewChatPageProps) {
disabled={!!item.isDisabled}
accessibilityLabel={item.text ? translate('selectionList.userSelected', item.text) : ''}
style={styles.ml5}
shouldUseOptimisticSelection
/>
);
}
Expand All @@ -308,7 +350,11 @@ function NewChatPage({ref}: NewChatPageProps) {
};

const createGroup = () => {
const participants = buildDraftParticipants(selectedOptions);
const latestSelectedOptions = latestSelectedOptionsRef.current;
if (latestSelectedOptions.length === 0) {
return;
}
const participants = buildDraftParticipants(latestSelectedOptions);
if (!participants.length) {
return;
}
Expand Down Expand Up @@ -373,7 +419,7 @@ function NewChatPage({ref}: NewChatPageProps) {
shouldUpdateFocusedIndex
shouldSingleExecuteRowSelect
confirmButtonOptions={{
onConfirm: (e, option) => (selectedOptions.length > 0 ? createGroup() : selectOption(option)),
onConfirm: (e, option) => (latestSelectedOptionsRef.current.length > 0 ? createGroup() : selectOption(option)),
}}
rightHandSideComponent={itemRightSideComponent}
footerContent={footerContent}
Expand Down
Loading
Loading