Skip to content
18 changes: 12 additions & 6 deletions src/hooks/useSidebarOrderedReports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function SidebarOrderedReportsContextProvider({
const [currentReportsToDisplay, setCurrentReportsToDisplay] = useState<ReportsToDisplayInLHN>({});
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {isOffline} = useNetwork();
const {accountID} = useCurrentUserPersonalDetails();
const {accountID, login: currentUserLogin} = useCurrentUserPersonalDetails();
const {currentReportID: currentReportIDValue} = useCurrentReportIDState();
const derivedCurrentReportID = currentReportIDForTests ?? currentReportIDValue;
const prevDerivedCurrentReportID = usePrevious(derivedCurrentReportID);
Expand Down Expand Up @@ -213,21 +213,25 @@ function SidebarOrderedReportsContextProvider({
draftComments: reportsDrafts,
transactions,
isOffline,
currentUserLogin: currentUserLogin ?? '',
currentUserAccountID: accountID,
});
} else {
Log.info('[useSidebarOrderedReports] building reportsToDisplay from scratch');
reportsToDisplay = SidebarUtils.getReportsToDisplayInLHN(
derivedCurrentReportID,
chatReports,
reportsToDisplay = SidebarUtils.getReportsToDisplayInLHN({
currentReportId: derivedCurrentReportID,
reports: chatReports,
betas,
priorityMode,
reportsDrafts,
draftComments: reportsDrafts,
transactionViolations,
transactions,
isOffline,
currentUserLogin: currentUserLogin ?? '',
currentUserAccountID: accountID,
reportNameValuePairs,
reportAttributes,
);
});
}

return reportsToDisplay;
Expand All @@ -245,6 +249,8 @@ function SidebarOrderedReportsContextProvider({
reportsDrafts,
isOffline,
clearCacheDummyCounter,
currentUserLogin,
accountID,
]);

// Derive a stable boolean map indicating which reports have drafts.
Expand Down
6 changes: 6 additions & 0 deletions src/libs/DebugUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,8 @@ function getReasonForShowingRowInLHN({
isInFocusMode = false,
betas = undefined,
draftComment,
currentUserLogin,
currentUserAccountID,
}: {
report: OnyxEntry<Report>;
chatReport: OnyxEntry<Report>;
Expand All @@ -1425,6 +1427,8 @@ function getReasonForShowingRowInLHN({
isInFocusMode?: boolean;
betas?: OnyxEntry<Beta[]>;
draftComment: string | undefined;
currentUserLogin?: string;
currentUserAccountID?: number;
}): TranslationPaths | null {
if (!report) {
return null;
Expand All @@ -1442,6 +1446,8 @@ function getReasonForShowingRowInLHN({
includeSelfDM: true,
isReportArchived,
draftComment,
currentUserLogin,
currentUserAccountID,
});

if (!([CONST.REPORT_IN_LHN_REASONS.HAS_ADD_WORKSPACE_ROOM_ERRORS, CONST.REPORT_IN_LHN_REASONS.HAS_IOU_VIOLATIONS] as Array<typeof reason>).includes(reason) && hasRBR) {
Expand Down
4 changes: 4 additions & 0 deletions src/libs/OptionsListUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,7 @@ function isValidReport(option: SearchOption<Report>, policy: OnyxEntry<Policy>,
isRestrictedToPreferredPolicy,
preferredPolicyID,
currentUserAccountID,
currentUserLogin,
shouldAlwaysIncludeDM,
isTimeRequest = false,
} = config;
Expand All @@ -2062,6 +2063,8 @@ function isValidReport(option: SearchOption<Report>, policy: OnyxEntry<Policy>,
includeDomainEmail,
isReportArchived: option.private_isArchived,
draftComment,
currentUserLogin,
currentUserAccountID,
});

if (!shouldBeInOptionList) {
Expand Down Expand Up @@ -2446,6 +2449,7 @@ function getValidOptions(
includeDomainEmail,
loginsToExclude: loginsToExcludeFromSuggestions,
currentUserAccountID,
currentUserLogin: currentUserEmail,
},
draftComment,
chatReport,
Expand Down
1 change: 1 addition & 0 deletions src/libs/OptionsListUtils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ type IsValidReportsConfig = Pick<
| 'isTimeRequest'
> & {
currentUserAccountID: number;
currentUserLogin: string;
};

type GetOptionsConfig = {
Expand Down
17 changes: 13 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ import {isEmptyObject, isEmptyValueObject} from '@src/types/utils/EmptyObject';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
import {getBankAccountFromID} from './actions/BankAccounts';
import {getUserAccountID} from './actions/IOU';
import {unholdRequest} from './actions/IOU/Hold';
import {
createDraftTransaction,
Expand Down Expand Up @@ -4321,8 +4320,14 @@ function getReasonAndReportActionThatRequiresAttention(
* @param option (report or optionItem)
* @param parentReportAction (the report action the current report is a thread of)
*/
function requiresAttentionFromCurrentUser(optionOrReport: OnyxEntry<Report> | OptionData, parentReportAction?: OnyxEntry<ReportAction>, isReportArchived = false) {
return !!getReasonAndReportActionThatRequiresAttention(optionOrReport, getCurrentUserEmail() ?? '', getUserAccountID(), parentReportAction, isReportArchived);
function requiresAttentionFromCurrentUser(
optionOrReport: OnyxEntry<Report> | OptionData,
currentUserLogin: string,
currentUserAccountID: number,
parentReportAction?: OnyxEntry<ReportAction>,
isReportArchived = false,
) {
return !!getReasonAndReportActionThatRequiresAttention(optionOrReport, currentUserLogin, currentUserAccountID, parentReportAction, isReportArchived);
}

/**
Expand Down Expand Up @@ -9486,6 +9491,8 @@ type ShouldReportBeInOptionListParams = {
doesReportHaveViolations: boolean;
includeSelfDM?: boolean;
login?: string;
currentUserLogin?: string;
currentUserAccountID?: number;
includeDomainEmail?: boolean;
isReportArchived: boolean | undefined;
draftComment: string | undefined;
Expand All @@ -9504,6 +9511,8 @@ function reasonForReportToBeInOptionList({
draftComment,
includeSelfDM = false,
login,
currentUserLogin,
currentUserAccountID,
includeDomainEmail = false,
isReportArchived,
requiresAttention,
Expand Down Expand Up @@ -9587,7 +9596,7 @@ function reasonForReportToBeInOptionList({
return CONST.REPORT_IN_LHN_REASONS.HAS_DRAFT_COMMENT;
}

if (requiresAttention ?? requiresAttentionFromCurrentUser(report, undefined, isReportArchived)) {
if (requiresAttention ?? requiresAttentionFromCurrentUser(report, currentUserLogin ?? '', currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID, undefined, isReportArchived)) {
return CONST.REPORT_IN_LHN_REASONS.HAS_GBR;
}

Expand Down
53 changes: 41 additions & 12 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ type ShouldDisplayReportInLHNParams = {
isOffline: boolean;
isReportArchived?: boolean;
reportAttributes?: ReportAttributesDerivedValue['reports'];
currentUserLogin: string;
currentUserAccountID: number;
};

function shouldDisplayReportInLHN({
Expand All @@ -288,6 +290,8 @@ function shouldDisplayReportInLHN({
isOffline,
isReportArchived,
reportAttributes,
currentUserAccountID,
currentUserLogin,
}: ShouldDisplayReportInLHNParams) {
if (!report) {
return {shouldDisplay: false};
Expand Down Expand Up @@ -345,23 +349,40 @@ function shouldDisplayReportInLHN({
includeSelfDM: true,
isReportArchived,
requiresAttention,
currentUserLogin,
currentUserAccountID,
});

return {shouldDisplay};
}

function getReportsToDisplayInLHN(
currentReportId: string | undefined,
reports: OnyxCollection<Report>,
betas: OnyxEntry<Beta[]>,
priorityMode: OnyxEntry<PriorityMode>,
draftComments: OnyxCollection<string>,
transactionViolations: OnyxCollection<TransactionViolation[]>,
transactions: OnyxCollection<Transaction>,
isOffline: boolean,
reportNameValuePairs?: OnyxCollection<ReportNameValuePairs>,
reportAttributes?: ReportAttributesDerivedValue['reports'],
) {
function getReportsToDisplayInLHN({
currentReportId,
reports,
betas,
priorityMode,
draftComments,
transactionViolations,
transactions,
isOffline,
currentUserLogin,
currentUserAccountID,
reportNameValuePairs,
reportAttributes,
}: {
currentReportId: string | undefined;
reports: OnyxCollection<Report>;
betas: OnyxEntry<Beta[]>;
priorityMode: OnyxEntry<PriorityMode>;
draftComments: OnyxCollection<string>;
transactionViolations: OnyxCollection<TransactionViolation[]>;
transactions: OnyxCollection<Transaction>;
isOffline: boolean;
currentUserLogin: string;
currentUserAccountID: number;
reportNameValuePairs?: OnyxCollection<ReportNameValuePairs>;
reportAttributes?: ReportAttributesDerivedValue['reports'];
}) {
const isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD;
const allReportsDictValues = reports ?? {};
const reportsToDisplay: ReportsToDisplayInLHN = {};
Expand All @@ -385,6 +406,8 @@ function getReportsToDisplayInLHN(
isOffline,
isReportArchived: isArchivedReport(reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]),
reportAttributes,
currentUserLogin,
currentUserAccountID,
});

if (shouldDisplay) {
Expand All @@ -410,6 +433,8 @@ type UpdateReportsToDisplayInLHNProps = {
draftComments: OnyxCollection<string>;
transactions: OnyxCollection<Transaction>;
isOffline: boolean;
currentUserLogin: string;
currentUserAccountID: number;
};

function updateReportsToDisplayInLHN({
Expand All @@ -425,6 +450,8 @@ function updateReportsToDisplayInLHN({
draftComments,
transactions,
isOffline,
currentUserLogin,
currentUserAccountID,
}: UpdateReportsToDisplayInLHNProps) {
// Use a lazy copy to avoid creating a new object reference when no entries actually change.
let displayedReportsCopy: ReportsToDisplayInLHN | undefined;
Expand Down Expand Up @@ -460,6 +487,8 @@ function updateReportsToDisplayInLHN({
isOffline,
isReportArchived: isArchivedReport(reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`] ?? {}),
reportAttributes,
currentUserLogin,
currentUserAccountID,
});

if (shouldDisplay) {
Expand Down
14 changes: 13 additions & 1 deletion src/libs/UnreadIndicatorUpdater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ import * as ReportUtils from '@libs/ReportUtils';
import Navigation, {navigationRef} from '@navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report, ReportActions, ReportNameValuePairs} from '@src/types/onyx';
import type {Report, ReportActions, ReportNameValuePairs, Session} from '@src/types/onyx';
import updateUnread from './updateUnread';

let allReports: OnyxCollection<Report> = {};
let currentUserAccountID: number = CONST.DEFAULT_NUMBER_ID;
let currentUserLogin = '';

Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: (value: Session | undefined) => {
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
currentUserLogin = value?.email ?? '';
},
});

let allReportNameValuePairs: OnyxCollection<ReportNameValuePairs> = {};
// This subscription is used to update the unread indicators count which is not linked to UI and it does not update any UI state.
Expand Down Expand Up @@ -62,6 +72,8 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection<Report>, cur
excludeEmptyChats: false,
isReportArchived,
draftComment,
currentUserLogin,
currentUserAccountID,
}) &&
/**
* Chats with hidden preference remain invisible in the LHN and are not considered "unread."
Expand Down
12 changes: 6 additions & 6 deletions src/libs/actions/IOU/MoneyRequestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import type ReportAction from '@src/types/onyx/ReportAction';
import type {OnyxData} from '@src/types/onyx/Request';
import type {Receipt, TransactionChanges, TransactionCustomUnit, WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {getAllPersonalDetails, getAllReportActionsFromIOU, getAllReportNameValuePairs, getAllReports, getCurrentUserPersonalDetails, getUserAccountID} from './index';
import {getAllPersonalDetails, getAllReportActionsFromIOU, getAllReportNameValuePairs, getAllReports, getCurrentUserPersonalDetails} from './index';
import type {ReplaceReceipt} from './Receipt';
import {getSearchOnyxUpdate} from './SearchUpdate';
import type {StartSplitBilActionParams} from './Split';
Expand Down Expand Up @@ -261,6 +261,7 @@ type BuildOnyxDataForTestDriveIOUParams = {
iouOptimisticParams: MoneyRequestOptimisticParams['iou'];
chatOptimisticParams: MoneyRequestOptimisticParams['chat'];
testDriveCommentReportActionID?: string;
currentUserAccountIDParam: number;
};

function buildMinimalTransactionForFormula(
Expand Down Expand Up @@ -323,8 +324,6 @@ function getReceiptError(
function buildOnyxDataForTestDriveIOU(
testDriveIOUParams: BuildOnyxDataForTestDriveIOUParams,
): OnyxData<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.TRANSACTION_DRAFT> {
const deprecatedUserAccountID = getUserAccountID();
const deprecatedCurrentUserPersonalDetails = getCurrentUserPersonalDetails();
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT>> = [];
const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT>> = [];
const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.TRANSACTION_DRAFT>> = [];
Expand All @@ -340,11 +339,11 @@ function buildOnyxDataForTestDriveIOU(
transactionID: testDriveIOUParams.transaction.transactionID,
reportActionID: testDriveIOUParams.iouOptimisticParams.action.reportActionID,
});
const text = translateLocal('testDrive.employeeInviteMessage', getAllPersonalDetails()?.[deprecatedUserAccountID]?.firstName ?? '');
const text = translateLocal('testDrive.employeeInviteMessage', getAllPersonalDetails()?.[testDriveIOUParams.currentUserAccountIDParam]?.firstName ?? '');
// delegateAccountIDParam: will be threaded in PR 15; buildOptimisticAddCommentReportAction falls back to module-level Onyx.connect value (https://github.com/Expensify/App/issues/66425)
const textComment = buildOptimisticAddCommentReportAction({
text,
actorAccountID: deprecatedUserAccountID,
actorAccountID: testDriveIOUParams.currentUserAccountIDParam,
reportActionID: testDriveIOUParams.testDriveCommentReportActionID,
delegateAccountIDParam: undefined,
});
Expand All @@ -364,7 +363,7 @@ function buildOnyxDataForTestDriveIOU(
value: {
...{lastActionType: CONST.REPORT.ACTIONS.TYPE.MARKED_REIMBURSED, statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED},
hasOutstandingChildRequest: false,
lastActorAccountID: deprecatedCurrentUserPersonalDetails?.accountID,
lastActorAccountID: testDriveIOUParams.currentUserAccountIDParam,
},
},
{
Expand Down Expand Up @@ -618,6 +617,7 @@ function buildOnyxDataForMoneyRequest(moneyRequestParams: BuildOnyxDataForMoneyR
iouOptimisticParams: iou,
chatOptimisticParams: chat,
testDriveCommentReportActionID,
currentUserAccountIDParam,
});
onyxData.optimisticData?.push(...testDriveOptimisticData);
onyxData.successData?.push(...testDriveSuccessData);
Expand Down
7 changes: 4 additions & 3 deletions src/libs/actions/IOU/SearchUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type * as OnyxTypes from '@src/types/onyx';
import type {Participant} from '@src/types/onyx/IOU';
import type {OnyxData} from '@src/types/onyx/Request';
import type {SearchResultDataType} from '@src/types/onyx/SearchResults';
import {getCurrentUserPersonalDetails, getUserAccountID} from './index';
import {getCurrentUserPersonalDetails} from './index';

type ExpenseReportStatusPredicate = (expenseReport: OnyxEntry<OnyxTypes.Report>, transactionReportID?: string) => boolean;

Expand Down Expand Up @@ -44,6 +44,7 @@ function shouldOptimisticallyUpdateSearch(
currentSearchQueryJSON: Readonly<SearchQueryJSON>,
iouReport: OnyxEntry<OnyxTypes.Report>,
isInvoice: boolean | undefined,
currentUserAccountID: number,
transaction?: OnyxEntry<OnyxTypes.Transaction>,
) {
if (
Expand Down Expand Up @@ -76,7 +77,7 @@ function shouldOptimisticallyUpdateSearch(
return false;
}

const suggestedSearches = getSuggestedSearches(getUserAccountID());
const suggestedSearches = getSuggestedSearches(currentUserAccountID);
const submitQueryJSON = suggestedSearches[CONST.SEARCH.SEARCH_KEYS.SUBMIT].searchQueryJSON;
const approveQueryJSON = suggestedSearches[CONST.SEARCH.SEARCH_KEYS.APPROVE].searchQueryJSON;
const unapprovedCashSimilarSearchHash = suggestedSearches[CONST.SEARCH.SEARCH_KEYS.UNAPPROVED_CASH].similarSearchHash;
Expand Down Expand Up @@ -125,7 +126,7 @@ function getSearchOnyxUpdate({
return;
}

if (shouldOptimisticallyUpdateSearch(currentSearchQueryJSON, iouReport, isInvoice, transaction)) {
if (shouldOptimisticallyUpdateSearch(currentSearchQueryJSON, iouReport, isInvoice, fromAccountID, transaction)) {
const isOptimisticToAccountData = isOptimisticPersonalDetail(toAccountID);
const successData = [];
if (isOptimisticToAccountData) {
Expand Down
Loading
Loading