Skip to content

Commit

Permalink
fix: leaving from multilevel threads navigates to wrong thread
Browse files Browse the repository at this point in the history
  • Loading branch information
daledah committed Jul 25, 2024
1 parent ab4b8ae commit b3a2daf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,14 +1207,15 @@ function hasExpensifyGuidesEmails(accountIDs: number[]): boolean {
return accountIDs.some((accountID) => Str.extractEmailDomain(allPersonalDetails?.[accountID]?.login ?? '') === CONST.EMAIL.GUIDES_DOMAIN);
}

function getMostRecentlyVisitedReport(reports: Array<OnyxEntry<Report>>, reportMetadata: OnyxCollection<ReportMetadata>): OnyxEntry<Report> {
const filteredReports = reports.filter(
(report) => !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime),
);
function getMostRecentlyVisitedReport(reports: Array<OnyxEntry<Report>>, reportMetadata: OnyxCollection<ReportMetadata>, shouldKeepLeavedThread = true): OnyxEntry<Report> {
const filteredReports = reports.filter((report) => {
const shouldKeep = shouldKeepLeavedThread || !isChatThread(report) || (isChatThread(report) && report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN);
return shouldKeep && !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime);
});
return lodashMaxBy(filteredReports, (a) => new Date(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${a?.reportID}`]?.lastVisitTime ?? a?.lastReadTime ?? '').valueOf());
}

function findLastAccessedReport(ignoreDomainRooms: boolean, openOnAdminRoom = false, policyID?: string, excludeReportID?: string): OnyxEntry<Report> {
function findLastAccessedReport(ignoreDomainRooms: boolean, openOnAdminRoom = false, policyID?: string, excludeReportID?: string, shouldKeepLeavedThread = true): OnyxEntry<Report> {
// If it's the user's first time using New Expensify, then they could either have:
// - just a Concierge report, if so we'll return that
// - their Concierge report, and a separate report that must have deeplinked them to the app before they created their account.
Expand Down Expand Up @@ -1286,7 +1287,7 @@ function findLastAccessedReport(ignoreDomainRooms: boolean, openOnAdminRoom = fa
}

// We are getting the last read report from the metadata of the report.
const lastRead = getMostRecentlyVisitedReport(reportsValues, allReportMetadata);
const lastRead = getMostRecentlyVisitedReport(reportsValues, allReportMetadata, shouldKeepLeavedThread);

return adminReport ?? lastRead;
}
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2654,8 +2654,8 @@ function getCurrentUserAccountID(): number {
return currentUserAccountID;
}

function navigateToMostRecentReport(currentReport: OnyxEntry<Report>) {
const lastAccessedReportID = ReportUtils.findLastAccessedReport(false, false, undefined, currentReport?.reportID)?.reportID;
function navigateToMostRecentReport(currentReport: OnyxEntry<Report>, shouldKeepLeavedThread = true) {
const lastAccessedReportID = ReportUtils.findLastAccessedReport(false, false, undefined, currentReport?.reportID, shouldKeepLeavedThread)?.reportID;

if (lastAccessedReportID) {
const lastAccessedReportRoute = ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID ?? '-1');
Expand Down Expand Up @@ -2793,7 +2793,7 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal
return;
}
// In other cases, the report is deleted and we should move the user to another report.
navigateToMostRecentReport(report);
navigateToMostRecentReport(report, false);
}

/** Invites people to a room */
Expand Down

0 comments on commit b3a2daf

Please sign in to comment.