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

Group chat: Use draft only on init and on draft changes #39312

Merged
merged 1 commit into from
Apr 3, 2024
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
38 changes: 23 additions & 15 deletions src/pages/NewChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,30 @@ const excludedGroupEmails = CONST.EXPENSIFY_EMAILS.filter((value) => value !== C
function NewChatPage({betas, isGroupChat, personalDetails, reports, isSearchingForReports, dismissedReferralBanners, newGroupDraft}: NewChatPageProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();

const personalData = useCurrentUserPersonalDetails();

const getGroupParticipants = () => {
if (!newGroupDraft?.participants) {
return [];
}
const selectedParticipants = newGroupDraft.participants.filter((participant) => participant.accountID !== personalData.accountID);
const newSelectedOptions = selectedParticipants.map((participant): OptionData => {
const baseOption = OptionsListUtils.getParticipantsOption({accountID: participant.accountID, login: participant.login, reportID: ''}, personalDetails);
return {...baseOption, reportID: baseOption.reportID ?? ''};
});
return newSelectedOptions;
};

const [searchTerm, setSearchTerm] = useState('');
const [filteredRecentReports, setFilteredRecentReports] = useState<ReportUtils.OptionData[]>([]);
const [filteredPersonalDetails, setFilteredPersonalDetails] = useState<ReportUtils.OptionData[]>([]);
const [filteredUserToInvite, setFilteredUserToInvite] = useState<ReportUtils.OptionData | null>();
const [selectedOptions, setSelectedOptions] = useState<OptionData[]>([]);
const [selectedOptions, setSelectedOptions] = useState<OptionData[]>(getGroupParticipants);
const {isOffline} = useNetwork();
const {isSmallScreenWidth} = useWindowDimensions();
const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false);

const personalData = useCurrentUserPersonalDetails();

const maxParticipantsReached = selectedOptions.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const setSearchTermAndSearchInServer = useSearchTermAndSearch(setSearchTerm, maxParticipantsReached);

Expand Down Expand Up @@ -188,16 +201,6 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, isSearchingF
};

const updateOptions = useCallback(() => {
let newSelectedOptions;
if (newGroupDraft?.participants) {
const selectedParticipants = newGroupDraft.participants.filter((participant) => participant.accountID !== personalData.accountID);
newSelectedOptions = selectedParticipants.map((participant): OptionData => {
const baseOption = OptionsListUtils.getParticipantsOption({accountID: participant.accountID, login: participant.login, reportID: ''}, personalDetails);
return {...baseOption, reportID: baseOption.reportID ?? ''};
});
setSelectedOptions(newSelectedOptions);
}

const {
recentReports,
personalDetails: newChatPersonalDetails,
Expand All @@ -207,7 +210,7 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, isSearchingF
personalDetails,
betas ?? [],
searchTerm,
newSelectedOptions ?? selectedOptions,
selectedOptions,
isGroupChat ? excludedGroupEmails : [],
false,
true,
Expand All @@ -225,7 +228,7 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, isSearchingF
setFilteredUserToInvite(userToInvite);
// props.betas is not added as dependency since it doesn't change during the component lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reports, personalDetails, searchTerm, newGroupDraft]);
}, [reports, personalDetails, searchTerm]);

useEffect(() => {
const interactionTask = doInteractionTask(() => {
Expand All @@ -241,6 +244,11 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, isSearchingF
};
}, []);

useEffect(() => {
setSelectedOptions(getGroupParticipants());
// eslint-disable-next-line react-hooks/exhaustive-deps -- Overwrite participants only if the draft changes
}, [newGroupDraft?.participants]);
Comment on lines +247 to +250
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may raise the question on why we don't always use the draft participants i.e. selectedOptions = newGroupDraft.participants. The reason is that the draft participants are set only after the user presses the next button.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. Makes me wonder why we don't start saving them when you first tap "Add to group". Feels like we should have designed it that way (fixing that seems outside the scope of your current changes). 🤔


useEffect(() => {
if (!didScreenTransitionEnd) {
return;
Expand Down
Loading