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

Handle iframe/webview navigation in-app #8174

Merged
merged 10 commits into from
Mar 17, 2022
Merged
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
27 changes: 26 additions & 1 deletion src/components/WalletStatementModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import React from 'react';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import {View} from 'react-native';
import _ from 'underscore';
import compose from '../../libs/compose';
import withLocalize from '../withLocalize';
import ONYXKEYS from '../../ONYXKEYS';
import {walletStatementPropTypes, walletStatementDefaultProps} from './WalletStatementModalPropTypes';
import styles from '../../styles/styles';
import FullScreenLoadingIndicator from '../FullscreenLoadingIndicator';
import ROUTES from '../../ROUTES';
import Navigation from '../../libs/Navigation/Navigation';

class WalletStatementModal extends React.Component {
constructor(props) {
Expand All @@ -18,6 +21,22 @@ class WalletStatementModal extends React.Component {
};
}

/**
* Handles in-app navigation for iframe links
*
* @param {MessageEvent} e
*/
navigate(e) {
if (!e.data || e.data.type !== 'STATEMENT_NAVIGATE' || !e.data.url) {
return;
}
const iouRoutes = [ROUTES.IOU_REQUEST, ROUTES.IOU_SEND, ROUTES.IOU_BILL];
const navigateToIOURoute = _.find(iouRoutes, iouRoute => e.data.url.includes(iouRoute));
if (navigateToIOURoute) {
Navigation.navigate(navigateToIOURoute);
}
}

render() {
const authToken = lodashGet(this.props, 'session.authToken', null);
return (
Expand All @@ -33,7 +52,13 @@ class WalletStatementModal extends React.Component {
width="100%"
seamless="seamless"
frameBorder="0"
onLoad={() => this.setState({isLoading: false})}
onLoad={() => {
this.setState({isLoading: false});

// We listen to a message sent from the iframe to the parent component when a link is clicked.
// This lets us handle navigation in the app, outside of the iframe.
window.onmessage = e => this.navigate(e);
}}
/>
</View>
</>
Expand Down
65 changes: 48 additions & 17 deletions src/components/WalletStatementModal/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,60 @@ import React from 'react';
import {WebView} from 'react-native-webview';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import withLocalize from '../withLocalize';
import ONYXKEYS from '../../ONYXKEYS';
import compose from '../../libs/compose';
import {walletStatementPropTypes, walletStatementDefaultProps} from './WalletStatementModalPropTypes';
import FullScreenLoadingIndicator from '../FullscreenLoadingIndicator';
import Navigation from '../../libs/Navigation/Navigation';
import ROUTES from '../../ROUTES';

const WalletStatementModal = (props) => {
const authToken = lodashGet(props, 'session.authToken', null);
return (
<WebView
originWhitelist={['https://*']}
source={{
uri: props.statementPageURL,
headers: {
Cookie: `authToken=${authToken}`,
},
}}
incognito // 'incognito' prop required for Android, issue here https://github.com/react-native-webview/react-native-webview/issues/1352
startInLoadingState
renderLoading={() => <FullScreenLoadingIndicator />}
/>
);
};
class WalletStatementModal extends React.Component {
constructor(props) {
super(props);

this.authToken = lodashGet(props, 'session.authToken', null);
this.navigate = this.navigate.bind(this);
}

/**
* Handles in-app navigation for webview links
*
* @param {Object} params
* @param {String} params.url
*/
navigate({url}) {
if (!this.webview || !url) {
return;
}
const iouRoutes = [ROUTES.IOU_REQUEST, ROUTES.IOU_SEND, ROUTES.IOU_BILL];
const navigateToIOURoute = _.find(iouRoutes, iouRoute => url.includes(iouRoute));
if (navigateToIOURoute) {
this.webview.stopLoading();
Navigation.navigate(navigateToIOURoute);
}
}

render() {
return (
<WebView
ref={node => this.webview = node}
originWhitelist={['https://*']}
source={{
uri: this.props.statementPageURL,
headers: {
Cookie: `authToken=${this.authToken}`,
},
}}
incognito // 'incognito' prop required for Android, issue here https://github.com/react-native-webview/react-native-webview/issues/1352
startInLoadingState
renderLoading={() => <FullScreenLoadingIndicator />}
onNavigationStateChange={this.navigate}
/>
);
}
}

WalletStatementModal.propTypes = walletStatementPropTypes;
WalletStatementModal.defaultProps = walletStatementDefaultProps;
Expand Down