Skip to content

Commit

Permalink
[WorkspaceSwitcher] Handle disabling workspace for native platforms (#31
Browse files Browse the repository at this point in the history
)

* Handle disabling workspace for native platforms

* Adapt new navigation utility function

* Remove redundant comment

* Check if report belongs to workspace

* Change set to merge
  • Loading branch information
mateuuszzzzz authored Jan 24, 2024
1 parent 2e4f00f commit 1c435ca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ function navContainsProtectedRoutes(state: State | undefined): boolean {
return false;
}

const protectedScreensName = Object.values(PROTECTED_SCREENS);
return !protectedScreensName.some((screen) => !state.routeNames?.includes(screen));
// If one protected screen is in the routeNames then other screens are there as well.
return state?.routeNames.includes(PROTECTED_SCREENS.CONCIERGE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import Onyx from 'react-native-onyx';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import getPolicyMemberAccountIDs from '@libs/PolicyMembersUtils';
import {extractPolicyIDFromPath} from '@libs/PolicyUtils';
import {doesReportBelongToWorkspace, getReport} from '@libs/ReportUtils';
import Visibility from '@libs/Visibility';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import backgroundRefresh from './backgroundRefresh';
import PushNotification from './index';

let lastVisitedPath: string | undefined;
Onyx.connect({
key: ONYXKEYS.LAST_VISITED_PATH,
callback: (value) => {
if (!value) {
return;
}
lastVisitedPath = value;
},
});

/**
* Setup reportComment push notification callbacks.
*/
Expand All @@ -22,6 +38,12 @@ export default function subscribeToReportCommentPushNotifications() {
Log.warn('[PushNotification] This push notification has no reportID');
}

const policyID = lastVisitedPath && extractPolicyIDFromPath(lastVisitedPath);
const report = getReport(reportID.toString());
const policyMembersAccountIDs = policyID ? getPolicyMemberAccountIDs(policyID) : [];

const reportBelongsToWorkspace = policyID && !isEmptyObject(report) && doesReportBelongToWorkspace(report, policyID, policyMembersAccountIDs);

Log.info('[PushNotification] onSelected() - called', false, {reportID, reportActionID});
Navigation.isNavigationReady()
.then(Navigation.waitForProtectedRoutes)
Expand All @@ -33,6 +55,9 @@ export default function subscribeToReportCommentPushNotifications() {
}

Log.info('[PushNotification] onSelected() - Navigation is ready. Navigating...', false, {reportID, reportActionID});
if (!reportBelongsToWorkspace) {
Navigation.navigateWithSwitchPolicyID({policyID: undefined, route: ROUTES.HOME});
}
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(String(reportID)));
} catch (error) {
let errorMessage = String(error);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ function handleRestrictedEvent(eventName: string) {
}

function updateLastVisitedPath(path: string) {
Onyx.set(ONYXKEYS.LAST_VISITED_PATH, path);
Onyx.merge(ONYXKEYS.LAST_VISITED_PATH, path);
}

export {
Expand Down
11 changes: 8 additions & 3 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import Navigation from '@libs/Navigation/Navigation';
import LocalNotification from '@libs/Notification/LocalNotification';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import getPolicyMemberAccountIDs from '@libs/PolicyMembersUtils';
import {extractPolicyIDFromPath} from '@libs/PolicyUtils';
import * as Pusher from '@libs/Pusher/pusher';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {doesReportBelongToWorkspace} from '@libs/ReportUtils';
import shouldSkipDeepLinkNavigation from '@libs/shouldSkipDeepLinkNavigation';
import * as UserUtils from '@libs/UserUtils';
import Visibility from '@libs/Visibility';
Expand Down Expand Up @@ -1883,10 +1885,13 @@ function showReportActionNotification(reportID: string, reportAction: ReportActi

const onClick = () => {
const policyID = lastVisitedPath && extractPolicyIDFromPath(lastVisitedPath);
const pathPrefix = `w/${policyID === report.policyID ? policyID : 'global'}/`;
const policyMembersAccountIDs = policyID ? getPolicyMemberAccountIDs(policyID) : [];

// TO DO: Unify workspace-related navigation and extended definition of report that belongs to workspace
Navigation.navigate(`${pathPrefix}${ROUTES.HOME}` as Route);
const reportBelongsToWorkspace = policyID ? doesReportBelongToWorkspace(report, policyID, policyMembersAccountIDs) : true;

if (!reportBelongsToWorkspace) {
Navigation.navigateWithSwitchPolicyID({policyID: undefined, route: ROUTES.HOME});
}
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID));
};

Expand Down

0 comments on commit 1c435ca

Please sign in to comment.