From ce354b0826919adf1c7cc582a8148e1360dddce1 Mon Sep 17 00:00:00 2001
From: tienifr <christianwen18@gmail.com>
Date: Thu, 14 Sep 2023 00:19:08 +0700
Subject: [PATCH 1/2] fix: 27054 Conversations do not load if you interrupt the
 Internet connection when logging in

---
 src/ONYXKEYS.ts                               |  3 +++
 .../Navigation/AppNavigator/AuthScreens.js    | 17 +++++++++++-
 src/libs/actions/App.js                       | 26 +++++++++++++++++--
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts
index 2e0b75910bae..5695d4321e37 100755
--- a/src/ONYXKEYS.ts
+++ b/src/ONYXKEYS.ts
@@ -175,6 +175,9 @@ const ONYXKEYS = {
     /** Is report data loading? */
     IS_LOADING_REPORT_DATA: 'isLoadingReportData',
 
+    /** Is report data loading? */
+    IS_LOADING_APP: 'isLoadingApp',
+
     /** Is Keyboard shortcuts modal open? */
     IS_SHORTCUTS_MODAL_OPEN: 'isShortcutsModalOpen',
 
diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js
index 37b7087b6ad4..57e038b7104e 100644
--- a/src/libs/Navigation/AppNavigator/AuthScreens.js
+++ b/src/libs/Navigation/AppNavigator/AuthScreens.js
@@ -38,6 +38,8 @@ import DemoSetupPage from '../../../pages/DemoSetupPage';
 
 let timezone;
 let currentAccountID;
+let isLoadingApp;
+
 Onyx.connect({
     key: ONYXKEYS.SESSION,
     callback: (val) => {
@@ -75,6 +77,13 @@ Onyx.connect({
     },
 });
 
+Onyx.connect({
+    key: ONYXKEYS.IS_LOADING_APP,
+    callback: (val) => {
+        isLoadingApp = val;
+    },
+});
+
 const RootStack = createCustomStackNavigator();
 
 // We want to delay the re-rendering for components(e.g. ReportActionCompose)
@@ -126,7 +135,13 @@ class AuthScreens extends React.Component {
 
     componentDidMount() {
         NetworkConnection.listenForReconnect();
-        NetworkConnection.onReconnect(() => App.reconnectApp(this.props.lastUpdateIDAppliedToClient));
+        NetworkConnection.onReconnect(() => {
+            if (isLoadingApp) {
+                App.openApp();
+            } else {
+                App.reconnectApp(this.props.lastUpdateIDAppliedToClient);
+            }
+        });
         PusherConnectionManager.init();
         Pusher.init({
             appKey: CONFIG.PUSHER.APP_KEY,
diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js
index fffe43b88ee9..b6f1617d70ad 100644
--- a/src/libs/actions/App.js
+++ b/src/libs/actions/App.js
@@ -141,9 +141,10 @@ function getPolicyParamsForOpenOrReconnect() {
 
 /**
  * Returns the Onyx data that is used for both the OpenApp and ReconnectApp API commands.
+ * @param {Boolean} isOpenApp
  * @returns {Object}
  */
-function getOnyxDataForOpenOrReconnect() {
+function getOnyxDataForOpenOrReconnect(isOpenApp = false) {
     return {
         optimisticData: [
             {
@@ -151,6 +152,13 @@ function getOnyxDataForOpenOrReconnect() {
                 key: ONYXKEYS.IS_LOADING_REPORT_DATA,
                 value: true,
             },
+            isOpenApp
+                ? {
+                      onyxMethod: Onyx.METHOD.MERGE,
+                      key: ONYXKEYS.IS_LOADING_APP,
+                      value: true,
+                  }
+                : undefined,
         ],
         successData: [
             {
@@ -158,6 +166,13 @@ function getOnyxDataForOpenOrReconnect() {
                 key: ONYXKEYS.IS_LOADING_REPORT_DATA,
                 value: false,
             },
+            isOpenApp
+                ? {
+                      onyxMethod: Onyx.METHOD.MERGE,
+                      key: ONYXKEYS.IS_LOADING_APP,
+                      value: false,
+                  }
+                : undefined,
         ],
         failureData: [
             {
@@ -165,6 +180,13 @@ function getOnyxDataForOpenOrReconnect() {
                 key: ONYXKEYS.IS_LOADING_REPORT_DATA,
                 value: false,
             },
+            isOpenApp
+                ? {
+                      onyxMethod: Onyx.METHOD.MERGE,
+                      key: ONYXKEYS.IS_LOADING_APP,
+                      value: false,
+                  }
+                : undefined,
         ],
     };
 }
@@ -174,7 +196,7 @@ function getOnyxDataForOpenOrReconnect() {
  */
 function openApp() {
     getPolicyParamsForOpenOrReconnect().then((policyParams) => {
-        API.read('OpenApp', policyParams, getOnyxDataForOpenOrReconnect());
+        API.read('OpenApp', policyParams, getOnyxDataForOpenOrReconnect(true));
     });
 }
 

From c9f3f2b6ed3a23786c92a4c34aff81dffb864cba Mon Sep 17 00:00:00 2001
From: tienifr <christianwen18@gmail.com>
Date: Wed, 20 Sep 2023 10:42:00 +0700
Subject: [PATCH 2/2] clean code

---
 src/libs/actions/App.js | 50 +++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js
index b6f1617d70ad..38d71622e1f4 100644
--- a/src/libs/actions/App.js
+++ b/src/libs/actions/App.js
@@ -145,20 +145,13 @@ function getPolicyParamsForOpenOrReconnect() {
  * @returns {Object}
  */
 function getOnyxDataForOpenOrReconnect(isOpenApp = false) {
-    return {
+    const defaultData = {
         optimisticData: [
             {
                 onyxMethod: Onyx.METHOD.MERGE,
                 key: ONYXKEYS.IS_LOADING_REPORT_DATA,
                 value: true,
             },
-            isOpenApp
-                ? {
-                      onyxMethod: Onyx.METHOD.MERGE,
-                      key: ONYXKEYS.IS_LOADING_APP,
-                      value: true,
-                  }
-                : undefined,
         ],
         successData: [
             {
@@ -166,13 +159,6 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false) {
                 key: ONYXKEYS.IS_LOADING_REPORT_DATA,
                 value: false,
             },
-            isOpenApp
-                ? {
-                      onyxMethod: Onyx.METHOD.MERGE,
-                      key: ONYXKEYS.IS_LOADING_APP,
-                      value: false,
-                  }
-                : undefined,
         ],
         failureData: [
             {
@@ -180,13 +166,33 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false) {
                 key: ONYXKEYS.IS_LOADING_REPORT_DATA,
                 value: false,
             },
-            isOpenApp
-                ? {
-                      onyxMethod: Onyx.METHOD.MERGE,
-                      key: ONYXKEYS.IS_LOADING_APP,
-                      value: false,
-                  }
-                : undefined,
+        ],
+    };
+    if (!isOpenApp) return defaultData;
+    return {
+        optimisticData: [
+            ...defaultData.optimisticData,
+            {
+                onyxMethod: Onyx.METHOD.MERGE,
+                key: ONYXKEYS.IS_LOADING_APP,
+                value: true,
+            },
+        ],
+        successData: [
+            ...defaultData.successData,
+            {
+                onyxMethod: Onyx.METHOD.MERGE,
+                key: ONYXKEYS.IS_LOADING_APP,
+                value: false,
+            },
+        ],
+        failureData: [
+            ...defaultData.failureData,
+            {
+                onyxMethod: Onyx.METHOD.MERGE,
+                key: ONYXKEYS.IS_LOADING_APP,
+                value: false,
+            },
         ],
     };
 }
