accounts-frontend/packages/app/services/history.ts

38 lines
827 B
TypeScript
Raw Normal View History

/**
* A helper wrapper service around window.history
*/
import { createBrowserHistory, History } from 'history';
export const browserHistory = createBrowserHistory();
browserHistory.listen(() => {
2020-05-24 04:38:24 +05:30
patchHistory(browserHistory);
});
function patchHistory(history: History): void {
2020-05-24 04:38:24 +05:30
Object.assign(history.location, {
query: new URLSearchParams(history.location.search),
});
}
patchHistory(browserHistory);
export default {
2020-05-24 04:38:24 +05:30
initialLength: 0,
init() {
this.initialLength = window.history.length;
},
/**
* @returns {boolean} - whether history.back() can be safetly called
*/
canGoBack() {
return (
document.referrer.includes(`${location.protocol}//${location.host}`) ||
this.initialLength < window.history.length
);
},
};