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

Enable back button if going from dapp to home & redirection problems #1472

Merged
merged 5 commits into from
Apr 16, 2020
Merged
Changes from 4 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
43 changes: 32 additions & 11 deletions app/components/Views/BrowserTab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ export class BrowserTab extends PureComponent {
activated: props.id === props.activeTab,
lastError: null,
showApprovalDialogHostname: undefined,
showOptions: false
showOptions: false,
lastUrlBeforeHome: null
};
}
backgroundBridges = [];
Expand All @@ -402,7 +403,6 @@ export class BrowserTab extends PureComponent {
sessionENSNames = {};
ensIgnoreList = [];
snapshotTimer = null;
lastUrlBeforeHome = null;
goingBack = false;
wizardScrollAdjusted = false;
isReloading = false;
Expand Down Expand Up @@ -1064,13 +1064,23 @@ export class BrowserTab extends PureComponent {
};

goBack = () => {
if (!this.canGoBack()) return;

this.toggleOptionsIfNeeded();
this.goingBack = true;
setTimeout(() => {
this.goingBack = false;
}, 500);

const { current } = this.webview;
current && current.goBack();
const { lastUrlBeforeHome } = this.state;

if (this.isHomepage() && lastUrlBeforeHome) {
this.go(lastUrlBeforeHome);
} else {
current && current.goBack();
Copy link
Contributor

Choose a reason for hiding this comment

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

If this.isHomepage() is true and lastUrlBeforeHome is false, is it possible that current.goBack() could be called when there is nowhere to go back to? I am thinking of when this.handleAndroidBackPress is called.

}

setTimeout(() => {
this.props.navigation.setParams({
...this.props.navigation.state.params,
Expand All @@ -1083,7 +1093,7 @@ export class BrowserTab extends PureComponent {
forwardEnabled: true,
currentPageTitle: null
});
}, 500);
}, 1000);
};

goBackToHomepage = async () => {
Expand All @@ -1103,14 +1113,15 @@ export class BrowserTab extends PureComponent {
}, 100);
Analytics.trackEvent(ANALYTICS_EVENT_OPTS.DAPP_HOME);
setTimeout(() => {
this.lastUrlBeforeHome = lastUrlBeforeHome;
this.setState({ lastUrlBeforeHome });
}, 1000);
};

goForward = async () => {
const { current } = this.webview;
if (this.lastUrlBeforeHome) {
this.go(this.lastUrlBeforeHome);
const { lastUrlBeforeHome } = this.state;
if (lastUrlBeforeHome) {
this.go(lastUrlBeforeHome);
} else if (this.canGoForward()) {
this.toggleOptionsIfNeeded();
current && current.goForward && current.goForward();
Expand Down Expand Up @@ -1291,7 +1302,7 @@ export class BrowserTab extends PureComponent {
currentPageTitle: title,
forwardEnabled: false
});
this.lastUrlBeforeHome = null;
this.setState({ lastUrlBeforeHome: null });
this.props.navigation.setParams({ url: data.payload.url, silent: true, showUrlModal: false });
this.updateTabInfo(data.payload.url);
break;
Expand Down Expand Up @@ -1327,7 +1338,7 @@ export class BrowserTab extends PureComponent {
if (this.isHomepage(url)) {
this.refreshHomeScripts();
}
if (url === this.state.url) return;
if (url === this.state.url && !this.isHomepage(url)) return;
const { ipfsGateway } = this.props;
const data = {};
const urlObj = new URL(url);
Expand All @@ -1339,7 +1350,9 @@ export class BrowserTab extends PureComponent {
return;
}

this.lastUrlBeforeHome = null;
if (!this.isHomepage(url)) {
this.setState({ lastUrlBeforeHome: null });
}

if (!this.state.showPhishingModal && !this.isAllowedUrl(urlObj.hostname)) {
this.handleNotAllowedUrl(url);
Expand Down Expand Up @@ -1518,7 +1531,7 @@ export class BrowserTab extends PureComponent {
};

renderBottomBar = () => {
const canGoBack = !this.isHomepage();
const canGoBack = this.canGoBack();
const canGoForward = this.canGoForward();
return (
<BrowserBottomBar
Expand Down Expand Up @@ -1839,6 +1852,14 @@ export class BrowserTab extends PureComponent {

canGoForward = () => this.state.forwardEnabled;

canGoBack = () => {
if (this.isHomepage()) {
return !!this.state.lastUrlBeforeHome && !this.isHomepage(this.state.lastUrlBeforeHome);
}

return true;
};

isTabActive = () => {
const { activeTab, id } = this.props;
return activeTab === id;
Expand Down