From fa716a984c7cfccf788e4262151ad2b54f9cc3c2 Mon Sep 17 00:00:00 2001
From: Marc Glasser <marc.aaron.glasser@gmail.com>
Date: Tue, 12 Mar 2024 15:30:49 -1000
Subject: [PATCH 1/6] Add Onyx key to allow remotely resetting client data

---
 src/ONYXKEYS.ts                               |  4 ++
 src/libs/actions/App.ts                       | 37 +++++++++++++++++++
 .../settings/AboutPage/TroubleshootPage.tsx   | 18 +--------
 3 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts
index 8c48cbad561f..26af65ea1029 100755
--- a/src/ONYXKEYS.ts
+++ b/src/ONYXKEYS.ts
@@ -271,6 +271,9 @@ const ONYXKEYS = {
     /** Indicates whether an forced upgrade is required */
     UPDATE_REQUIRED: 'updateRequired',
 
+    /** Indicates whether an forced reset is required a.k.a. clear Oynx data without signing the user out */
+    RESET_REQUIRED: 'resetRequired',
+
     /** Stores the logs of the app for debugging purposes */
     LOGS: 'logs',
 
@@ -583,6 +586,7 @@ type OnyxValuesMapping = {
     [ONYXKEYS.LAST_VISITED_PATH]: string | undefined;
     [ONYXKEYS.RECENTLY_USED_REPORT_FIELDS]: OnyxTypes.RecentlyUsedReportFields;
     [ONYXKEYS.UPDATE_REQUIRED]: boolean;
+    [ONYXKEYS.RESET_REQUIRED]: boolean;
     [ONYXKEYS.PLAID_CURRENT_EVENT]: string;
     [ONYXKEYS.LOGS]: Record<number, OnyxTypes.Log>;
     [ONYXKEYS.SHOULD_STORE_LOGS]: boolean;
diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts
index 302e4048d0e8..6d4c79fc8843 100644
--- a/src/libs/actions/App.ts
+++ b/src/libs/actions/App.ts
@@ -31,6 +31,7 @@ import ROUTES from '@src/ROUTES';
 import type * as OnyxTypes from '@src/types/onyx';
 import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';
 import type {OnyxData} from '@src/types/onyx/Request';
+import type {OnyxKey} from '@src/ONYXKEYS';
 import * as Policy from './Policy';
 import * as Session from './Session';
 import Timing from './Timing';
@@ -77,6 +78,41 @@ Onyx.connect({
     },
 });
 
+const KEYS_TO_PRESERVE: OnyxKey[] = [
+    ONYXKEYS.ACCOUNT,
+    ONYXKEYS.IS_CHECKING_PUBLIC_ROOM,
+    ONYXKEYS.IS_LOADING_APP,
+    ONYXKEYS.IS_SIDEBAR_LOADED,
+    ONYXKEYS.MODAL,
+    ONYXKEYS.NETWORK,
+    ONYXKEYS.SESSION,
+    ONYXKEYS.SHOULD_SHOW_COMPOSE_INPUT,
+    ONYXKEYS.NVP_TRY_FOCUS_MODE,
+    ONYXKEYS.PREFERRED_THEME,
+    ONYXKEYS.NVP_PREFERRED_LOCALE,
+    ONYXKEYS.CREDENTIALS,
+];
+
+let previousResetRequired: boolean|null = false;
+Onyx.connect({
+    key: ONYXKEYS.RESET_REQUIRED,
+    callback: (isResetRequired) => {
+        // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
+        if (previousResetRequired || !isResetRequired) {
+            return;
+        }
+
+        previousResetRequired = isResetRequired;
+        Onyx.clear(KEYS_TO_PRESERVE).then(() => {
+            // Set this to false to reset the flag for this client
+            Onyx.set(ONYXKEYS.RESET_REQUIRED, false);
+
+            // eslint-disable-next-line @typescript-eslint/no-use-before-define
+            openApp();
+        });
+    },
+});
+
 let resolveIsReadyPromise: () => void;
 const isReadyToOpenApp = new Promise<void>((resolve) => {
     resolveIsReadyPromise = resolve;
@@ -529,4 +565,5 @@ export {
     savePolicyDraftByNewWorkspace,
     createWorkspaceWithPolicyDraftAndNavigateToIt,
     updateLastVisitedPath,
+    KEYS_TO_PRESERVE,
 };
diff --git a/src/pages/settings/AboutPage/TroubleshootPage.tsx b/src/pages/settings/AboutPage/TroubleshootPage.tsx
index 9bc756df03cb..5f7dfbc33323 100644
--- a/src/pages/settings/AboutPage/TroubleshootPage.tsx
+++ b/src/pages/settings/AboutPage/TroubleshootPage.tsx
@@ -24,25 +24,9 @@ import * as App from '@userActions/App';
 import * as Report from '@userActions/Report';
 import type {TranslationPaths} from '@src/languages/types';
 import ONYXKEYS from '@src/ONYXKEYS';
-import type {OnyxKey} from '@src/ONYXKEYS';
 import ROUTES from '@src/ROUTES';
 import SCREENS from '@src/SCREENS';
 
-const keysToPreserve: OnyxKey[] = [
-    ONYXKEYS.ACCOUNT,
-    ONYXKEYS.IS_CHECKING_PUBLIC_ROOM,
-    ONYXKEYS.IS_LOADING_APP,
-    ONYXKEYS.IS_SIDEBAR_LOADED,
-    ONYXKEYS.MODAL,
-    ONYXKEYS.NETWORK,
-    ONYXKEYS.SESSION,
-    ONYXKEYS.SHOULD_SHOW_COMPOSE_INPUT,
-    ONYXKEYS.NVP_TRY_FOCUS_MODE,
-    ONYXKEYS.PREFERRED_THEME,
-    ONYXKEYS.NVP_PREFERRED_LOCALE,
-    ONYXKEYS.CREDENTIALS,
-];
-
 type BaseMenuItem = {
     translationKey: TranslationPaths;
     icon: React.FC<SvgProps>;
@@ -136,7 +120,7 @@ function TroubleshootPage({shouldStoreLogs}: TroubleshootPageProps) {
                 isVisible={isConfirmationModalVisible}
                 onConfirm={() => {
                     setIsConfirmationModalVisible(false);
-                    Onyx.clear(keysToPreserve).then(() => {
+                    Onyx.clear(App.KEYS_TO_PRESERVE).then(() => {
                         App.openApp();
                     });
                 }}

From 692092fa77c2b34fdfa8286a1d999be0f1c08be2 Mon Sep 17 00:00:00 2001
From: Marc Glasser <marc.aaron.glasser@gmail.com>
Date: Thu, 4 Apr 2024 12:40:27 -1000
Subject: [PATCH 2/6] Prettier

---
 src/libs/actions/App.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts
index a89fbde7b32b..0f5376218497 100644
--- a/src/libs/actions/App.ts
+++ b/src/libs/actions/App.ts
@@ -26,12 +26,12 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils';
 import * as SessionUtils from '@libs/SessionUtils';
 import CONST from '@src/CONST';
 import ONYXKEYS from '@src/ONYXKEYS';
+import type {OnyxKey} from '@src/ONYXKEYS';
 import type {Route} from '@src/ROUTES';
 import ROUTES from '@src/ROUTES';
 import type * as OnyxTypes from '@src/types/onyx';
 import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';
 import type {OnyxData} from '@src/types/onyx/Request';
-import type {OnyxKey} from '@src/ONYXKEYS';
 import * as Policy from './Policy';
 import * as Session from './Session';
 import Timing from './Timing';
@@ -93,7 +93,7 @@ const KEYS_TO_PRESERVE: OnyxKey[] = [
     ONYXKEYS.CREDENTIALS,
 ];
 
-let previousResetRequired: boolean|null = false;
+let previousResetRequired: boolean | null = false;
 Onyx.connect({
     key: ONYXKEYS.RESET_REQUIRED,
     callback: (isResetRequired) => {

From 356e38ab3e6cac3839c9ef0aa884d459e7e0d558 Mon Sep 17 00:00:00 2001
From: Marc Glasser <marc.aaron.glasser@gmail.com>
Date: Thu, 4 Apr 2024 13:22:00 -1000
Subject: [PATCH 3/6] Fix logic

---
 src/libs/actions/App.ts | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts
index 0f5376218497..f245d9d20d82 100644
--- a/src/libs/actions/App.ts
+++ b/src/libs/actions/App.ts
@@ -96,21 +96,20 @@ const KEYS_TO_PRESERVE: OnyxKey[] = [
 let previousResetRequired: boolean | null = false;
 Onyx.connect({
     key: ONYXKEYS.RESET_REQUIRED,
+    // eslint-disable-next-line rulesdir/prefer-early-return
     callback: (isResetRequired) => {
-        // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
-        if (previousResetRequired || !isResetRequired) {
-            return;
-        }
+        if (!previousResetRequired && isResetRequired) {
+            Onyx.clear(KEYS_TO_PRESERVE).then(() => {
+                previousResetRequired = false;
 
-        previousResetRequired = isResetRequired;
-        Onyx.clear(KEYS_TO_PRESERVE).then(() => {
-            // Set this to false to reset the flag for this client
-            Onyx.set(ONYXKEYS.RESET_REQUIRED, false);
+                // Set this to false to reset the flag for this client
+                Onyx.set(ONYXKEYS.RESET_REQUIRED, false);
 
-            // eslint-disable-next-line @typescript-eslint/no-use-before-define
-            openApp();
-        });
-    },
+                // eslint-disable-next-line @typescript-eslint/no-use-before-define
+                openApp();
+            });
+        }
+     },
 });
 
 let resolveIsReadyPromise: () => void;

From be7b69a31e8e835ac42ccc82736b3b44d73b6f30 Mon Sep 17 00:00:00 2001
From: Marc Glasser <marc.aaron.glasser@gmail.com>
Date: Thu, 4 Apr 2024 13:24:30 -1000
Subject: [PATCH 4/6] I am being dumb

---
 src/libs/actions/App.ts | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts
index f245d9d20d82..dbe5545320b8 100644
--- a/src/libs/actions/App.ts
+++ b/src/libs/actions/App.ts
@@ -93,22 +93,21 @@ const KEYS_TO_PRESERVE: OnyxKey[] = [
     ONYXKEYS.CREDENTIALS,
 ];
 
-let previousResetRequired: boolean | null = false;
 Onyx.connect({
     key: ONYXKEYS.RESET_REQUIRED,
-    // eslint-disable-next-line rulesdir/prefer-early-return
     callback: (isResetRequired) => {
-        if (!previousResetRequired && isResetRequired) {
-            Onyx.clear(KEYS_TO_PRESERVE).then(() => {
-                previousResetRequired = false;
+        if (!isResetRequired) {
+            return;
+        }
 
-                // Set this to false to reset the flag for this client
-                Onyx.set(ONYXKEYS.RESET_REQUIRED, false);
+        Onyx.clear(KEYS_TO_PRESERVE).then(() => {
 
-                // eslint-disable-next-line @typescript-eslint/no-use-before-define
-                openApp();
-            });
-        }
+            // Set this to false to reset the flag for this client
+            Onyx.set(ONYXKEYS.RESET_REQUIRED, false);
+
+            // eslint-disable-next-line @typescript-eslint/no-use-before-define
+            openApp();
+        });
      },
 });
 

From b38eb55ef732dffbb488ccaae19abd1504144bee Mon Sep 17 00:00:00 2001
From: Marc Glasser <marc.aaron.glasser@gmail.com>
Date: Thu, 4 Apr 2024 13:25:20 -1000
Subject: [PATCH 5/6] style

---
 src/libs/actions/App.ts | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts
index dbe5545320b8..ba2ea750542e 100644
--- a/src/libs/actions/App.ts
+++ b/src/libs/actions/App.ts
@@ -101,14 +101,13 @@ Onyx.connect({
         }
 
         Onyx.clear(KEYS_TO_PRESERVE).then(() => {
-
             // Set this to false to reset the flag for this client
             Onyx.set(ONYXKEYS.RESET_REQUIRED, false);
 
             // eslint-disable-next-line @typescript-eslint/no-use-before-define
             openApp();
         });
-     },
+    },
 });
 
 let resolveIsReadyPromise: () => void;

From 3cd178a5b4af00e408f9bd1300629c6e90f19af1 Mon Sep 17 00:00:00 2001
From: Marc Glasser <marc.aaron.glasser@gmail.com>
Date: Wed, 10 Apr 2024 10:13:21 -1000
Subject: [PATCH 6/6] Update comment

---
 src/ONYXKEYS.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts
index 6e2b1f7cf1ea..d6d08211a79e 100755
--- a/src/ONYXKEYS.ts
+++ b/src/ONYXKEYS.ts
@@ -291,7 +291,7 @@ const ONYXKEYS = {
     /** Indicates whether an forced upgrade is required */
     UPDATE_REQUIRED: 'updateRequired',
 
-    /** Indicates whether an forced reset is required a.k.a. clear Oynx data without signing the user out */
+    /** Indicates whether an forced reset is required. Used in emergency situations where we must completely erase the Onyx data in the client because it is in a bad state. This will clear Oynx data without signing the user out. */
     RESET_REQUIRED: 'resetRequired',
 
     /** Stores the logs of the app for debugging purposes */
