From 7c2fc516bbdc5dd8d5339a63370e2c56e5f9fbb1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 14 Oct 2020 15:10:32 +0200 Subject: [PATCH] restore last url if not providing a specific one on startup --- src/domain/RootViewModel.js | 15 +++++++++------ src/main.js | 9 ++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/domain/RootViewModel.js b/src/domain/RootViewModel.js index f81591db..24c810c7 100644 --- a/src/domain/RootViewModel.js +++ b/src/domain/RootViewModel.js @@ -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) { diff --git a/src/main.js b/src/main.js index 8294aa46..af686d2b 100644 --- a/src/main.js +++ b/src/main.js @@ -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); +}