From a885e274c5b10200281bec87f929bcadfff60a0a Mon Sep 17 00:00:00 2001 From: dominictb Date: Thu, 27 Jun 2024 11:53:57 +0700 Subject: [PATCH 1/6] fix: return to report screen if RHP has valid reportID params Signed-off-by: dominictb --- .../linkingConfig/getAdaptedStateFromPath.ts | 11 +++++++++++ src/libs/ReportUtils.ts | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts index 17ea0e17d1b9..33376b777f32 100644 --- a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts +++ b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts @@ -6,6 +6,7 @@ import getIsNarrowLayout from '@libs/getIsNarrowLayout'; import type {BottomTabName, CentralPaneName, FullScreenName, NavigationPartialRoute, RootStackParamList} from '@libs/Navigation/types'; import isCentralPaneName from '@libs/NavigationUtils'; import {extractPolicyIDFromPath, getPathWithoutPolicyID} from '@libs/PolicyUtils'; +import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import SCREENS from '@src/SCREENS'; @@ -139,6 +140,16 @@ function getMatchingRootRouteForRHPRoute(route: NavigationPartialRoute): Navigat return createFullScreenNavigator({name: fullScreenName as FullScreenName, params: route.params}); } } + + // check for valid reportID in the route params + // if the reportID is valid, we should navigate back to screen report in CPN + const reportID = (route.params as Record)?.['reportID']; + if (ReportUtils.doesReportExist(reportID)) { + return { + name: SCREENS.REPORT, + params: {reportID}, + }; + } } function getAdaptedState(state: PartialState>, policyID?: string): GetAdaptedStateReturnType { diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index cca52ac37c9d..c6cb952610d7 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -603,6 +603,15 @@ function isDraftReport(reportID: string | undefined): boolean { return !!draftReport; } +/** + * Check if a report exists with a report ID + * @param reportID + */ +function doesReportExist(reportID: string | undefined): boolean { + const report = getReportOrDraftReport(reportID); + return isEmptyObject(report?.reportID) ? false : true; +} + /** * Returns the parentReport if the given report is a thread */ @@ -7304,6 +7313,7 @@ export { shouldShowMerchantColumn, isCurrentUserInvoiceReceiver, isDraftReport, + doesReportExist, changeMoneyRequestHoldStatus, createDraftWorkspaceAndNavigateToConfirmationScreen, isChatUsedForOnboarding, From 52557fcaebdbf2f7301d22010478db64a3ba57b7 Mon Sep 17 00:00:00 2001 From: dominictb Date: Thu, 27 Jun 2024 12:40:37 +0700 Subject: [PATCH 2/6] fix: lint Signed-off-by: dominictb fix: prettier Signed-off-by: dominictb --- src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts | 2 +- src/libs/ReportUtils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts index 33376b777f32..5b2f56a9536c 100644 --- a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts +++ b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts @@ -143,7 +143,7 @@ function getMatchingRootRouteForRHPRoute(route: NavigationPartialRoute): Navigat // check for valid reportID in the route params // if the reportID is valid, we should navigate back to screen report in CPN - const reportID = (route.params as Record)?.['reportID']; + const reportID = (route.params as Record)?.reportID; if (ReportUtils.doesReportExist(reportID)) { return { name: SCREENS.REPORT, diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index c6cb952610d7..171738c843d4 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -609,7 +609,7 @@ function isDraftReport(reportID: string | undefined): boolean { */ function doesReportExist(reportID: string | undefined): boolean { const report = getReportOrDraftReport(reportID); - return isEmptyObject(report?.reportID) ? false : true; + return !isEmptyObject(report?.reportID); } /** From a01bd92a57075ab532525ab9cdccd666ae6a4592 Mon Sep 17 00:00:00 2001 From: dominictb Date: Tue, 2 Jul 2024 09:24:16 +0700 Subject: [PATCH 3/6] fix: remove doesReportExist fn in ReportUtils --- .../linkingConfig/getAdaptedStateFromPath.ts | 18 ++++++++++++------ src/libs/ReportUtils.ts | 10 ---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts index 5b2f56a9536c..205252d0930b 100644 --- a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts +++ b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts @@ -1,15 +1,17 @@ import type {NavigationState, PartialState, Route} from '@react-navigation/native'; import {findFocusedRoute, getStateFromPath} from '@react-navigation/native'; +import Onyx, {OnyxCollection} from 'react-native-onyx'; import type {TupleToUnion} from 'type-fest'; import {isAnonymousUser} from '@libs/actions/Session'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; import type {BottomTabName, CentralPaneName, FullScreenName, NavigationPartialRoute, RootStackParamList} from '@libs/Navigation/types'; import isCentralPaneName from '@libs/NavigationUtils'; import {extractPolicyIDFromPath, getPathWithoutPolicyID} from '@libs/PolicyUtils'; -import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; +import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; +import {Report} from '@src/types/onyx'; import CENTRAL_PANE_TO_RHP_MAPPING from './CENTRAL_PANE_TO_RHP_MAPPING'; import config from './config'; import extractPolicyIDsFromState from './extractPolicyIDsFromState'; @@ -38,6 +40,13 @@ type GetAdaptedStateReturnType = { type GetAdaptedStateFromPath = (...args: Parameters) => GetAdaptedStateReturnType; +let allReports: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => (allReports = value), +}); + // The function getPathFromState that we are using in some places isn't working correctly without defined index. const getRoutesWithIndex = (routes: NavigationPartialRoute[]): PartialState => ({routes, index: routes.length - 1}); @@ -144,11 +153,8 @@ function getMatchingRootRouteForRHPRoute(route: NavigationPartialRoute): Navigat // check for valid reportID in the route params // if the reportID is valid, we should navigate back to screen report in CPN const reportID = (route.params as Record)?.reportID; - if (ReportUtils.doesReportExist(reportID)) { - return { - name: SCREENS.REPORT, - params: {reportID}, - }; + if (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]) { + return {name: SCREENS.REPORT, params: {reportID}}; } } diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 171738c843d4..cca52ac37c9d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -603,15 +603,6 @@ function isDraftReport(reportID: string | undefined): boolean { return !!draftReport; } -/** - * Check if a report exists with a report ID - * @param reportID - */ -function doesReportExist(reportID: string | undefined): boolean { - const report = getReportOrDraftReport(reportID); - return !isEmptyObject(report?.reportID); -} - /** * Returns the parentReport if the given report is a thread */ @@ -7313,7 +7304,6 @@ export { shouldShowMerchantColumn, isCurrentUserInvoiceReceiver, isDraftReport, - doesReportExist, changeMoneyRequestHoldStatus, createDraftWorkspaceAndNavigateToConfirmationScreen, isChatUsedForOnboarding, From 467bccf0b49130cdeb43ff83b23a73247a716863 Mon Sep 17 00:00:00 2001 From: dominictb Date: Tue, 2 Jul 2024 14:53:52 +0700 Subject: [PATCH 4/6] fix: lint issue --- src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts index 205252d0930b..25281a468003 100644 --- a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts +++ b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts @@ -1,6 +1,7 @@ import type {NavigationState, PartialState, Route} from '@react-navigation/native'; import {findFocusedRoute, getStateFromPath} from '@react-navigation/native'; -import Onyx, {OnyxCollection} from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; +import Onyx from 'react-native-onyx'; import type {TupleToUnion} from 'type-fest'; import {isAnonymousUser} from '@libs/actions/Session'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; @@ -11,7 +12,7 @@ import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; -import {Report} from '@src/types/onyx'; +import type {Report} from '@src/types/onyx'; import CENTRAL_PANE_TO_RHP_MAPPING from './CENTRAL_PANE_TO_RHP_MAPPING'; import config from './config'; import extractPolicyIDsFromState from './extractPolicyIDsFromState'; From 3d96978942d9fd8846cdfd1d58932ee67efd18a9 Mon Sep 17 00:00:00 2001 From: dominictb Date: Tue, 9 Jul 2024 16:46:38 +0700 Subject: [PATCH 5/6] fix: use ReportConnection func Signed-off-by: dominictb --- .../linkingConfig/getAdaptedStateFromPath.ts | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts index 0d27380e6511..7bdbf7cd5649 100644 --- a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts +++ b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts @@ -1,22 +1,20 @@ -import type {NavigationState, PartialState, Route} from '@react-navigation/native'; -import {findFocusedRoute, getStateFromPath} from '@react-navigation/native'; -import type {OnyxCollection} from 'react-native-onyx'; -import Onyx from 'react-native-onyx'; -import type {TupleToUnion} from 'type-fest'; -import {isAnonymousUser} from '@libs/actions/Session'; +import type { BottomTabName, CentralPaneName, FullScreenName, NavigationPartialRoute, RootStackParamList } from '@libs/Navigation/types'; +import { isCentralPaneName } from '@libs/NavigationUtils'; +import { extractPolicyIDFromPath, getPathWithoutPolicyID } from '@libs/PolicyUtils'; +import { isAnonymousUser } from '@libs/actions/Session'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; -import type {BottomTabName, CentralPaneName, FullScreenName, NavigationPartialRoute, RootStackParamList} from '@libs/Navigation/types'; -import {isCentralPaneName} from '@libs/NavigationUtils'; -import {extractPolicyIDFromPath, getPathWithoutPolicyID} from '@libs/PolicyUtils'; +import type { NavigationState, PartialState, Route } from '@react-navigation/native'; +import { findFocusedRoute, getStateFromPath } from '@react-navigation/native'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; -import type {Report} from '@src/types/onyx'; +import * as ReportConnection from '@libs/ReportConnection'; +import type { TupleToUnion } from 'type-fest'; import CENTRAL_PANE_TO_RHP_MAPPING from './CENTRAL_PANE_TO_RHP_MAPPING'; +import FULL_SCREEN_TO_RHP_MAPPING from './FULL_SCREEN_TO_RHP_MAPPING'; import config from './config'; import extractPolicyIDsFromState from './extractPolicyIDsFromState'; -import FULL_SCREEN_TO_RHP_MAPPING from './FULL_SCREEN_TO_RHP_MAPPING'; import getMatchingBottomTabRouteForState from './getMatchingBottomTabRouteForState'; import getMatchingCentralPaneRouteForState from './getMatchingCentralPaneRouteForState'; import replacePathInNestedState from './replacePathInNestedState'; @@ -41,13 +39,6 @@ type GetAdaptedStateReturnType = { type GetAdaptedStateFromPath = (...args: Parameters) => GetAdaptedStateReturnType; -let allReports: OnyxCollection; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (value) => (allReports = value), -}); - // The function getPathFromState that we are using in some places isn't working correctly without defined index. const getRoutesWithIndex = (routes: NavigationPartialRoute[]): PartialState => ({routes, index: routes.length - 1}); @@ -154,7 +145,7 @@ function getMatchingRootRouteForRHPRoute(route: NavigationPartialRoute): Navigat // check for valid reportID in the route params // if the reportID is valid, we should navigate back to screen report in CPN const reportID = (route.params as Record)?.reportID; - if (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]) { + if (ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]) { return {name: SCREENS.REPORT, params: {reportID}}; } } @@ -390,4 +381,5 @@ const getAdaptedStateFromPath: GetAdaptedStateFromPath = (path, options) => { }; export default getAdaptedStateFromPath; -export type {Metainfo}; +export type { Metainfo }; + From f20710dd89d443802166bfe3fa9a98574bc42b00 Mon Sep 17 00:00:00 2001 From: dominictb Date: Tue, 9 Jul 2024 16:52:13 +0700 Subject: [PATCH 6/6] fix: prettier Signed-off-by: dominictb --- .../linkingConfig/getAdaptedStateFromPath.ts | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts index 7bdbf7cd5649..547b766e1ce5 100644 --- a/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts +++ b/src/libs/Navigation/linkingConfig/getAdaptedStateFromPath.ts @@ -1,20 +1,20 @@ -import type { BottomTabName, CentralPaneName, FullScreenName, NavigationPartialRoute, RootStackParamList } from '@libs/Navigation/types'; -import { isCentralPaneName } from '@libs/NavigationUtils'; -import { extractPolicyIDFromPath, getPathWithoutPolicyID } from '@libs/PolicyUtils'; -import { isAnonymousUser } from '@libs/actions/Session'; +import type {NavigationState, PartialState, Route} from '@react-navigation/native'; +import {findFocusedRoute, getStateFromPath} from '@react-navigation/native'; +import type {TupleToUnion} from 'type-fest'; +import {isAnonymousUser} from '@libs/actions/Session'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; -import type { NavigationState, PartialState, Route } from '@react-navigation/native'; -import { findFocusedRoute, getStateFromPath } from '@react-navigation/native'; +import type {BottomTabName, CentralPaneName, FullScreenName, NavigationPartialRoute, RootStackParamList} from '@libs/Navigation/types'; +import {isCentralPaneName} from '@libs/NavigationUtils'; +import {extractPolicyIDFromPath, getPathWithoutPolicyID} from '@libs/PolicyUtils'; +import * as ReportConnection from '@libs/ReportConnection'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; -import * as ReportConnection from '@libs/ReportConnection'; -import type { TupleToUnion } from 'type-fest'; import CENTRAL_PANE_TO_RHP_MAPPING from './CENTRAL_PANE_TO_RHP_MAPPING'; -import FULL_SCREEN_TO_RHP_MAPPING from './FULL_SCREEN_TO_RHP_MAPPING'; import config from './config'; import extractPolicyIDsFromState from './extractPolicyIDsFromState'; +import FULL_SCREEN_TO_RHP_MAPPING from './FULL_SCREEN_TO_RHP_MAPPING'; import getMatchingBottomTabRouteForState from './getMatchingBottomTabRouteForState'; import getMatchingCentralPaneRouteForState from './getMatchingCentralPaneRouteForState'; import replacePathInNestedState from './replacePathInNestedState'; @@ -381,5 +381,4 @@ const getAdaptedStateFromPath: GetAdaptedStateFromPath = (path, options) => { }; export default getAdaptedStateFromPath; -export type { Metainfo }; - +export type {Metainfo};