@@ -96,7 +96,20 @@ function SearchChangeApproverPage() {
9696 const [ onyxReports ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT , { selector : getOnyxReports } ) ;
9797
9898 const hasAutoAppliedRef = useRef ( false ) ;
99+ // Set when navigating to WORKSPACE_UPGRADE; prevents the auto-close in useLayoutEffect from
100+ // dismissing the RHP if selectedReports goes transiently empty before this page unmounts
101+ // TODO: drop this ref once the CHANGE_APPROVER_SEARCH_RHP `backTo` is removed so navigation matches the regular
102+ // "Change approver" flow after upgrading workspace. See https://github.com/Expensify/App/pull/89192.
103+ const hasInitiatedUpgradeRef = useRef ( false ) ;
99104 const prevSelectedReportsLength = useRef ( 0 ) ;
105+
106+ useEffect ( ( ) => {
107+ if ( selectedReports . length === 0 ) {
108+ return ;
109+ }
110+ hasInitiatedUpgradeRef . current = false ;
111+ } , [ selectedReports . length ] ) ;
112+
100113 useEffect ( ( ) => {
101114 if ( ! hasLoadedApp || ! selectedReports . length || prevSelectedReportsLength . current === selectedReports . length ) {
102115 return ;
@@ -123,12 +136,19 @@ function SearchChangeApproverPage() {
123136 const policiesToUpgrade = selectedPolicies . filter ( ( policy ) => ! isControlPolicy ( policy ) ) ;
124137 if ( policiesToUpgrade . length > 1 ) {
125138 // Bulk upgrade is not supported, so show a general page to guide the user to upgrade manually
126- Navigation . navigate ( ROUTES . WORKSPACE_UPGRADE . getRoute ( undefined , undefined , ROUTES . CHANGE_APPROVER_SEARCH_RHP ) ) ;
139+ hasInitiatedUpgradeRef . current = true ;
140+ Navigation . navigate ( ROUTES . WORKSPACE_UPGRADE . getRoute ( undefined , undefined , ROUTES . CHANGE_APPROVER_ADD_APPROVER_SEARCH_RHP ) , { forceReplace : true } ) ;
127141 return ;
128142 }
129143 if ( policiesToUpgrade . length === 1 ) {
144+ hasInitiatedUpgradeRef . current = true ;
130145 Navigation . navigate (
131- ROUTES . WORKSPACE_UPGRADE . getRoute ( policiesToUpgrade . at ( 0 ) ?. id , CONST . UPGRADE_FEATURE_INTRO_MAPPING . multiApprovalLevels . alias , ROUTES . CHANGE_APPROVER_SEARCH_RHP ) ,
146+ ROUTES . WORKSPACE_UPGRADE . getRoute (
147+ policiesToUpgrade . at ( 0 ) ?. id ,
148+ CONST . UPGRADE_FEATURE_INTRO_MAPPING . multiApprovalLevels . alias ,
149+ ROUTES . CHANGE_APPROVER_ADD_APPROVER_SEARCH_RHP ,
150+ ) ,
151+ { forceReplace : true } ,
132152 ) ;
133153 return ;
134154 }
@@ -208,6 +228,12 @@ function SearchChangeApproverPage() {
208228 return ;
209229 }
210230
231+ // Skip during an upgrade round-trip: selectedReports may be transiently empty while
232+ // navigating to WORKSPACE_UPGRADE, and closing the RHP would kill the Add Approver flow.
233+ if ( hasInitiatedUpgradeRef . current ) {
234+ return ;
235+ }
236+
211237 Navigation . setNavigationActionToMicrotaskQueue ( ( ) => {
212238 Navigation . closeRHPFlow ( ) ;
213239 } ) ;
0 commit comments