restore last url if not providing a specific one on startup

This commit is contained in:
Bruno Windels 2020-10-14 15:10:32 +02:00
parent 19e774ce01
commit 7c2fc516bb
2 changed files with 17 additions and 7 deletions

View file

@ -35,13 +35,13 @@ export class RootViewModel extends ViewModel {
this._sessionViewModel = null;
}
async load() {
async load(lastUrlHash) {
this.track(this.navigation.observe("login").subscribe(() => this._applyNavigation()));
this.track(this.navigation.observe("session").subscribe(() => this._applyNavigation()));
this._applyNavigation();
this._applyNavigation(lastUrlHash);
}
async _applyNavigation() {
async _applyNavigation(restoreHashIfAtDefault) {
const isLogin = this.navigation.observe("login").get();
const sessionId = this.navigation.observe("session").get();
if (isLogin) {
@ -58,9 +58,12 @@ export class RootViewModel extends ViewModel {
}
} else {
try {
// redirect depending on what sessions are already present
const sessionInfos = await this._sessionInfoStorage.getAll();
const url = this._urlForSessionInfos(sessionInfos);
let url = restoreHashIfAtDefault;
if (!url) {
// redirect depending on what sessions are already present
const sessionInfos = await this._sessionInfoStorage.getAll();
url = this._urlForSessionInfos(sessionInfos);
}
this.urlRouter.history.replaceUrl(url);
this.urlRouter.applyUrl(url);
} catch (err) {

View file

@ -142,11 +142,18 @@ export async function main(container, paths, legacyExtras) {
navigation
});
window.__brawlViewModel = vm;
await vm.load();
const lastUrlHash = window.localStorage?.getItem("hydrogen_last_url_hash");
await vm.load(lastUrlHash);
// TODO: replace with platform.createAndMountRootView(vm, container);
const view = new RootView(vm);
container.appendChild(view.mount());
window.addEventListener("beforeunload", storeUrlHashOnClose);
} catch(err) {
console.error(`${err.message}:\n${err.stack}`);
}
}
function storeUrlHashOnClose() {
window.localStorage?.setItem("hydrogen_last_url_hash", document.location.hash);
window.removeEventListener("beforeunload", storeUrlHashOnClose);
}