@@ -54,6 +54,7 @@ import {
5454 generateReportID ,
5555 getReportOrDraftReport ,
5656 hasViolations as hasViolationsReportUtils ,
57+ isMoneyRequestReport ,
5758 isProcessingReport ,
5859 isReportOutstanding ,
5960 isSelectedManagerMcTest ,
@@ -527,6 +528,41 @@ function IOURequestStepConfirmation({
527528 } ) ;
528529 } , [ requestType , iouType , initialTransactionID , reportID , action , report , transactions , participants ] ) ;
529530
531+ const policyParams = useMemo (
532+ ( ) => ( {
533+ policy,
534+ policyTagList : policyTags ,
535+ policyCategories,
536+ policyRecentlyUsedCategories,
537+ } ) ,
538+ [ policy , policyTags , policyCategories , policyRecentlyUsedCategories ] ,
539+ ) ;
540+
541+ const emptyPolicyParams = useMemo ( ( ) => ( { } ) , [ ] ) ;
542+
543+ const getMoneyRequestContextForParticipant = useCallback (
544+ ( participant : Participant | undefined ) => {
545+ const isWorkspaceTarget = ! ! participant ?. isPolicyExpenseChat ;
546+ const workspaceChatReport = isWorkspaceTarget && participant ?. reportID ? getReportOrDraftReport ( participant . reportID ) : undefined ;
547+ return {
548+ parentChatReport : isWorkspaceTarget ? workspaceChatReport : undefined ,
549+ policyParams : isWorkspaceTarget ? policyParams : emptyPolicyParams ,
550+ } ;
551+ } ,
552+ [ policyParams , emptyPolicyParams ] ,
553+ ) ;
554+
555+ const getReportToUseAndBackToReport = useCallback (
556+ ( participant : Participant | undefined , parentChatReport : Report | undefined , reportParam : Report | undefined , backToReportParam : string | undefined ) => {
557+ const reportToUse = participant ?. isPolicyExpenseChat ? parentChatReport : undefined ;
558+
559+ const backToReportToUse = backToReportParam ?? ( isMoneyRequestReport ( reportParam ) ? reportParam ?. reportID : undefined ) ;
560+
561+ return { reportToUse, backToReportToUse} ;
562+ } ,
563+ [ ] ,
564+ ) ;
565+
530566 const requestMoney = useCallback (
531567 ( selectedParticipants : Participant [ ] , gpsPoint ?: GpsPoint ) => {
532568 if ( ! transactions . length ) {
@@ -538,6 +574,9 @@ function IOURequestStepConfirmation({
538574 return ;
539575 }
540576
577+ const { parentChatReport, policyParams : contextPolicyParams } = getMoneyRequestContextForParticipant ( participant ) ;
578+ const { reportToUse, backToReportToUse} = getReportToUseAndBackToReport ( participant , parentChatReport , report , backToReport ) ;
579+
541580 const optimisticChatReportID = generateReportID ( ) ;
542581 const optimisticCreatedReportActionID = rand64 ( ) ;
543582 const optimisticReportPreviewActionID = rand64 ( ) ;
@@ -556,7 +595,7 @@ function IOURequestStepConfirmation({
556595 }
557596
558597 const { iouReport} = requestMoneyIOUActions ( {
559- report,
598+ report : reportToUse ,
560599 existingIOUReport,
561600 optimisticChatReportID,
562601 optimisticCreatedReportActionID,
@@ -566,12 +605,7 @@ function IOURequestStepConfirmation({
566605 payeeAccountID : currentUserPersonalDetails . accountID ,
567606 participant,
568607 } ,
569- policyParams : {
570- policy,
571- policyTagList : policyTags ,
572- policyCategories,
573- policyRecentlyUsedCategories,
574- } ,
608+ policyParams : contextPolicyParams ,
575609 gpsPoint,
576610 action,
577611 transactionParams : {
@@ -600,7 +634,7 @@ function IOURequestStepConfirmation({
600634 } ,
601635 shouldHandleNavigation : index === transactions . length - 1 ,
602636 shouldGenerateTransactionThreadReport,
603- backToReport,
637+ backToReport : backToReportToUse ,
604638 isASAPSubmitBetaEnabled,
605639 currentUserAccountIDParam : currentUserPersonalDetails . accountID ,
606640 currentUserEmailParam : currentUserPersonalDetails . login ?? '' ,
@@ -700,24 +734,23 @@ function IOURequestStepConfirmation({
700734 if ( ! participant ) {
701735 return ;
702736 }
737+ const { parentChatReport, policyParams : contextPolicyParams } = getMoneyRequestContextForParticipant ( participant ) ;
738+ const { reportToUse} = getReportToUseAndBackToReport ( participant , parentChatReport , report , backToReport ) ;
739+
703740 for ( const [ index , item ] of transactions . entries ( ) ) {
704741 const isLinkedTrackedExpenseReportArchived =
705742 ! ! item . linkedTrackedExpenseReportID && archivedReportsIdSet . has ( `${ ONYXKEYS . COLLECTION . REPORT_NAME_VALUE_PAIRS } ${ item . linkedTrackedExpenseReportID } ` ) ;
706743
707744 trackExpenseIOUActions ( {
708- report,
745+ report : reportToUse ,
709746 isDraftPolicy,
710747 action,
711748 participantParams : {
712749 payeeEmail : currentUserPersonalDetails . login ,
713750 payeeAccountID : currentUserPersonalDetails . accountID ,
714751 participant,
715752 } ,
716- policyParams : {
717- policy,
718- policyCategories,
719- policyTagList : policyTags ,
720- } ,
753+ policyParams : contextPolicyParams ,
721754 transactionParams : {
722755 amount : item . amount ,
723756 distance : isManualDistanceRequest ? ( item . comment ?. customUnit ?. quantity ?? undefined ) : undefined ,
@@ -774,19 +807,23 @@ function IOURequestStepConfirmation({
774807 if ( ! transaction ) {
775808 return ;
776809 }
810+
811+ const participant = selectedParticipants . at ( 0 ) ;
812+ if ( ! participant ) {
813+ return ;
814+ }
815+
816+ const { parentChatReport, policyParams : contextPolicyParams } = getMoneyRequestContextForParticipant ( participant ) ;
817+ const { reportToUse, backToReportToUse} = getReportToUseAndBackToReport ( participant , parentChatReport , report , backToReport ) ;
818+
777819 createDistanceRequestIOUActions ( {
778- report,
820+ report : reportToUse ,
779821 participants : selectedParticipants ,
780822 currentUserLogin : currentUserPersonalDetails . login ,
781823 currentUserAccountID : currentUserPersonalDetails . accountID ,
782824 iouType,
783825 existingTransaction : transaction ,
784- policyParams : {
785- policy,
786- policyCategories,
787- policyTagList : policyTags ,
788- policyRecentlyUsedCategories,
789- } ,
826+ policyParams : contextPolicyParams ,
790827 transactionParams : {
791828 amount : transaction . amount ,
792829 comment : trimmedComment ,
@@ -806,7 +843,7 @@ function IOURequestStepConfirmation({
806843 attendees : transaction . comment ?. attendees ,
807844 receipt : isManualDistanceRequest ? receiptFiles [ transaction . transactionID ] : undefined ,
808845 } ,
809- backToReport,
846+ backToReport : backToReportToUse ,
810847 isASAPSubmitBetaEnabled,
811848 transactionViolations,
812849 } ) ;
0 commit comments