Skip to content

Commit

Permalink
Enable back button if going from dapp to home & redirection problems (#…
Browse files Browse the repository at this point in the history
…1472)

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

* fix for android

* prevent going back when it's not possible

* Fix can go back function and go forward
  • Loading branch information
andrepimenta authored Apr 16, 2020
1 parent 024bea1 commit 25e3910
Showing 1 changed file with 32 additions and 11 deletions.
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();
}

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

0 comments on commit 25e3910

Please sign in to comment.