From 14ecb26d480a2c921388e820b1348d2dd58b0806 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Mon, 22 Aug 2022 17:48:22 -0400 Subject: [PATCH 1/5] use window.location.href for transition url check --- .../Navigation/AppNavigator/AuthScreens.js | 8 ++----- src/libs/Navigation/currentUrl/index.js | 3 +++ .../Navigation/currentUrl/index.native.js | 3 +++ src/libs/actions/App.js | 21 +++++++------------ 4 files changed, 16 insertions(+), 19 deletions(-) create mode 100644 src/libs/Navigation/currentUrl/index.js create mode 100644 src/libs/Navigation/currentUrl/index.native.js diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 79868dbe51e3..dd572c3354cb 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -89,6 +89,7 @@ const propTypes = { ...windowDimensionsPropTypes, /** The current path as reported by the NavigationContainer */ + // eslint-disable-next-line react/no-unused-prop-types currentPath: PropTypes.string.isRequired, }; @@ -121,7 +122,7 @@ class AuthScreens extends React.Component { UnreadIndicatorUpdater.listenForReportChanges(); App.getAppData(); App.openApp(); - + App.setUpPoliciesAndNavigate(this.props.session); App.fixAccountAndReloadData(); Timing.end(CONST.TIMING.HOMEPAGE_INITIAL_RENDER); @@ -140,11 +141,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/currentUrl/index.js b/src/libs/Navigation/currentUrl/index.js new file mode 100644 index 000000000000..e0e19f0d95dc --- /dev/null +++ b/src/libs/Navigation/currentUrl/index.js @@ -0,0 +1,3 @@ +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..35bf7973e16c --- /dev/null +++ b/src/libs/Navigation/currentUrl/index.native.js @@ -0,0 +1,3 @@ +export default function getCurrentUrl() { + return ''; +} diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 86842a882ebd..3ca7cf7a3a7f 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -16,6 +16,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 = ''; @@ -187,24 +188,18 @@ function fixAccountAndReloadData() { * @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(); From 2e1d6871e52946194c1e11ea410bcb2d4d63d94f Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Mon, 22 Aug 2022 17:55:28 -0400 Subject: [PATCH 2/5] remove unused react router url tracking --- .../Navigation/AppNavigator/AuthScreens.js | 5 --- src/libs/Navigation/AppNavigator/index.js | 5 +-- src/libs/Navigation/NavigationRoot.js | 36 ++----------------- 3 files changed, 3 insertions(+), 43 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index dd572c3354cb..db2413f70bf2 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,10 +86,6 @@ const modalScreenListeners = { const propTypes = { ...windowDimensionsPropTypes, - - /** The current path as reported by the NavigationContainer */ - // eslint-disable-next-line react/no-unused-prop-types - currentPath: PropTypes.string.isRequired, }; class AuthScreens extends React.Component { 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..966858fff3f0 100644 --- a/src/libs/Navigation/NavigationRoot.js +++ b/src/libs/Navigation/NavigationRoot.js @@ -1,11 +1,10 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import {getPathFromState, NavigationContainer, DefaultTheme} from '@react-navigation/native'; +import {NavigationContainer, DefaultTheme} 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'; @@ -27,37 +26,6 @@ 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 - * @param {NavigationState} state - */ - parseAndStoreRoute(state) { - if (!state) { - return; - } - - const currentPath = getPathFromState(state, linkingConfig.config); - - // Don't log the route transitions from OldDot because they contain authTokens - if (currentPath.includes('/transition')) { - Log.info('Navigating from transition link from OldDot using short lived authToken'); - } else { - Log.info('Navigating to route', false, {path: currentPath}); - } - - this.setState({currentPath}); - } - render() { return ( - + ); } From 1bdc2df961c7f952dd6ef3ccb6f44b8ffeb944b4 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Wed, 24 Aug 2022 10:48:37 -0400 Subject: [PATCH 3/5] restore navigator route logging --- src/libs/Navigation/NavigationRoot.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/NavigationRoot.js b/src/libs/Navigation/NavigationRoot.js index 966858fff3f0..2b21856bc35c 100644 --- a/src/libs/Navigation/NavigationRoot.js +++ b/src/libs/Navigation/NavigationRoot.js @@ -1,12 +1,13 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; -import {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 colors from '../../styles/colors'; import styles from '../../styles/styles'; +import Log from '../Log'; // https://reactnavigation.org/docs/themes const navigationTheme = { @@ -26,6 +27,25 @@ const propTypes = { }; class NavigationRoot extends Component { + /** + * Intercept navigation state changes and log it + * @param {NavigationState} state + */ + parseAndLogRoute(state) { + if (!state) { + return; + } + + const currentPath = getPathFromState(state, linkingConfig.config); + + // Don't log the route transitions from OldDot because they contain authTokens + if (currentPath.includes('/transition')) { + Log.info('Navigating from transition link from OldDot using short lived authToken'); + } else { + Log.info('Navigating to route', false, {path: currentPath}); + } + } + render() { return ( )} - onStateChange={this.parseAndStoreRoute} + onStateChange={this.parseAndLogRoute} onReady={this.props.onReady} theme={navigationTheme} ref={Navigation.navigationRef} From eb6df339ce63611910e3e28c300b14862c18107d Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Wed, 24 Aug 2022 10:50:41 -0400 Subject: [PATCH 4/5] jsdocs for getCurrentUrl --- src/libs/Navigation/currentUrl/index.js | 3 +++ src/libs/Navigation/currentUrl/index.native.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/libs/Navigation/currentUrl/index.js b/src/libs/Navigation/currentUrl/index.js index e0e19f0d95dc..48339f4690a6 100644 --- a/src/libs/Navigation/currentUrl/index.js +++ b/src/libs/Navigation/currentUrl/index.js @@ -1,3 +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 index 35bf7973e16c..265b1bf4efd6 100644 --- a/src/libs/Navigation/currentUrl/index.native.js +++ b/src/libs/Navigation/currentUrl/index.native.js @@ -1,3 +1,6 @@ +/** + * @returns {String} + */ export default function getCurrentUrl() { return ''; } From 2ec69315cfe93137a31ea0eaf050c1e836f34ce0 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Wed, 31 Aug 2022 15:20:27 -0400 Subject: [PATCH 5/5] remove unused param doc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marco Chávez --- src/libs/actions/App.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 7bae10f3e1ef..c5e7b93c8060 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -162,7 +162,6 @@ 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) { const currentUrl = getCurrentUrl();