diff --git a/src/components/ReportActionAvatars/useReportActionAvatars.ts b/src/components/ReportActionAvatars/useReportActionAvatars.ts index 2d52fd386530..2f4fd434ca0b 100644 --- a/src/components/ReportActionAvatars/useReportActionAvatars.ts +++ b/src/components/ReportActionAvatars/useReportActionAvatars.ts @@ -4,7 +4,7 @@ import {FallbackAvatar} from '@components/Icon/Expensicons'; import useOnyx from '@hooks/useOnyx'; import usePolicy from '@hooks/usePolicy'; import useReportIsArchived from '@hooks/useReportIsArchived'; -import {getOriginalMessage, getReportAction, isMoneyRequestAction} from '@libs/ReportActionsUtils'; +import {getDelegateAccountIDFromReportAction, getOriginalMessage, getReportAction, isMoneyRequestAction} from '@libs/ReportActionsUtils'; import { getDefaultWorkspaceAvatar, getDisplayNameForParticipant, @@ -91,7 +91,8 @@ function useReportActionAvatars({ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const [policyChatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportIDAnnounce || chatReportIDAdmins}`, {canBeMissing: true}); - const delegatePersonalDetails = action?.delegateAccountID ? personalDetails?.[action?.delegateAccountID] : undefined; + const delegateAccountID = getDelegateAccountIDFromReportAction(action); + const delegatePersonalDetails = delegateAccountID ? personalDetails?.[delegateAccountID] : undefined; const actorAccountID = getReportActionActorAccountID(action, iouReport, chatReport, delegatePersonalDetails); const isAInvoiceReport = isInvoiceReport(iouReport ?? null); @@ -315,7 +316,7 @@ function useReportActionAvatars({ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing actorHint: String(shouldUsePrimaryAvatarID ? primaryAvatar.id : login || defaultDisplayName || fallbackDisplayName).replace(CONST.REGEX.MERGED_ACCOUNT_PREFIX, ''), accountID, - delegateAccountID: !isWorkspaceActor && delegatePersonalDetails ? actorAccountID : undefined, + delegateAccountID: !isWorkspaceActor && !!delegateAccountID ? actorAccountID : undefined, }, source: { iouReport, diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index de1623cac308..746a6d2c25a4 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -271,6 +271,27 @@ function getOriginalMessage(reportAction: OnyxInputO return reportAction.originalMessage; } +function getDelegateAccountIDFromReportAction(reportAction: OnyxInputOrEntry): number | undefined { + if (!reportAction) { + return undefined; + } + + if (reportAction.delegateAccountID) { + return reportAction.delegateAccountID; + } + + const originalMessage = getOriginalMessage(reportAction); + if (!originalMessage) { + return undefined; + } + + if ('delegateAccountID' in originalMessage) { + return originalMessage.delegateAccountID; + } + + return undefined; +} + function isExportIntegrationAction(reportAction: OnyxInputOrEntry): reportAction is ReportAction { return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.EXPORTED_TO_INTEGRATION; } @@ -3157,6 +3178,7 @@ export { getVacationer, getSubmittedTo, getReceiptScanFailedMessage, + getDelegateAccountIDFromReportAction, }; export type {LastVisibleMessage}; diff --git a/src/pages/home/report/ReportActionItemSingle.tsx b/src/pages/home/report/ReportActionItemSingle.tsx index 848cc94a122b..55d657a6044c 100644 --- a/src/pages/home/report/ReportActionItemSingle.tsx +++ b/src/pages/home/report/ReportActionItemSingle.tsx @@ -6,9 +6,11 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback'; import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback'; import ReportActionAvatars from '@components/ReportActionAvatars'; import useReportActionAvatars from '@components/ReportActionAvatars/useReportActionAvatars'; +import useReportPreviewSenderID from '@components/ReportActionAvatars/useReportPreviewSenderID'; import Text from '@components/Text'; import Tooltip from '@components/Tooltip'; import useLocalize from '@hooks/useLocalize'; +import useOnyx from '@hooks/useOnyx'; import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -16,9 +18,10 @@ import ControlSelection from '@libs/ControlSelection'; import DateUtils from '@libs/DateUtils'; import Navigation from '@libs/Navigation/Navigation'; import {getPersonalDetailByEmail} from '@libs/PersonalDetailsUtils'; -import {getManagerOnVacation, getReportActionMessage, getSubmittedTo, getVacationer} from '@libs/ReportActionsUtils'; +import {getDelegateAccountIDFromReportAction, getManagerOnVacation, getReportActionMessage, getSubmittedTo, getVacationer} from '@libs/ReportActionsUtils'; import {isOptimisticPersonalDetail} from '@libs/ReportUtils'; import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {Report, ReportAction} from '@src/types/onyx'; import type ChildrenProps from '@src/types/utils/ChildrenProps'; @@ -78,6 +81,10 @@ function ReportActionItemSingle({ const StyleUtils = useStyleUtils(); const {translate} = useLocalize(); + const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, { + canBeMissing: true, + }); + const {avatarType, avatars, details, source} = useReportActionAvatars({report: potentialIOUReport ?? report, action}); const reportID = source.chatReport?.reportID; @@ -85,7 +92,15 @@ function ReportActionItemSingle({ const [primaryAvatar, secondaryAvatar] = avatars; - const accountOwnerDetails = getPersonalDetailByEmail(details.login ?? ''); + const reportPreviewSenderID = useReportPreviewSenderID({ + iouReport: potentialIOUReport, + action, + chatReport: source.chatReport, + }); + const delegateAccountID = getDelegateAccountIDFromReportAction(action); + const mainAccountID = delegateAccountID ? (reportPreviewSenderID ?? potentialIOUReport?.ownerAccountID ?? action?.childOwnerAccountID) : (details.accountID ?? CONST.DEFAULT_NUMBER_ID); + const mainAccountLogin = mainAccountID ? (personalDetails?.[mainAccountID]?.login ?? details.login) : details.login; + const accountOwnerDetails = getPersonalDetailByEmail(String(mainAccountLogin ?? '')); // Vacation delegate details for submitted action const vacationer = getVacationer(action); @@ -210,9 +225,7 @@ function ReportActionItemSingle({ ) : null} - {!!action?.delegateAccountID && ( - {translate('delegate.onBehalfOfMessage', {delegator: accountOwnerDetails?.displayName ?? ''})} - )} + {!!delegateAccountID && {translate('delegate.onBehalfOfMessage', {delegator: accountOwnerDetails?.displayName ?? ''})}} {!!vacationer && !!submittedTo && ( {translate('statusPage.toAsVacationDelegate', {