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

fix last accessed report for new user #49324

Merged
Merged
Changes from 4 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
74 changes: 32 additions & 42 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ type ParsingDetails = {
policyID?: string;
};

type Thread = Report & {parentReportID: string; parentReportActionID: string};

let currentUserEmail: string | undefined;
let currentUserPrivateDomain: string | undefined;
let currentUserAccountID: number | undefined;
Expand Down Expand Up @@ -625,14 +627,6 @@ Onyx.connect({
},
});

let isFirstTimeNewExpensifyUser = false;
Onyx.connect({
key: ONYXKEYS.NVP_IS_FIRST_TIME_NEW_EXPENSIFY_USER,
callback: (value) => {
isFirstTimeNewExpensifyUser = value ?? false;
},
});

let onboarding: OnyxEntry<Onboarding | []>;
Onyx.connect({
key: ONYXKEYS.NVP_ONBOARDING,
Expand Down Expand Up @@ -1112,14 +1106,14 @@ function isWorkspaceTaskReport(report: OnyxEntry<Report>): boolean {
/**
* Returns true if report has a parent
*/
function isThread(report: OnyxInputOrEntry<Report>): boolean {
function isThread(report: OnyxInputOrEntry<Report>): report is Thread {
return !!(report?.parentReportID && report?.parentReportActionID);
}

/**
* Returns true if report is of type chat and has a parent and is therefore a Thread.
*/
function isChatThread(report: OnyxInputOrEntry<Report>): boolean {
function isChatThread(report: OnyxInputOrEntry<Report>): report is Thread {
return isThread(report) && report?.type === CONST.REPORT.TYPE.CHAT;
}

Expand Down Expand Up @@ -1336,26 +1330,13 @@ function findLastAccessedReport(ignoreDomainRooms: boolean, openOnAdminRoom = fa
});
}

if (isFirstTimeNewExpensifyUser) {
// Filter out the systemChat report from the reports list, as we don't want to drop the user into that report over Concierge when they first log in
reportsValues = reportsValues.filter((report) => !isSystemChat(report)) ?? [];
if (reportsValues.length === 1) {
return reportsValues[0];
}

return adminReport ?? reportsValues.find((report) => !isConciergeChatReport(report));
}

// If we only have two reports and one of them is the system chat, filter it out so we don't
// overwrite showing the concierge chat
const hasSystemChat = reportsValues.find((report) => isSystemChat(report)) ?? false;
if (reportsValues.length === 2 && hasSystemChat) {
reportsValues = reportsValues.filter((report) => !isSystemChat(report)) ?? [];
}
// Filter out the system chat (Expensify chat) because the composer is disabled in it,
// and it prompts the user to use the Concierge chat instead.
reportsValues = reportsValues.filter((report) => !isSystemChat(report)) ?? [];

// We are getting the last read report from the metadata of the report.
// 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.
const lastRead = getMostRecentlyVisitedReport(reportsValues, allReportMetadata);

return adminReport ?? lastRead;
}

Expand Down Expand Up @@ -1535,9 +1516,9 @@ function isChildReport(report: OnyxEntry<Report>): boolean {
* An Expense Request is a thread where the parent report is an Expense Report and
* the parentReportAction is a transaction.
*/
function isExpenseRequest(report: OnyxInputOrEntry<Report>): boolean {
function isExpenseRequest(report: OnyxInputOrEntry<Report>): report is Thread {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID];
const parentReport = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`];
return isExpenseReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
Expand All @@ -1550,7 +1531,7 @@ function isExpenseRequest(report: OnyxInputOrEntry<Report>): boolean {
*/
function isIOURequest(report: OnyxInputOrEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID];
const parentReport = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`];
return isIOUReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
Expand All @@ -1563,7 +1544,7 @@ function isIOURequest(report: OnyxInputOrEntry<Report>): boolean {
*/
function isTrackExpenseReport(report: OnyxInputOrEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID];
return !isEmptyObject(parentReportAction) && ReportActionsUtils.isTrackExpenseAction(parentReportAction);
}
return false;
Expand Down Expand Up @@ -2212,7 +2193,7 @@ function getIcons(
return [fallbackIcon];
}
if (isExpenseRequest(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID];
const workspaceIcon = getWorkspaceIcon(report, policy);
const memberIcon = {
source: personalDetails?.[parentReportAction?.actorAccountID ?? -1]?.avatar ?? FallbackAvatar,
Expand All @@ -2225,7 +2206,7 @@ function getIcons(
return [memberIcon, workspaceIcon];
}
if (isChatThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID];

const actorAccountID = getReportActionActorAccountID(parentReportAction, report);
const actorDisplayName = PersonalDetailsUtils.getDisplayNameOrDefault(allPersonalDetails?.[actorAccountID ?? -1], '', false);
Expand Down Expand Up @@ -3106,8 +3087,9 @@ function canHoldUnholdReportAction(reportAction: OnyxInputOrEntry<ReportAction>)
const transactionID = moneyRequestReport ? ReportActionsUtils.getOriginalMessage(reportAction)?.IOUTransactionID : 0;
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction);

const parentReportAction = ReportActionsUtils.getParentReportAction(moneyRequestReport);

const parentReportAction = isThread(moneyRequestReport)
? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReport.parentReportID}`]?.[moneyRequestReport.parentReportActionID]
: undefined;
const isRequestIOU = isIOUReport(moneyRequestReport);
const isHoldActionCreator = isHoldCreator(transaction, reportAction.childReportID ?? '-1');

Expand Down Expand Up @@ -3713,7 +3695,12 @@ function getReportName(
}

let formattedName: string | undefined;
const parentReportAction = parentReportActionParam ?? ReportActionsUtils.getParentReportAction(report);
let parentReportAction: OnyxEntry<ReportAction>;
if (parentReportActionParam) {
parentReportAction = parentReportActionParam;
} else {
parentReportAction = isThread(report) ? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID] : undefined;
}
const parentReportActionMessage = ReportActionsUtils.getReportActionMessage(parentReportAction);

if (parentReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.SUBMITTED) {
Expand Down Expand Up @@ -4249,10 +4236,13 @@ function buildOptimisticTaskCommentReportAction(

// These parameters are not saved on the reportAction, but are used to display the task in the UI
// Added when we fetch the reportActions on a report
reportAction.reportAction.originalMessage = {
html: ReportActionsUtils.getReportActionHtml(reportAction.reportAction),
taskReportID: ReportActionsUtils.getReportActionMessage(reportAction.reportAction)?.taskReportID,
whisperedTo: [],
reportAction.reportAction = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

reportAction.originalMessage throws a lint error. We use originalMessage in building all optimistic report actions in this file. So, just refactored to remove that error.

Copy link
Contributor

Choose a reason for hiding this comment

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

@c3024 We have a PR #49325 addressing the linting of the src/libs/ReportUtils.ts file. What do you think about waiting for the other PR before proceeding?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Commented on that PR. Wish I checked that before.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wish I checked that before

That happens sometimes 😃

...reportAction.reportAction,
originalMessage: {
html: ReportActionsUtils.getReportActionHtml(reportAction.reportAction),
taskReportID: ReportActionsUtils.getReportActionMessage(reportAction.reportAction)?.taskReportID,
whisperedTo: [],
},
};
reportAction.reportAction.childReportID = taskReportID;
reportAction.reportAction.parentReportID = parentReportID;
Expand Down Expand Up @@ -5995,7 +5985,7 @@ function shouldReportBeInOptionList({
// This can also happen for anyone accessing a public room or archived room for which they don't have access to the underlying policy.
// Optionally exclude reports that do not belong to currently active workspace

const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReportAction = isThread(report) ? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID] : undefined;

if (
!report?.reportID ||
Expand Down
Loading