Skip to content

Commit

Permalink
Merge pull request Expensify#39312 from s77rt/group-chat-draft-on-demand
Browse files Browse the repository at this point in the history
Group chat: Use draft only on init and on draft changes
  • Loading branch information
marcaaron authored Apr 3, 2024
2 parents 5c6c0f9 + d4da2d7 commit 6abecc8
Showing 1 changed file with 23 additions and 15 deletions.
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]);

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

0 comments on commit 6abecc8

Please sign in to comment.