From 5f96fb63323403b7eb733a0b9a6f5a3d6b9ea048 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Fri, 29 Nov 2024 12:46:21 +0100 Subject: [PATCH 1/2] Switch to global workspace on ReportScreen when current policy is deleted --- .../ActiveWorkspaceProvider/index.tsx | 35 ++++--------------- .../SidebarScreen/BaseSidebarScreen.tsx | 34 +++++++++++++----- src/pages/workspace/WorkspaceProfilePage.tsx | 8 +---- src/pages/workspace/WorkspacesListPage.tsx | 8 ----- 4 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/components/ActiveWorkspaceProvider/index.tsx b/src/components/ActiveWorkspaceProvider/index.tsx index b9e5f34d5389..8bb4f043a07e 100644 --- a/src/components/ActiveWorkspaceProvider/index.tsx +++ b/src/components/ActiveWorkspaceProvider/index.tsx @@ -1,41 +1,18 @@ import {useNavigationState} from '@react-navigation/native'; import React, {useEffect, useMemo, useState} from 'react'; import ActiveWorkspaceContext from '@components/ActiveWorkspace/ActiveWorkspaceContext'; -import * as SearchQueryUtils from '@libs/SearchQueryUtils'; -import NAVIGATORS from '@src/NAVIGATORS'; -import SCREENS from '@src/SCREENS'; +import getPolicyIDFromState from '@libs/Navigation/getPolicyIDFromState'; +import type {RootStackParamList, State} from '@libs/Navigation/types'; import type ChildrenProps from '@src/types/utils/ChildrenProps'; function ActiveWorkspaceContextProvider({children}: ChildrenProps) { - const [activeWorkspaceID, setActiveWorkspaceID] = useState(undefined); + const policyID = useNavigationState((state) => getPolicyIDFromState(state as State)); - const lastPolicyRoute = useNavigationState((state) => - state?.routes?.findLast((route) => route.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR || route.name === SCREENS.SEARCH.CENTRAL_PANE), - ); - - const policyIDFromRouteParam = lastPolicyRoute?.params && 'policyID' in lastPolicyRoute.params ? (lastPolicyRoute?.params?.policyID as string) : ''; - const queryFromRouteParam = lastPolicyRoute?.params && 'q' in lastPolicyRoute.params ? (lastPolicyRoute.params.q as string) : ''; + const [activeWorkspaceID, setActiveWorkspaceID] = useState(policyID); useEffect(() => { - if (policyIDFromRouteParam) { - setActiveWorkspaceID(policyIDFromRouteParam); - return; - } - - if (!queryFromRouteParam) { - setActiveWorkspaceID(undefined); - return; - } - - const queryJSON = SearchQueryUtils.buildSearchQueryJSON(queryFromRouteParam); - - if (!queryJSON) { - setActiveWorkspaceID(undefined); - return; - } - - setActiveWorkspaceID(SearchQueryUtils.getPolicyIDFromSearchQuery(queryJSON)); - }, [policyIDFromRouteParam, queryFromRouteParam, setActiveWorkspaceID]); + setActiveWorkspaceID(policyID); + }, [policyID, setActiveWorkspaceID]); const value = useMemo( () => ({ diff --git a/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx b/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx index dcb91b6c0157..23633f2bb0ec 100644 --- a/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx +++ b/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx @@ -1,3 +1,4 @@ +import {useRoute} from '@react-navigation/native'; import React, {useEffect} from 'react'; import {View} from 'react-native'; import {useOnyx} from 'react-native-onyx'; @@ -9,10 +10,14 @@ import useThemeStyles from '@hooks/useThemeStyles'; import * as Browser from '@libs/Browser'; import BottomTabBar from '@libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar'; import TopBar from '@libs/Navigation/AppNavigator/createCustomBottomTabNavigator/TopBar'; +import {getPreservedSplitNavigatorState} from '@libs/Navigation/AppNavigator/createSplitStackNavigator/usePreserveSplitNavigatorState'; +import createSplitNavigator from '@libs/Navigation/linkingConfig/createSplitNavigator'; +import navigationRef from '@libs/Navigation/navigationRef'; import Performance from '@libs/Performance'; import SidebarLinksData from '@pages/home/sidebar/SidebarLinksData'; import Timing from '@userActions/Timing'; import CONST from '@src/CONST'; +import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; import SCREENS from '@src/SCREENS'; @@ -29,21 +34,34 @@ function BaseSidebarScreen() { const {activeWorkspaceID} = useActiveWorkspace(); const {translate} = useLocalize(); const {shouldUseNarrowLayout} = useResponsiveLayout(); - // const [activeWorkspace] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activeWorkspaceID ?? -1}`); + const [activeWorkspace] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activeWorkspaceID ?? -1}`); + const currentRoute = useRoute(); useEffect(() => { Performance.markStart(CONST.TIMING.SIDEBAR_LOADED); Timing.start(CONST.TIMING.SIDEBAR_LOADED); }, []); - // useEffect(() => { - // if (!!activeWorkspace || activeWorkspaceID === undefined) { - // return; - // } + useEffect(() => { + if (!!activeWorkspace || activeWorkspaceID === undefined) { + return; + } + + const topmostReport = navigationRef.getRootState()?.routes.findLast((route) => route.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR); + + if (!topmostReport) { + return; + } - // Navigation.switchPolicyID({policyID: undefined}); - // updateLastAccessedWorkspace(undefined); - // }, [activeWorkspace, activeWorkspaceID]); + const topmostReportState = topmostReport?.state ?? getPreservedSplitNavigatorState(topmostReport?.key); + if (topmostReportState?.routes.some((route) => currentRoute.key === route.key)) { + navigationRef.current?.dispatch({ + target: navigationRef.current.getRootState().key, + payload: createSplitNavigator({name: SCREENS.HOME}, {name: SCREENS.REPORT}), + type: 'REPLACE', + }); + } + }, [activeWorkspace, activeWorkspaceID, currentRoute]); const shouldDisplaySearch = shouldUseNarrowLayout; diff --git a/src/pages/workspace/WorkspaceProfilePage.tsx b/src/pages/workspace/WorkspaceProfilePage.tsx index 823c100893d5..f9b670c1bb4b 100644 --- a/src/pages/workspace/WorkspaceProfilePage.tsx +++ b/src/pages/workspace/WorkspaceProfilePage.tsx @@ -44,7 +44,6 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, route}: Workspac const {translate} = useLocalize(); const {shouldUseNarrowLayout} = useResponsiveLayout(); const illustrations = useThemeIllustrations(); - const activeWorkspaceID = route.params.policyID; const {canUseSpotnanaTravel} = usePermissions(); const [currencyList = {}] = useOnyx(ONYXKEYS.CURRENCY_LIST); @@ -132,12 +131,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, route}: Workspac Policy.deleteWorkspace(policy?.id, policyName); setIsDeleteModalOpen(false); - - // If the workspace being deleted is the active workspace, switch to the "All Workspaces" view - if (activeWorkspaceID === policy?.id) { - Navigation.switchPolicyID(undefined); - } - }, [policy?.id, policyName, activeWorkspaceID]); + }, [policy?.id, policyName]); return ( (); const [policyNameToDelete, setPolicyNameToDelete] = useState(); @@ -135,11 +132,6 @@ function WorkspacesListPage() { Policy.deleteWorkspace(policyIDToDelete, policyNameToDelete); setIsDeleteModalOpen(false); - - // If the workspace being deleted is the active workspace, switch to the "All Workspaces" view - if (activeWorkspaceID === policyIDToDelete) { - Navigation.switchPolicyID(undefined); - } }; /** From 60d4895c964a83d96cf99ebb2c845725d8a235b8 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Fri, 29 Nov 2024 14:52:37 +0100 Subject: [PATCH 2/2] Remove Navigation.switchPolicyID --- src/libs/Navigation/Navigation.ts | 5 ----- src/pages/WorkspaceSwitcherPage/index.tsx | 4 ++-- src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx | 7 +++++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index f816085f5325..31b6f139041d 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -415,10 +415,6 @@ function waitForProtectedRoutes() { }); } -function switchPolicyID(policyID?: string) { - navigationRef.dispatch({type: CONST.NAVIGATION.ACTION_TYPE.SWITCH_POLICY_ID, payload: {policyID}}); -} - type NavigateToReportWithPolicyCheckPayload = {report?: OnyxEntry; reportID?: string; reportActionID?: string; referrer?: string; policyIDToCheck?: string}; function navigateToReportWithPolicyCheck({report, reportID, reportActionID, referrer, policyIDToCheck}: NavigateToReportWithPolicyCheckPayload, ref = navigationRef) { @@ -499,7 +495,6 @@ export default { getTopmostReportActionId, waitForProtectedRoutes, parseHybridAppUrl, - switchPolicyID, resetToHome, closeRHPFlow, setNavigationActionToMicrotaskQueue, diff --git a/src/pages/WorkspaceSwitcherPage/index.tsx b/src/pages/WorkspaceSwitcherPage/index.tsx index cae6024d4f7b..97d459522e97 100644 --- a/src/pages/WorkspaceSwitcherPage/index.tsx +++ b/src/pages/WorkspaceSwitcherPage/index.tsx @@ -14,7 +14,7 @@ import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; -import Navigation from '@libs/Navigation/Navigation'; +import Navigation, {navigationRef} from '@libs/Navigation/Navigation'; import * as PolicyUtils from '@libs/PolicyUtils'; import {sortWorkspacesBySelected} from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; @@ -92,7 +92,7 @@ function WorkspaceSwitcherPage() { Navigation.goBack(); if (policyID !== activeWorkspaceID) { - Navigation.switchPolicyID(policyID); + navigationRef.dispatch({type: CONST.NAVIGATION.ACTION_TYPE.SWITCH_POLICY_ID, payload: {policyID}}); } }, [activeWorkspaceID], diff --git a/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx b/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx index 23633f2bb0ec..15b7b35ce889 100644 --- a/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx +++ b/src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx @@ -42,8 +42,11 @@ function BaseSidebarScreen() { Timing.start(CONST.TIMING.SIDEBAR_LOADED); }, []); + // If the selected workspace has been deleted, the current workspace is reset to global. useEffect(() => { - if (!!activeWorkspace || activeWorkspaceID === undefined) { + const isActiveWorkspaceDeleted = !!activeWorkspace || activeWorkspaceID === undefined; + + if (isActiveWorkspaceDeleted) { return; } @@ -58,7 +61,7 @@ function BaseSidebarScreen() { navigationRef.current?.dispatch({ target: navigationRef.current.getRootState().key, payload: createSplitNavigator({name: SCREENS.HOME}, {name: SCREENS.REPORT}), - type: 'REPLACE', + type: CONST.NAVIGATION.ACTION_TYPE.REPLACE, }); } }, [activeWorkspace, activeWorkspaceID, currentRoute]);