diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 0d1eceb4d711..3454a6847847 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -3,7 +3,6 @@ import Onyx, {withOnyx} from 'react-native-onyx'; import moment from 'moment'; import _ from 'underscore'; import lodashGet from 'lodash/get'; -import PropTypes from 'prop-types'; import * as StyleUtils from '../../../styles/StyleUtils'; import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions'; import CONST from '../../../CONST'; @@ -87,9 +86,6 @@ const modalScreenListeners = { const propTypes = { ...windowDimensionsPropTypes, - - /** The current path as reported by the NavigationContainer */ - currentPath: PropTypes.string.isRequired, }; class AuthScreens extends React.Component { @@ -116,7 +112,7 @@ class AuthScreens extends React.Component { // Listen for report changes and fetch some data we need on initialization UnreadIndicatorUpdater.listenForReportChanges(); App.openApp(); - + App.setUpPoliciesAndNavigate(this.props.session); Timing.end(CONST.TIMING.HOMEPAGE_INITIAL_RENDER); const searchShortcutConfig = CONST.KEYBOARD_SHORTCUTS.SEARCH; @@ -134,11 +130,6 @@ class AuthScreens extends React.Component { } shouldComponentUpdate(nextProps) { - // we perform this check here instead of componentDidUpdate to skip an unnecessary re-render - if (this.props.currentPath !== nextProps.currentPath) { - App.setUpPoliciesAndNavigate(nextProps.session, nextProps.currentPath); - } - return nextProps.isSmallScreenWidth !== this.props.isSmallScreenWidth; } diff --git a/src/libs/Navigation/AppNavigator/index.js b/src/libs/Navigation/AppNavigator/index.js index b1f53844dcb5..6a7b910ebaef 100644 --- a/src/libs/Navigation/AppNavigator/index.js +++ b/src/libs/Navigation/AppNavigator/index.js @@ -6,9 +6,6 @@ import AuthScreens from './AuthScreens'; const propTypes = { /** If we have an authToken this is true */ authenticated: PropTypes.bool.isRequired, - - /** The current path as reported by the NavigationContainer */ - currentPath: PropTypes.string.isRequired, }; const AppNavigator = props => ( @@ -16,7 +13,7 @@ const AppNavigator = props => ( ? ( // These are the protected screens and only accessible when an authToken is present - + ) : ( diff --git a/src/libs/Navigation/NavigationRoot.js b/src/libs/Navigation/NavigationRoot.js index aac2594d6939..2b21856bc35c 100644 --- a/src/libs/Navigation/NavigationRoot.js +++ b/src/libs/Navigation/NavigationRoot.js @@ -1,13 +1,13 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import {getPathFromState, NavigationContainer, DefaultTheme} from '@react-navigation/native'; +import {NavigationContainer, DefaultTheme, getPathFromState} from '@react-navigation/native'; import * as Navigation from './Navigation'; import linkingConfig from './linkingConfig'; import AppNavigator from './AppNavigator'; import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator'; -import Log from '../Log'; import colors from '../../styles/colors'; import styles from '../../styles/styles'; +import Log from '../Log'; // https://reactnavigation.org/docs/themes const navigationTheme = { @@ -27,21 +27,11 @@ const propTypes = { }; class NavigationRoot extends Component { - constructor(props) { - super(props); - - this.state = { - currentPath: '', - }; - - this.parseAndStoreRoute = this.parseAndStoreRoute.bind(this); - } - /** - * Intercept state changes and perform different logic + * Intercept navigation state changes and log it * @param {NavigationState} state */ - parseAndStoreRoute(state) { + parseAndLogRoute(state) { if (!state) { return; } @@ -54,8 +44,6 @@ class NavigationRoot extends Component { } else { Log.info('Navigating to route', false, {path: currentPath}); } - - this.setState({currentPath}); } render() { @@ -67,7 +55,7 @@ class NavigationRoot extends Component { style={styles.navigatorFullScreenLoading} /> )} - onStateChange={this.parseAndStoreRoute} + onStateChange={this.parseAndLogRoute} onReady={this.props.onReady} theme={navigationTheme} ref={Navigation.navigationRef} @@ -76,7 +64,7 @@ class NavigationRoot extends Component { enabled: false, }} > - + ); } diff --git a/src/libs/Navigation/currentUrl/index.js b/src/libs/Navigation/currentUrl/index.js new file mode 100644 index 000000000000..48339f4690a6 --- /dev/null +++ b/src/libs/Navigation/currentUrl/index.js @@ -0,0 +1,6 @@ +/** + * @returns {String} + */ +export default function getCurrentUrl() { + return window.location.href; +} diff --git a/src/libs/Navigation/currentUrl/index.native.js b/src/libs/Navigation/currentUrl/index.native.js new file mode 100644 index 000000000000..265b1bf4efd6 --- /dev/null +++ b/src/libs/Navigation/currentUrl/index.native.js @@ -0,0 +1,6 @@ +/** + * @returns {String} + */ +export default function getCurrentUrl() { + return ''; +} diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index d11396535ef7..c5e7b93c8060 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -14,6 +14,7 @@ import * as Policy from './Policy'; import Navigation from '../Navigation/Navigation'; import ROUTES from '../../ROUTES'; import * as SessionUtils from '../SessionUtils'; +import getCurrentUrl from '../Navigation/currentUrl'; let currentUserAccountID; let currentUserEmail = ''; @@ -161,26 +162,19 @@ function reconnectApp() { * pass it in as a parameter. withOnyx guarantees that the value has been read * from Onyx because it will not render the AuthScreens until that point. * @param {Object} session - * @param {string} currentPath */ -function setUpPoliciesAndNavigate(session, currentPath) { - if (!session || !currentPath || !currentPath.includes('exitTo')) { +function setUpPoliciesAndNavigate(session) { + const currentUrl = getCurrentUrl(); + if (!session || !currentUrl || !currentUrl.includes('exitTo')) { return; } - let exitTo; - try { - const url = new URL(currentPath, CONST.NEW_EXPENSIFY_URL); - exitTo = url.searchParams.get('exitTo'); - } catch (error) { - // URLSearchParams is unsupported on iOS so we catch th error and - // silence it here since this is primarily a Web flow - return; - } + const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, session.email); + const url = new URL(currentUrl); + const exitTo = url.searchParams.get('exitTo'); - const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentPath, session.email); const shouldCreateFreePolicy = !isLoggingInAsNewUser - && Str.startsWith(currentPath, Str.normalizeUrl(ROUTES.TRANSITION_FROM_OLD_DOT)) + && Str.startsWith(url.pathname, Str.normalizeUrl(ROUTES.TRANSITION_FROM_OLD_DOT)) && exitTo === ROUTES.WORKSPACE_NEW; if (shouldCreateFreePolicy) { Policy.createAndGetPolicyList();