Skip to content

Commit 8924709

Browse files
authored
Merge pull request Expensify#68633 from lorretheboy/fix/66874
Expense preview in Reports > Chats keeps switching between main account and copilot avatar
2 parents d6d08a9 + e90040e commit 8924709

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/components/ReportActionAvatars/useReportActionAvatars.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {FallbackAvatar} from '@components/Icon/Expensicons';
44
import useOnyx from '@hooks/useOnyx';
55
import usePolicy from '@hooks/usePolicy';
66
import useReportIsArchived from '@hooks/useReportIsArchived';
7-
import {getOriginalMessage, getReportAction, isMoneyRequestAction} from '@libs/ReportActionsUtils';
7+
import {getDelegateAccountIDFromReportAction, getOriginalMessage, getReportAction, isMoneyRequestAction} from '@libs/ReportActionsUtils';
88
import {
99
getDefaultWorkspaceAvatar,
1010
getDisplayNameForParticipant,
@@ -91,7 +91,8 @@ function useReportActionAvatars({
9191
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
9292
const [policyChatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportIDAnnounce || chatReportIDAdmins}`, {canBeMissing: true});
9393

94-
const delegatePersonalDetails = action?.delegateAccountID ? personalDetails?.[action?.delegateAccountID] : undefined;
94+
const delegateAccountID = getDelegateAccountIDFromReportAction(action);
95+
const delegatePersonalDetails = delegateAccountID ? personalDetails?.[delegateAccountID] : undefined;
9596
const actorAccountID = getReportActionActorAccountID(action, iouReport, chatReport, delegatePersonalDetails);
9697

9798
const isAInvoiceReport = isInvoiceReport(iouReport ?? null);
@@ -315,7 +316,7 @@ function useReportActionAvatars({
315316
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
316317
actorHint: String(shouldUsePrimaryAvatarID ? primaryAvatar.id : login || defaultDisplayName || fallbackDisplayName).replace(CONST.REGEX.MERGED_ACCOUNT_PREFIX, ''),
317318
accountID,
318-
delegateAccountID: !isWorkspaceActor && delegatePersonalDetails ? actorAccountID : undefined,
319+
delegateAccountID: !isWorkspaceActor && !!delegateAccountID ? actorAccountID : undefined,
319320
},
320321
source: {
321322
iouReport,

src/libs/ReportActionsUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ function getOriginalMessage<T extends ReportActionName>(reportAction: OnyxInputO
274274
return reportAction.originalMessage;
275275
}
276276

277+
function getDelegateAccountIDFromReportAction(reportAction: OnyxInputOrEntry<ReportAction>): number | undefined {
278+
if (!reportAction) {
279+
return undefined;
280+
}
281+
282+
if (reportAction.delegateAccountID) {
283+
return reportAction.delegateAccountID;
284+
}
285+
286+
const originalMessage = getOriginalMessage(reportAction);
287+
if (!originalMessage) {
288+
return undefined;
289+
}
290+
291+
if ('delegateAccountID' in originalMessage) {
292+
return originalMessage.delegateAccountID;
293+
}
294+
295+
return undefined;
296+
}
297+
277298
function isExportIntegrationAction(reportAction: OnyxInputOrEntry<ReportAction>): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.EXPORTED_TO_INTEGRATION> {
278299
return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.EXPORTED_TO_INTEGRATION;
279300
}
@@ -3189,6 +3210,7 @@ export {
31893210
getVacationer,
31903211
getSubmittedTo,
31913212
getReceiptScanFailedMessage,
3213+
getDelegateAccountIDFromReportAction,
31923214
};
31933215

31943216
export type {LastVisibleMessage};

src/pages/home/report/ReportActionItemSingle.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
66
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
77
import ReportActionAvatars from '@components/ReportActionAvatars';
88
import useReportActionAvatars from '@components/ReportActionAvatars/useReportActionAvatars';
9+
import useReportPreviewSenderID from '@components/ReportActionAvatars/useReportPreviewSenderID';
910
import Text from '@components/Text';
1011
import Tooltip from '@components/Tooltip';
1112
import useLocalize from '@hooks/useLocalize';
13+
import useOnyx from '@hooks/useOnyx';
1214
import useStyleUtils from '@hooks/useStyleUtils';
1315
import useTheme from '@hooks/useTheme';
1416
import useThemeStyles from '@hooks/useThemeStyles';
1517
import ControlSelection from '@libs/ControlSelection';
1618
import DateUtils from '@libs/DateUtils';
1719
import Navigation from '@libs/Navigation/Navigation';
1820
import {getPersonalDetailByEmail} from '@libs/PersonalDetailsUtils';
19-
import {getManagerOnVacation, getReportActionMessage, getSubmittedTo, getVacationer} from '@libs/ReportActionsUtils';
21+
import {getDelegateAccountIDFromReportAction, getManagerOnVacation, getReportActionMessage, getSubmittedTo, getVacationer} from '@libs/ReportActionsUtils';
2022
import {isOptimisticPersonalDetail} from '@libs/ReportUtils';
2123
import CONST from '@src/CONST';
24+
import ONYXKEYS from '@src/ONYXKEYS';
2225
import ROUTES from '@src/ROUTES';
2326
import type {Report, ReportAction} from '@src/types/onyx';
2427
import type ChildrenProps from '@src/types/utils/ChildrenProps';
@@ -78,14 +81,26 @@ function ReportActionItemSingle({
7881
const StyleUtils = useStyleUtils();
7982
const {translate} = useLocalize();
8083

84+
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
85+
canBeMissing: true,
86+
});
87+
8188
const {avatarType, avatars, details, source} = useReportActionAvatars({report: potentialIOUReport ?? report, action});
8289

8390
const reportID = source.chatReport?.reportID;
8491
const iouReportID = source.iouReport?.reportID;
8592

8693
const [primaryAvatar, secondaryAvatar] = avatars;
8794

88-
const accountOwnerDetails = getPersonalDetailByEmail(details.login ?? '');
95+
const reportPreviewSenderID = useReportPreviewSenderID({
96+
iouReport: potentialIOUReport,
97+
action,
98+
chatReport: source.chatReport,
99+
});
100+
const delegateAccountID = getDelegateAccountIDFromReportAction(action);
101+
const mainAccountID = delegateAccountID ? (reportPreviewSenderID ?? potentialIOUReport?.ownerAccountID ?? action?.childOwnerAccountID) : (details.accountID ?? CONST.DEFAULT_NUMBER_ID);
102+
const mainAccountLogin = mainAccountID ? (personalDetails?.[mainAccountID]?.login ?? details.login) : details.login;
103+
const accountOwnerDetails = getPersonalDetailByEmail(String(mainAccountLogin ?? ''));
89104

90105
// Vacation delegate details for submitted action
91106
const vacationer = getVacationer(action);
@@ -210,9 +225,7 @@ function ReportActionItemSingle({
210225
<ReportActionItemDate created={action?.created ?? ''} />
211226
</View>
212227
) : null}
213-
{!!action?.delegateAccountID && (
214-
<Text style={[styles.chatDelegateMessage]}>{translate('delegate.onBehalfOfMessage', {delegator: accountOwnerDetails?.displayName ?? ''})}</Text>
215-
)}
228+
{!!delegateAccountID && <Text style={[styles.chatDelegateMessage]}>{translate('delegate.onBehalfOfMessage', {delegator: accountOwnerDetails?.displayName ?? ''})}</Text>}
216229
{!!vacationer && !!submittedTo && (
217230
<Text style={[styles.chatDelegateMessage]}>
218231
{translate('statusPage.toAsVacationDelegate', {

0 commit comments

Comments
 (0)