Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to global workspace on ReportScreen when current policy is del… #146

Draft
wants to merge 2 commits into
base: poc/split-navs
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions src/components/ActiveWorkspaceProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -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<string | undefined>(undefined);
const policyID = useNavigationState((state) => getPolicyIDFromState(state as State<RootStackParamList>));

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<string | undefined>(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(
() => ({
Expand Down
5 changes: 0 additions & 5 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Report>; reportID?: string; reportActionID?: string; referrer?: string; policyIDToCheck?: string};

function navigateToReportWithPolicyCheck({report, reportID, reportActionID, referrer, policyIDToCheck}: NavigateToReportWithPolicyCheckPayload, ref = navigationRef) {
Expand Down Expand Up @@ -499,7 +495,6 @@ export default {
getTopmostReportActionId,
waitForProtectedRoutes,
parseHybridAppUrl,
switchPolicyID,
resetToHome,
closeRHPFlow,
setNavigationActionToMicrotaskQueue,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/WorkspaceSwitcherPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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],
Expand Down
37 changes: 29 additions & 8 deletions src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

Expand All @@ -29,21 +34,37 @@ 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;
// }
// If the selected workspace has been deleted, the current workspace is reset to global.
useEffect(() => {
const isActiveWorkspaceDeleted = !!activeWorkspace || activeWorkspaceID === undefined;

if (isActiveWorkspaceDeleted) {
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: CONST.NAVIGATION.ACTION_TYPE.REPLACE,
});
}
}, [activeWorkspace, activeWorkspaceID, currentRoute]);
Comment on lines +45 to +67

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this to a hook with some meaningful name. Also, can we put it in the reportsSplitNavigator? It seems that it's logic is more connected to the navigator than to baseSidebarScreen


const shouldDisplaySearch = shouldUseNarrowLayout;

Expand Down
8 changes: 1 addition & 7 deletions src/pages/workspace/WorkspaceProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 (
<WorkspacePageWithSections
Expand Down
8 changes: 0 additions & 8 deletions src/pages/workspace/WorkspacesListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {PressableWithoutFeedback} from '@components/Pressable';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
Expand Down Expand Up @@ -115,8 +114,6 @@ function WorkspacesListPage() {
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [session] = useOnyx(ONYXKEYS.SESSION);

const {activeWorkspaceID} = useActiveWorkspace();

const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [policyIDToDelete, setPolicyIDToDelete] = useState<string>();
const [policyNameToDelete, setPolicyNameToDelete] = useState<string>();
Expand All @@ -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);
}
};

/**
Expand Down
Loading