From 9bc2e6537f76807063e06741913dfc6c7d5ccd31 Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Sat, 18 May 2024 17:31:44 +0100 Subject: [PATCH 1/2] simplify navigateToMostRecentReport --- src/libs/ReportUtils.ts | 34 ++++++++++++++----- src/libs/actions/Report.ts | 57 +++++++++----------------------- src/pages/NewChatConfirmPage.tsx | 2 +- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index b8e4c448fdc2..b46537e81142 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1079,7 +1079,7 @@ function doesReportBelongToWorkspace(report: OnyxEntry, policyMemberAcco /** * Given an array of reports, return them filtered by a policyID and policyMemberAccountIDs. */ -function filterReportsByPolicyIDAndMemberAccountIDs(reports: Report[], policyMemberAccountIDs: number[] = [], policyID?: string) { +function filterReportsByPolicyIDAndMemberAccountIDs(reports: Array>, policyMemberAccountIDs: number[] = [], policyID?: string) { return reports.filter((report) => !!report && doesReportBelongToWorkspace(report, policyMemberAccountIDs, policyID)); } @@ -1165,6 +1165,7 @@ function findLastAccessedReport( reportMetadata: OnyxCollection = {}, policyID?: string, policyMemberAccountIDs: number[] = [], + excludeReportID?: string, ): OnyxEntry { // 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 @@ -1172,7 +1173,7 @@ function findLastAccessedReport( // If it's the latter, we'll use the deeplinked report over the Concierge report, // since the Concierge report would be incorrectly selected over the deep-linked report in the logic below. - let reportsValues = Object.values(reports ?? {}) as Report[]; + let reportsValues = Object.values(reports ?? {}); if (!!policyID || policyMemberAccountIDs.length > 0) { reportsValues = filterReportsByPolicyIDAndMemberAccountIDs(reportsValues, policyMemberAccountIDs, policyID); @@ -1188,13 +1189,28 @@ function findLastAccessedReport( }); } - if (ignoreDomainRooms) { - // We allow public announce rooms, admins, and announce rooms through since we bypass the default rooms beta for them. - // Check where ReportUtils.findLastAccessedReport is called in MainDrawerNavigator.js for more context. - // Domain rooms are now the only type of default room that are on the defaultRooms beta. - sortedReports = sortedReports.filter( - (report) => !isDomainRoom(report) || getPolicyType(report, policies) === CONST.POLICY.TYPE.FREE || hasExpensifyGuidesEmails(Object.keys(report?.participants ?? {}).map(Number)), - ); + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + const shouldFilter = excludeReportID || ignoreDomainRooms; + if (shouldFilter) { + sortedReports = sortedReports.filter((report) => { + if (excludeReportID && report?.reportID === excludeReportID) { + return false; + } + + // We allow public announce rooms, admins, and announce rooms through since we bypass the default rooms beta for them. + // Check where ReportUtils.findLastAccessedReport is called in MainDrawerNavigator.js for more context. + // Domain rooms are now the only type of default room that are on the defaultRooms beta. + if ( + ignoreDomainRooms && + isDomainRoom(report) && + getPolicyType(report, policies) !== CONST.POLICY.TYPE.FREE && + !hasExpensifyGuidesEmails(Object.keys(report?.participants ?? {}).map(Number)) + ) { + return false; + } + + return true; + }); } if (isFirstTimeNewExpensifyUser) { diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 91a0ce5da930..6f6f8cfe1944 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -946,6 +946,7 @@ function openReport( function navigateToAndOpenReport( userLogins: string[], shouldDismissModal = true, + actionType?: string, reportName?: string, avatarUri?: string, avatarFile?: File | CustomRNImageManipulatorResult | undefined, @@ -977,7 +978,7 @@ function navigateToAndOpenReport( Navigation.dismissModalWithReport(report); } else { Navigation.navigateWithSwitchPolicyID({route: ROUTES.HOME}); - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.reportID)); + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.reportID), actionType); } } @@ -1972,7 +1973,7 @@ function updateWriteCapabilityAndNavigate(report: Report, newValue: WriteCapabil /** * Navigates to the 1:1 report with Concierge */ -function navigateToConciergeChat(shouldDismissModal = false, checkIfCurrentPageActive = () => true) { +function navigateToConciergeChat(shouldDismissModal = false, checkIfCurrentPageActive = () => true, actionType?: string) { // If conciergeChatReportID contains a concierge report ID, we navigate to the concierge chat using the stored report ID. // Otherwise, we would find the concierge chat and navigate to it. if (!conciergeChatReportID) { @@ -1983,12 +1984,12 @@ function navigateToConciergeChat(shouldDismissModal = false, checkIfCurrentPageA if (!checkIfCurrentPageActive()) { return; } - navigateToAndOpenReport([CONST.EMAIL.CONCIERGE], shouldDismissModal); + navigateToAndOpenReport([CONST.EMAIL.CONCIERGE], shouldDismissModal, actionType); }); } else if (shouldDismissModal) { Navigation.dismissModal(conciergeChatReportID); } else { - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(conciergeChatReportID)); + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(conciergeChatReportID), actionType); } } @@ -2507,46 +2508,20 @@ function getCurrentUserAccountID(): number { } function navigateToMostRecentReport(currentReport: OnyxEntry) { - const reportID = currentReport?.reportID; - const sortedReportsByLastRead = ReportUtils.sortReportsByLastRead(Object.values(allReports ?? {}) as Report[], reportMetadata); - - // We want to filter out the current report, hidden reports and empty chats - const filteredReportsByLastRead = sortedReportsByLastRead.filter( - (sortedReport) => - sortedReport?.reportID !== reportID && - sortedReport?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && - ReportUtils.shouldReportBeInOptionList({ - report: sortedReport, - currentReportId: '', - isInFocusMode: false, - betas: [], - policies: {}, - excludeEmptyChats: true, - doesReportHaveViolations: false, - includeSelfDM: true, - }), - ); - const lastAccessedReportID = filteredReportsByLastRead.at(-1)?.reportID; + const lastAccessedReportID = ReportUtils.findLastAccessedReport(allReports, false, null, false, false, reportMetadata, undefined, [], currentReport?.reportID)?.reportID; + const isChatThread = ReportUtils.isChatThread(currentReport); + // We are using index 1 here because on index 0, we'll always have the bottomTabNavigator. + const isFirstRoute = navigationRef?.current?.getState()?.index === 1; + // If it is not a chat thread we should call Navigation.goBack to pop the current route first before navigating to last accessed report. + if (!isChatThread && !isFirstRoute) { + Navigation.goBack(); + } + if (lastAccessedReportID) { - const lastAccessedReportRoute = ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID ?? ''); - // We are using index 1 here because on index 0, we'll always have the bottomTabNavigator. - const isFirstRoute = navigationRef?.current?.getState()?.index === 1; - // If it is not a chat thread we should call Navigation.goBack to pop the current route first before navigating to last accessed report. - if (!isChatThread && !isFirstRoute) { - Navigation.goBack(); - } - Navigation.navigate(lastAccessedReportRoute, CONST.NAVIGATION.TYPE.FORCED_UP); + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID), CONST.NAVIGATION.TYPE.FORCED_UP); } else { - const participantAccountIDs = PersonalDetailsUtils.getAccountIDsByLogins([CONST.EMAIL.CONCIERGE]); - const chat = ReportUtils.getChatByParticipants([...participantAccountIDs, currentUserAccountID]); - if (chat?.reportID) { - // If it is not a chat thread we should call Navigation.goBack to pop the current route first before navigating to Concierge. - if (!isChatThread) { - Navigation.goBack(); - } - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(chat?.reportID), CONST.NAVIGATION.TYPE.FORCED_UP); - } + navigateToConciergeChat(false, () => true, CONST.NAVIGATION.TYPE.FORCED_UP); } } diff --git a/src/pages/NewChatConfirmPage.tsx b/src/pages/NewChatConfirmPage.tsx index 82f67eb40c86..0b2cdf3f5929 100644 --- a/src/pages/NewChatConfirmPage.tsx +++ b/src/pages/NewChatConfirmPage.tsx @@ -93,7 +93,7 @@ function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmP } const logins: string[] = (newGroupDraft.participants ?? []).map((participant) => participant.login); - Report.navigateToAndOpenReport(logins, true, newGroupDraft.reportName ?? '', newGroupDraft.avatarUri ?? '', fileRef.current, optimisticReportID.current, true); + Report.navigateToAndOpenReport(logins, true, undefined, newGroupDraft.reportName ?? '', newGroupDraft.avatarUri ?? '', fileRef.current, optimisticReportID.current, true); }; const navigateBack = () => { From 11cd7df7ba1f2fc23f6d0474856582a3117cd208 Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:16:55 +0100 Subject: [PATCH 2/2] ts + lint --- src/libs/actions/Report.ts | 2 +- src/pages/NewChatConfirmPage.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index b1d120962e4c..f0bb402da881 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -2551,7 +2551,7 @@ function getCurrentUserAccountID(): number { } function navigateToMostRecentReport(currentReport: OnyxEntry) { - const lastAccessedReportID = ReportUtils.findLastAccessedReport(allReports, false, null, false, false, reportMetadata, undefined, [], currentReport?.reportID)?.reportID; + const lastAccessedReportID = ReportUtils.findLastAccessedReport(allReports, false, undefined, false, false, reportMetadata, undefined, [], currentReport?.reportID)?.reportID; if (lastAccessedReportID) { const lastAccessedReportRoute = ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID ?? '-1'); diff --git a/src/pages/NewChatConfirmPage.tsx b/src/pages/NewChatConfirmPage.tsx index be810ff146d7..17e5708803cd 100644 --- a/src/pages/NewChatConfirmPage.tsx +++ b/src/pages/NewChatConfirmPage.tsx @@ -106,7 +106,7 @@ function NewChatConfirmPage({newGroupDraft, allPersonalDetails}: NewChatConfirmP const logins: string[] = (newGroupDraft.participants ?? []).map((participant) => participant.login); Report.navigateToAndOpenReport(logins, true, undefined, newGroupDraft.reportName ?? '', newGroupDraft.avatarUri ?? '', avatarFile, optimisticReportID.current, true); - }, [newGroupDraft]); + }, [newGroupDraft, avatarFile]); const stashedLocalAvatarImage = newGroupDraft?.avatarUri;