From 8eeaa0c4b1b7c59f34332f3459244252e3ab57e9 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Mon, 23 Sep 2024 09:03:54 +0200 Subject: [PATCH 1/3] prioritize OpenApp when clearing Onyx --- src/libs/API/index.ts | 4 ++-- src/libs/actions/App.ts | 5 ++--- .../settings/Troubleshoot/TroubleshootPage.tsx | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/libs/API/index.ts b/src/libs/API/index.ts index 65fd2b6ad01..be1706886b1 100644 --- a/src/libs/API/index.ts +++ b/src/libs/API/index.ts @@ -116,10 +116,10 @@ function processRequest(request: OnyxRequest, type: ApiRequestType): Promise(command: TCommand, apiCommandParameters: ApiRequestCommandParameters[TCommand], onyxData: OnyxData = {}): void { +function write(command: TCommand, apiCommandParameters: ApiRequestCommandParameters[TCommand], onyxData: OnyxData = {}): Promise { Log.info('[API] Called API write', false, {command, ...apiCommandParameters}); const request = prepareRequest(command, CONST.API_REQUEST_TYPE.WRITE, apiCommandParameters, onyxData); - processRequest(request, CONST.API_REQUEST_TYPE.WRITE); + return processRequest(request, CONST.API_REQUEST_TYPE.WRITE); } /** diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index a0f60752913..6b6f1a5f6dc 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -243,10 +243,9 @@ function getOnyxDataForOpenOrReconnect(isOpenApp = false): OnyxData { * Fetches data needed for app initialization */ function openApp() { - getPolicyParamsForOpenOrReconnect().then((policyParams: PolicyParamsForOpenOrReconnect) => { + return getPolicyParamsForOpenOrReconnect().then((policyParams: PolicyParamsForOpenOrReconnect) => { const params: OpenAppParams = {enablePriorityModeFilter: true, ...policyParams}; - - API.write(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true)); + return API.write(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true)); }); } diff --git a/src/pages/settings/Troubleshoot/TroubleshootPage.tsx b/src/pages/settings/Troubleshoot/TroubleshootPage.tsx index 79f7e45d8cd..15cf2dc8aa5 100644 --- a/src/pages/settings/Troubleshoot/TroubleshootPage.tsx +++ b/src/pages/settings/Troubleshoot/TroubleshootPage.tsx @@ -24,6 +24,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useThemeStyles from '@hooks/useThemeStyles'; import useWaitForNavigation from '@hooks/useWaitForNavigation'; import {setShouldMaskOnyxState} from '@libs/actions/MaskOnyx'; +import * as PersistedRequests from '@libs/actions/PersistedRequests'; import ExportOnyxState from '@libs/ExportOnyxState'; import Navigation from '@libs/Navigation/Navigation'; import * as App from '@userActions/App'; @@ -157,8 +158,20 @@ function TroubleshootPage({shouldStoreLogs, shouldMaskOnyxState}: TroubleshootPa isVisible={isConfirmationModalVisible} onConfirm={() => { setIsConfirmationModalVisible(false); + // Requests in a sequential queue should be called even if the Onyx state is reset, so we do not lose any pending data. + // However, the OpenApp request must be called before any other request in a queue to ensure data consistency. + // To do that, sequential queue is cleared together with other keys, and then it's restored once the OpenApp request is resolved. + const sequentialQueue = PersistedRequests.getAll(); Onyx.clear(App.KEYS_TO_PRESERVE).then(() => { - App.openApp(); + App.openApp().then(() => { + if (!sequentialQueue) { + return; + } + + sequentialQueue.forEach((request) => { + PersistedRequests.save(request); + }); + }); }); }} onCancel={() => setIsConfirmationModalVisible(false)} From b56b580613b597d48413562293b4d8d45a5ab872 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Mon, 23 Sep 2024 11:18:42 +0200 Subject: [PATCH 2/3] migrate from withOnyx to useOnyx --- .../Troubleshoot/TroubleshootPage.tsx | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/pages/settings/Troubleshoot/TroubleshootPage.tsx b/src/pages/settings/Troubleshoot/TroubleshootPage.tsx index 15cf2dc8aa5..d8448667a24 100644 --- a/src/pages/settings/Troubleshoot/TroubleshootPage.tsx +++ b/src/pages/settings/Troubleshoot/TroubleshootPage.tsx @@ -1,6 +1,6 @@ import React, {useCallback, useMemo, useState} from 'react'; import {View} from 'react-native'; -import Onyx, {withOnyx} from 'react-native-onyx'; +import Onyx, {useOnyx, withOnyx} from 'react-native-onyx'; import type {OnyxEntry} from 'react-native-onyx'; import type {SvgProps} from 'react-native-svg'; import ClientSideLoggingToolMenu from '@components/ClientSideLoggingToolMenu'; @@ -40,14 +40,9 @@ type BaseMenuItem = { action: () => void | Promise; }; -type TroubleshootPageOnyxProps = { - shouldStoreLogs: OnyxEntry; - shouldMaskOnyxState: boolean; -}; - -type TroubleshootPageProps = TroubleshootPageOnyxProps; - -function TroubleshootPage({shouldStoreLogs, shouldMaskOnyxState}: TroubleshootPageProps) { +function TroubleshootPage() { + const [shouldStoreLogs] = useOnyx(ONYXKEYS.SHOULD_STORE_LOGS); + const [shouldMaskOnyxState] = useOnyx(ONYXKEYS.SHOULD_MASK_ONYX_STATE); const {translate} = useLocalize(); const styles = useThemeStyles(); const {isProduction} = useEnvironment(); @@ -139,7 +134,7 @@ function TroubleshootPage({shouldStoreLogs, shouldMaskOnyxState}: TroubleshootPa @@ -189,12 +184,4 @@ function TroubleshootPage({shouldStoreLogs, shouldMaskOnyxState}: TroubleshootPa TroubleshootPage.displayName = 'TroubleshootPage'; -export default withOnyx({ - shouldStoreLogs: { - key: ONYXKEYS.SHOULD_STORE_LOGS, - }, - shouldMaskOnyxState: { - key: ONYXKEYS.SHOULD_MASK_ONYX_STATE, - selector: (shouldMaskOnyxState) => shouldMaskOnyxState ?? true, - }, -})(TroubleshootPage); +export default TroubleshootPage; From c7c6c45b6b7054067d588e5393bf088ef6c88d09 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Mon, 23 Sep 2024 11:46:21 +0200 Subject: [PATCH 3/3] fix linter --- src/pages/settings/Troubleshoot/TroubleshootPage.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/settings/Troubleshoot/TroubleshootPage.tsx b/src/pages/settings/Troubleshoot/TroubleshootPage.tsx index d8448667a24..e9f5e7ca7b3 100644 --- a/src/pages/settings/Troubleshoot/TroubleshootPage.tsx +++ b/src/pages/settings/Troubleshoot/TroubleshootPage.tsx @@ -1,7 +1,6 @@ import React, {useCallback, useMemo, useState} from 'react'; import {View} from 'react-native'; -import Onyx, {useOnyx, withOnyx} from 'react-native-onyx'; -import type {OnyxEntry} from 'react-native-onyx'; +import Onyx, {useOnyx} from 'react-native-onyx'; import type {SvgProps} from 'react-native-svg'; import ClientSideLoggingToolMenu from '@components/ClientSideLoggingToolMenu'; import ConfirmModal from '@components/ConfirmModal';