From 22a558fb457316e5752a88a3c5a72cdf1680bacc Mon Sep 17 00:00:00 2001
From: VH <hoangzinhvn@gmail.com>
Date: Thu, 26 Mar 2026 21:23:37 +0700
Subject: [PATCH] refactor: use connectWithoutView for STASHED_CREDENTIALS

Replace Onyx.connect with connectWithoutView for STASHED_CREDENTIALS
key as it doesn't affect view rendering directly. Update hasStashedSession
to accept credentials as a parameter for better testability and enable
useOnyx usage in InitialSettingsPage.

This is part of the Onyx.connect deprecation effort.

Fixes #67786
---
 src/libs/actions/Session/index.ts          | 16 ++++++++--------
 src/pages/settings/InitialSettingsPage.tsx |  3 ++-
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts
index 629925660165..8219ff12743d 100644
--- a/src/libs/actions/Session/index.ts
+++ b/src/libs/actions/Session/index.ts
@@ -118,7 +118,7 @@ Onyx.connect({
 });
 
 let stashedCredentials: Credentials = {};
-Onyx.connect({
+Onyx.connectWithoutView({
     key: ONYXKEYS.STASHED_CREDENTIALS,
     callback: (value) => (stashedCredentials = value ?? {}),
 });
@@ -277,8 +277,8 @@ function isAnonymousUser(sessionParam?: OnyxEntry<Session>): boolean {
     return (sessionParam?.authTokenType ?? session.authTokenType) === CONST.AUTH_TOKEN_TYPES.ANONYMOUS;
 }
 
-function hasStashedSession(): boolean {
-    return !!(stashedSession.authToken && stashedCredentials.autoGeneratedLogin && stashedCredentials.autoGeneratedLogin !== '');
+function hasStashedSession(stashedCredentialsParam: Credentials | undefined): boolean {
+    return !!(stashedSession.authToken && stashedCredentialsParam?.autoGeneratedLogin && stashedCredentialsParam.autoGeneratedLogin !== '');
 }
 
 /**
@@ -367,7 +367,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess
     // If this is a supportal token, and we've received the parameters to stashSession as true, and
     // we already have a stashedSession, that means we are supportal-ed, currently supportal-ing
     // into another account and we want to keep the stashed data from the original account.
-    if (isSupportal && shouldStashSession && hasStashedSession()) {
+    if (isSupportal && shouldStashSession && hasStashedSession(stashedCredentials)) {
         onyxSetParams = {
             [ONYXKEYS.STASHED_CREDENTIALS]: stashedCredentials,
             [ONYXKEYS.STASHED_SESSION]: stashedSession,
@@ -376,7 +376,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess
 
     // If we should restore the stashed session, and we do not want to stash the current session, and we have a
     // stashed session, then switch the account instead of completely logging out.
-    if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession()) {
+    if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession(stashedCredentials)) {
         if (CONFIG.IS_HYBRID_APP) {
             HybridAppModule.switchAccount({
                 newDotCurrentAccountEmail: stashedSession.email ?? '',
@@ -392,7 +392,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess
             [ONYXKEYS.SESSION]: stashedSession,
         };
     }
-    if (shouldRestoreStashedSession && !shouldStashSession && !hasStashedSession()) {
+    if (shouldRestoreStashedSession && !shouldStashSession && !hasStashedSession(stashedCredentials)) {
         Log.info('No stashed session found, clearing the session');
     }
 
@@ -419,7 +419,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess
                     true,
                     true,
                 );
-            } else if (isPerformingSupportalLogout && hasStashedSession()) {
+            } else if (isPerformingSupportalLogout && hasStashedSession(stashedCredentials)) {
                 // We have confirmed here that the supportal agent was logged in, so we can restore the stashed session
                 // and then redirect to the oldDot supportal page to restore the stashed session
                 // Clear the Onyx DB of stale data that might be present from a previous session
@@ -432,7 +432,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess
                         });
                     });
                 });
-            } else if (isPerformingSupportalLogout && !hasStashedSession()) {
+            } else if (isPerformingSupportalLogout && !hasStashedSession(stashedCredentials)) {
                 // If the supportal agent was not logged in, we call `redirectToSignIn` to clear the Onyx DB
                 // and then redirect to supportal and restore the stashed session
                 redirectToSignIn().then(() => {
diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx
index e698c9c22544..6f9f6e654c33 100755
--- a/src/pages/settings/InitialSettingsPage.tsx
+++ b/src/pages/settings/InitialSettingsPage.tsx
@@ -149,6 +149,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
     const [isTrackingGPS = false] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS, {selector: isTrackingSelector});
     const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL);
     const [unsharedBankAccount] = useOnyx(ONYXKEYS.UNSHARE_BANK_ACCOUNT);
+    const [stashedCredentials] = useOnyx(ONYXKEYS.STASHED_CREDENTIALS);
     const privateSubscription = usePrivateSubscription();
     const subscriptionPlan = useSubscriptionPlan();
     const previousUserPersonalDetails = usePrevious(currentUserPersonalDetails);
@@ -344,7 +345,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
      * Return a list of menu items data for general section
      * @returns object with translationKey, style and items for the general section
      */
-    const signOutTranslationKey = isSupportAuthToken() && hasStashedSession() ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut';
+    const signOutTranslationKey = isSupportAuthToken() && hasStashedSession(stashedCredentials) ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut';
     const generalMenuItemsData: Menu = {
         sectionStyle: {
             ...styles.pt4,
