Skip to content
Merged
Show file tree
Hide file tree
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
84 changes: 0 additions & 84 deletions src/hooks/useLastAccessedReport.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, {useState} from 'react';
import useLastAccessedReport from '@hooks/useLastAccessedReport';
import usePermissions from '@hooks/usePermissions';
import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator';
import FreezeWrapper from '@libs/Navigation/AppNavigator/FreezeWrapper';
Expand All @@ -8,6 +7,7 @@ import getCurrentUrl from '@libs/Navigation/currentUrl';
import shouldOpenOnAdminRoom from '@libs/Navigation/helpers/shouldOpenOnAdminRoom';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {AuthScreensParamList, ReportsSplitNavigatorParamList} from '@libs/Navigation/types';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import type NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';
Expand All @@ -24,7 +24,6 @@ const Split = createSplitNavigator<ReportsSplitNavigatorParamList>();
function ReportsSplitNavigator({route}: PlatformStackScreenProps<AuthScreensParamList, typeof NAVIGATORS.REPORTS_SPLIT_NAVIGATOR>) {
const {isBetaEnabled} = usePermissions();
const splitNavigatorScreenOptions = useSplitNavigatorScreenOptions();
const {lastAccessReportID} = useLastAccessedReport(!isBetaEnabled(CONST.BETAS.DEFAULT_ROOMS), shouldOpenOnAdminRoom());

const [initialReportID] = useState(() => {
const currentURL = getCurrentUrl();
Expand All @@ -33,8 +32,9 @@ function ReportsSplitNavigator({route}: PlatformStackScreenProps<AuthScreensPara
return reportIdFromPath;
}

const initialReport = ReportUtils.findLastAccessedReport(!isBetaEnabled(CONST.BETAS.DEFAULT_ROOMS), shouldOpenOnAdminRoom());
// eslint-disable-next-line rulesdir/no-default-id-values
return lastAccessReportID ?? '';
return initialReport?.reportID ?? '';
});

return (
Expand Down
35 changes: 10 additions & 25 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@
const parsedReportActionMessageCache: Record<string, string> = {};

let conciergeReportID: OnyxEntry<string>;
Onyx.connect({

Check warning on line 896 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.CONCIERGE_REPORT_ID,
callback: (value) => {
conciergeReportID = value;
Expand All @@ -901,7 +901,7 @@
});

const defaultAvatarBuildingIconTestID = 'SvgDefaultAvatarBuilding Icon';
Onyx.connect({

Check warning on line 904 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
// When signed out, val is undefined
Expand All @@ -919,7 +919,7 @@
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
let allPersonalDetailLogins: string[];
let currentUserPersonalDetails: OnyxEntry<PersonalDetails>;
Onyx.connect({

Check warning on line 922 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
if (currentUserAccountID) {
Expand All @@ -931,14 +931,14 @@
});

let allReportsDraft: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 934 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_DRAFT,
waitForCollectionCallback: true,
callback: (value) => (allReportsDraft = value),
});

let allPolicies: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 941 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand All @@ -946,7 +946,7 @@

let allReports: OnyxCollection<Report>;
let reportsByPolicyID: ReportByPolicyMap;
Onyx.connect({

Check warning on line 949 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -987,14 +987,14 @@
});

let allBetas: OnyxEntry<Beta[]>;
Onyx.connect({

Check warning on line 990 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.BETAS,
callback: (value) => (allBetas = value),
});

let allTransactions: OnyxCollection<Transaction> = {};
let reportsTransactions: Record<string, Transaction[]> = {};
Onyx.connect({

Check warning on line 997 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -1020,7 +1020,7 @@
});

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 1023 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand All @@ -1033,7 +1033,7 @@

let allReportMetadata: OnyxCollection<ReportMetadata>;
const allReportMetadataKeyValue: Record<string, ReportMetadata> = {};
Onyx.connect({

Check warning on line 1036 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_METADATA,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -1967,25 +1967,16 @@
return lodashMaxBy(filteredReports, (a) => [reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${a?.reportID}`]?.lastVisitTime ?? '', a?.lastReadTime ?? '']);
}

function findLastAccessedReport(
reports: OnyxCollection<OnyxEntry<Report>>,
policies: OnyxCollection<OnyxEntry<Policy>>,
reportMetadata: OnyxCollection<ReportMetadata>,
reportNameValuePairs: OnyxCollection<ReportNameValuePairs>,
ignoreDomainRooms: boolean,
openOnAdminRoom = false,
policyID?: string,
excludeReportID?: string,
): OnyxEntry<Report> {
function findLastAccessedReport(ignoreDomainRooms: boolean, openOnAdminRoom = false, policyID?: string, excludeReportID?: string): 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.
// 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.

const policyMemberAccountIDs = getPolicyEmployeeListByIdWithoutCurrentUser(policies, policyID, currentUserAccountID);
const policyMemberAccountIDs = getPolicyEmployeeListByIdWithoutCurrentUser(allPolicies, policyID, currentUserAccountID);

let reportsValues = Object.values(reports ?? {});
let reportsValues = Object.values(allReports ?? {});

if (!!policyID || policyMemberAccountIDs.length > 0) {
reportsValues = filterReportsByPolicyIDAndMemberAccountIDs(reportsValues, policyMemberAccountIDs, policyID);
Expand Down Expand Up @@ -2025,29 +2016,24 @@
// and it prompts the user to use the Concierge chat instead.
reportsValues =
reportsValues.filter((report) => {
return !isSystemChat(report) && !isArchivedReport(reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`]);
// This will get removed as part of https://github.com/Expensify/App/issues/59961
// eslint-disable-next-line deprecation/deprecation
const reportNameValuePairs = getReportNameValuePairs(report?.reportID);

return !isSystemChat(report) && !isArchivedReport(reportNameValuePairs);
}) ?? [];

// At least two reports remain: self DM and Concierge chat.
// Return the most recently visited report. Get the last read report from the report metadata.
// If allReportMetadata is empty we'll return most recent report owned by user
if (isEmptyObject(reportMetadata)) {
if (isEmptyObject(allReportMetadata)) {
const ownedReports = reportsValues.filter((report) => report?.ownerAccountID === currentUserAccountID);
if (ownedReports.length > 0) {
return lodashMaxBy(ownedReports, (a) => a?.lastReadTime ?? '');
}
return lodashMaxBy(reportsValues, (a) => a?.lastReadTime ?? '');
}
return getMostRecentlyVisitedReport(reportsValues, reportMetadata);
}

/**
* This function is used to retrieve the last accessed report in background
* It's used in the case we need to get the last accessed report in a listener like openReportFromDeepLink
* Please use useLastAccessedReport hook instead if we need to get the last accessed report from the UI
*/
function findLastAccessedReportWithoutView(ignoreDomainRooms: boolean, openOnAdminRoom = false, policyID?: string, excludeReportID?: string): OnyxEntry<Report> {
return findLastAccessedReport(allReports, allPolicies, allReportMetadata, allReportNameValuePair, ignoreDomainRooms, openOnAdminRoom, policyID, excludeReportID);
return getMostRecentlyVisitedReport(reportsValues, allReportMetadata);
}

/**
Expand Down Expand Up @@ -11669,7 +11655,6 @@
isWorkspaceTaskReport,
isWorkspaceThread,
getReportStatusTranslation,
findLastAccessedReportWithoutView,
};

export type {
Expand Down
21 changes: 12 additions & 9 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ import {
buildOptimisticSelfDMReport,
buildOptimisticUnreportedTransactionAction,
canUserPerformWriteAction as canUserPerformWriteActionReportUtils,
findLastAccessedReportWithoutView,
findLastAccessedReport,
findSelfDMReportID,
formatReportLastMessageText,
generateReportID,
Expand Down Expand Up @@ -3412,7 +3412,7 @@ function openReportFromDeepLink(
// Navigate to the report after sign-in/sign-up.
InteractionManager.runAfterInteractions(() => {
waitForUserSignIn().then(() => {
const connection = Onyx.connectWithoutView({
const connection = Onyx.connect({
key: ONYXKEYS.NVP_ONBOARDING,
callback: (val) => {
if (!val && !isAnonymousUser()) {
Expand Down Expand Up @@ -3458,7 +3458,7 @@ function openReportFromDeepLink(
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
// If the report does not exist, navigate to the last accessed report or Concierge chat
if (reportID && !report) {
const lastAccessedReportID = findLastAccessedReportWithoutView(false, shouldOpenOnAdminRoom(), undefined, reportID)?.reportID;
const lastAccessedReportID = findLastAccessedReport(false, shouldOpenOnAdminRoom(), undefined, reportID)?.reportID;
if (lastAccessedReportID) {
const lastAccessedReportRoute = ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID);
Navigation.navigate(lastAccessedReportRoute);
Expand Down Expand Up @@ -3504,7 +3504,9 @@ function getCurrentUserEmail(): string | undefined {
return currentUserEmail;
}

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

if (lastAccessedReportID) {
const lastAccessedReportRoute = ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID);
Navigation.goBack(lastAccessedReportRoute);
Expand All @@ -3520,7 +3522,8 @@ function navigateToMostRecentReport(currentReport: OnyxEntry<Report>, lastAccess
}
}

function getMostRecentReportID(lastAccessedReportID: string | undefined) {
function getMostRecentReportID(currentReport: OnyxEntry<Report>) {
const lastAccessedReportID = findLastAccessedReport(false, false, undefined, currentReport?.reportID)?.reportID;
return lastAccessedReportID ?? conciergeReportID;
}

Expand All @@ -3537,7 +3540,7 @@ function joinRoom(report: OnyxEntry<Report>) {
);
}

function leaveGroupChat(reportID: string, lastAccessedReportID: string | undefined) {
function leaveGroupChat(reportID: string) {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
if (!report) {
Log.warn('Attempting to leave Group Chat that does not existing locally');
Expand Down Expand Up @@ -3589,12 +3592,12 @@ function leaveGroupChat(reportID: string, lastAccessedReportID: string | undefin
},
];

navigateToMostRecentReport(report, lastAccessedReportID);
navigateToMostRecentReport(report);
API.write(WRITE_COMMANDS.LEAVE_GROUP_CHAT, {reportID}, {optimisticData, successData, failureData});
}

/** Leave a report by setting the state to submitted and closed */
function leaveRoom(reportID: string, lastAccessedReportID: string | undefined, isWorkspaceMemberLeavingWorkspaceRoom = false) {
function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = false) {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];

if (!report) {
Expand Down Expand Up @@ -3701,7 +3704,7 @@ function leaveRoom(reportID: string, lastAccessedReportID: string | undefined, i
return;
}
// In other cases, the report is deleted and we should move the user to another report.
navigateToMostRecentReport(report, lastAccessedReportID);
navigateToMostRecentReport(report);
}

function buildInviteToRoomOnyxData(reportID: string, inviteeEmailsToAccountIDs: InvitedEmailsToAccountIDs, formatPhoneNumber: LocaleContextProps['formatPhoneNumber']) {
Expand Down
8 changes: 4 additions & 4 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ function getParentReport(report: OnyxEntry<OnyxTypes.Report>): OnyxEntry<OnyxTyp
* @param report - The task report being deleted
* @returns The URL to navigate to
*/
function getNavigationUrlOnTaskDelete(report: OnyxEntry<OnyxTypes.Report>, lastAccessedReportID: string | undefined): string | undefined {
function getNavigationUrlOnTaskDelete(report: OnyxEntry<OnyxTypes.Report>): string | undefined {
if (!report) {
return undefined;
}
Expand All @@ -1062,7 +1062,7 @@ function getNavigationUrlOnTaskDelete(report: OnyxEntry<OnyxTypes.Report>, lastA
}

// If no parent report, try to navigate to most recent report
const mostRecentReportID = getMostRecentReportID(lastAccessedReportID);
const mostRecentReportID = getMostRecentReportID(report);
if (mostRecentReportID) {
return ROUTES.REPORT_WITH_ID.getRoute(mostRecentReportID);
}
Expand All @@ -1073,7 +1073,7 @@ function getNavigationUrlOnTaskDelete(report: OnyxEntry<OnyxTypes.Report>, lastA
/**
* Cancels a task by setting the report state to SUBMITTED and status to CLOSED
*/
function deleteTask(report: OnyxEntry<OnyxTypes.Report>, lastAccessedReportID: string | undefined) {
function deleteTask(report: OnyxEntry<OnyxTypes.Report>) {
if (!report) {
return;
}
Expand Down Expand Up @@ -1212,7 +1212,7 @@ function deleteTask(report: OnyxEntry<OnyxTypes.Report>, lastAccessedReportID: s
API.write(WRITE_COMMANDS.CANCEL_TASK, parameters, {optimisticData, successData, failureData});
notifyNewAction(report.reportID, currentUserAccountID);

const urlToNavigateBack = getNavigationUrlOnTaskDelete(report, lastAccessedReportID);
const urlToNavigateBack = getNavigationUrlOnTaskDelete(report);
if (urlToNavigateBack) {
Navigation.goBack();
return urlToNavigateBack;
Expand Down
12 changes: 6 additions & 6 deletions src/libs/navigateAfterOnboarding.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type {OnyxEntry} from 'react-native-onyx';
import ROUTES from '@src/ROUTES';
import type {Report} from '@src/types/onyx';
import {setDisableDismissOnEscape} from './actions/Modal';
import shouldOpenOnAdminRoom from './Navigation/helpers/shouldOpenOnAdminRoom';
import Navigation from './Navigation/Navigation';
import {isConciergeChatReport} from './ReportUtils';
import {findLastAccessedReport, isConciergeChatReport} from './ReportUtils';

const navigateAfterOnboarding = (
isSmallScreenWidth: boolean,
lastAccessedReport: OnyxEntry<Report>,
canUseDefaultRooms: boolean | undefined,
onboardingPolicyID?: string,
onboardingAdminsChatReportID?: string,
shouldPreventOpenAdminRoom = false,
Expand All @@ -25,6 +24,7 @@ const navigateAfterOnboarding = (
reportID = onboardingAdminsChatReportID;
}
} else {
const lastAccessedReport = findLastAccessedReport(!canUseDefaultRooms, shouldOpenOnAdminRoom() && !shouldPreventOpenAdminRoom);
const lastAccessedReportID = lastAccessedReport?.reportID;
// we don't want to navigate to newly created workspaces after onboarding is completed.
if (lastAccessedReportID && lastAccessedReport.policyID !== onboardingPolicyID && !isConciergeChatReport(lastAccessedReport)) {
Expand Down Expand Up @@ -52,13 +52,13 @@ const navigateAfterOnboarding = (

const navigateAfterOnboardingWithMicrotaskQueue = (
isSmallScreenWidth: boolean,
lastAccessedReport: OnyxEntry<Report>,
canUseDefaultRooms: boolean | undefined,
onboardingPolicyID?: string,
onboardingAdminsChatReportID?: string,
shouldPreventOpenAdminRoom = false,
) => {
Navigation.setNavigationActionToMicrotaskQueue(() => {
navigateAfterOnboarding(isSmallScreenWidth, lastAccessedReport, onboardingPolicyID, onboardingAdminsChatReportID, shouldPreventOpenAdminRoom);
navigateAfterOnboarding(isSmallScreenWidth, canUseDefaultRooms, onboardingPolicyID, onboardingAdminsChatReportID, shouldPreventOpenAdminRoom);
});
};

Expand Down
Loading
Loading