forked from mystiq/hydrogen-web
restore last url if not providing a specific one on startup
This commit is contained in:
parent
19e774ce01
commit
7c2fc516bb
2 changed files with 17 additions and 7 deletions
|
@ -35,13 +35,13 @@ export class RootViewModel extends ViewModel {
|
||||||
this._sessionViewModel = null;
|
this._sessionViewModel = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load(lastUrlHash) {
|
||||||
this.track(this.navigation.observe("login").subscribe(() => this._applyNavigation()));
|
this.track(this.navigation.observe("login").subscribe(() => this._applyNavigation()));
|
||||||
this.track(this.navigation.observe("session").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 isLogin = this.navigation.observe("login").get();
|
||||||
const sessionId = this.navigation.observe("session").get();
|
const sessionId = this.navigation.observe("session").get();
|
||||||
if (isLogin) {
|
if (isLogin) {
|
||||||
|
@ -58,9 +58,12 @@ export class RootViewModel extends ViewModel {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
let url = restoreHashIfAtDefault;
|
||||||
|
if (!url) {
|
||||||
// redirect depending on what sessions are already present
|
// redirect depending on what sessions are already present
|
||||||
const sessionInfos = await this._sessionInfoStorage.getAll();
|
const sessionInfos = await this._sessionInfoStorage.getAll();
|
||||||
const url = this._urlForSessionInfos(sessionInfos);
|
url = this._urlForSessionInfos(sessionInfos);
|
||||||
|
}
|
||||||
this.urlRouter.history.replaceUrl(url);
|
this.urlRouter.history.replaceUrl(url);
|
||||||
this.urlRouter.applyUrl(url);
|
this.urlRouter.applyUrl(url);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -142,11 +142,18 @@ export async function main(container, paths, legacyExtras) {
|
||||||
navigation
|
navigation
|
||||||
});
|
});
|
||||||
window.__brawlViewModel = vm;
|
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);
|
// TODO: replace with platform.createAndMountRootView(vm, container);
|
||||||
const view = new RootView(vm);
|
const view = new RootView(vm);
|
||||||
container.appendChild(view.mount());
|
container.appendChild(view.mount());
|
||||||
|
window.addEventListener("beforeunload", storeUrlHashOnClose);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(`${err.message}:\n${err.stack}`);
|
console.error(`${err.message}:\n${err.stack}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function storeUrlHashOnClose() {
|
||||||
|
window.localStorage?.setItem("hydrogen_last_url_hash", document.location.hash);
|
||||||
|
window.removeEventListener("beforeunload", storeUrlHashOnClose);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue