From dbfb08cd2c59b1c2b255f7af7ddb1d2d5299acb1 Mon Sep 17 00:00:00 2001 From: daledah Date: Mon, 2 Sep 2024 14:41:05 +0700 Subject: [PATCH 1/3] fix: app crash when clicking on not found policy --- src/pages/workspace/accounting/AccountingContext.tsx | 5 +++-- src/pages/workspace/accounting/PolicyAccountingPage.tsx | 4 ++-- src/pages/workspace/accounting/types.ts | 3 +-- src/pages/workspace/withPolicy.tsx | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/pages/workspace/accounting/AccountingContext.tsx b/src/pages/workspace/accounting/AccountingContext.tsx index ce5ac90fd2f1..ff7972e38008 100644 --- a/src/pages/workspace/accounting/AccountingContext.tsx +++ b/src/pages/workspace/accounting/AccountingContext.tsx @@ -1,6 +1,7 @@ import type {MutableRefObject, RefObject} from 'react'; import React, {useContext, useMemo, useRef, useState} from 'react'; import type {View} from 'react-native'; +import type {OnyxEntry} from 'react-native-onyx'; import AccountingConnectionConfirmationModal from '@components/AccountingConnectionConfirmationModal'; import useLocalize from '@hooks/useLocalize'; import {removePolicyConnection} from '@libs/actions/connections'; @@ -47,14 +48,14 @@ const defaultAccountingContext = { const AccountingContext = React.createContext(defaultAccountingContext); type AccountingContextProviderProps = ChildrenProps & { - policy: Policy; + policy: OnyxEntry; }; function AccountingContextProvider({children, policy}: AccountingContextProviderProps) { const popoverAnchorRefs = useRef>>(defaultAccountingContext.popoverAnchorRefs.current); const [activeIntegration, setActiveIntegration] = useState(); const {translate} = useLocalize(); - const policyID = policy.id; + const policyID = policy?.id ?? '-1'; const startIntegrationFlow = React.useCallback( (newActiveIntegration: ActiveIntegration) => { diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index 11c47a21f2e1..e47e0114517e 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -48,7 +48,7 @@ import type {MenuItemData, PolicyAccountingPageProps} from './types'; import {getAccountingIntegrationData} from './utils'; function PolicyAccountingPage({policy}: PolicyAccountingPageProps) { - const [connectionSyncProgress] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS}${policy.id}`); + const [connectionSyncProgress] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS}${policy?.id}`); const theme = useTheme(); const styles = useThemeStyles(); const {translate, datetimeToRelative: getDatetimeToRelative} = useLocalize(); @@ -65,7 +65,7 @@ function PolicyAccountingPage({policy}: PolicyAccountingPageProps) { const lastSyncProgressDate = parseISO(connectionSyncProgress?.timestamp ?? ''); const isSyncInProgress = !!connectionSyncProgress?.stageInProgress && - (connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE || !policy.connections?.[connectionSyncProgress.connectionName]) && + (connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE || !policy?.connections?.[connectionSyncProgress.connectionName]) && isValid(lastSyncProgressDate) && differenceInMinutes(new Date(), lastSyncProgressDate) < CONST.POLICY.CONNECTIONS.SYNC_STAGE_TIMEOUT_MINUTES; diff --git a/src/pages/workspace/accounting/types.ts b/src/pages/workspace/accounting/types.ts index ed4f11312f2a..4ff7728e5575 100644 --- a/src/pages/workspace/accounting/types.ts +++ b/src/pages/workspace/accounting/types.ts @@ -14,8 +14,7 @@ type PolicyAccountingPageOnyxProps = { type PolicyAccountingPageProps = WithPolicyConnectionsProps & PolicyAccountingPageOnyxProps & { - // This is not using OnyxEntry because the HOC withPolicyConnections will only render this component if there is a policy - policy: Policy; + policy: OnyxEntry; }; type WorkspaceUpgradeNavigationDetails = { diff --git a/src/pages/workspace/withPolicy.tsx b/src/pages/workspace/withPolicy.tsx index b6da4dd689e6..f006d990816d 100644 --- a/src/pages/workspace/withPolicy.tsx +++ b/src/pages/workspace/withPolicy.tsx @@ -76,7 +76,7 @@ export default function (WrappedComponent: const currentRoute = routes?.at(-1); const policyID = getPolicyIDFromRoute(currentRoute as PolicyRoute); - if (policyID.length > 0) { + if (policyID.length > 0 && props.policy) { Policy.updateLastAccessedWorkspace(policyID); } @@ -84,6 +84,7 @@ export default function (WrappedComponent: ); From 5b3bb78fa444590b7c194143979204d8386f9f01 Mon Sep 17 00:00:00 2001 From: daledah Date: Tue, 3 Sep 2024 12:22:17 +0700 Subject: [PATCH 2/3] fix: remove unnecessary nullish coalescing --- src/pages/workspace/withPolicy.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/workspace/withPolicy.tsx b/src/pages/workspace/withPolicy.tsx index f006d990816d..1f50f0cd23b8 100644 --- a/src/pages/workspace/withPolicy.tsx +++ b/src/pages/workspace/withPolicy.tsx @@ -84,7 +84,6 @@ export default function (WrappedComponent: ); From bbbf9156929053c9fbcafbb92fe4f1172df2e7e6 Mon Sep 17 00:00:00 2001 From: daledah Date: Wed, 4 Sep 2024 12:25:41 +0700 Subject: [PATCH 3/3] fix: remove unnecessary condition --- src/pages/workspace/withPolicy.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/workspace/withPolicy.tsx b/src/pages/workspace/withPolicy.tsx index 97d19e9cacee..db55db2514ef 100644 --- a/src/pages/workspace/withPolicy.tsx +++ b/src/pages/workspace/withPolicy.tsx @@ -77,7 +77,7 @@ export default function (WrappedComponent: const currentRoute = routes?.at(-1); const policyID = getPolicyIDFromRoute(currentRoute as PolicyRoute); - if (policyID.length > 0 && props.policy) { + if (policyID.length > 0) { Policy.updateLastAccessedWorkspace(policyID); }