use location hash to store current tab

This commit is contained in:
Brahim Hadriche committed 2024-01-08 20:42:39 -05:00
1 parent 5335a0273b
commit 29969db3e4
2 files changed
+29 -1

No files matched your search

+27 -1
View File
@@ -7,7 +7,8 @@
import RemoteIcon from "../assets/remote-control.svg.svelte";
import { appStateStore } from "lib/Stores";
import type { AppState } from "lib/Types";
import { ScreenNames, type AppState } from "lib/Types";
import { onMount } from "svelte";
function setScreen(screen: AppState["screen"]) {
appStateStore.update((state) => {
@@ -20,6 +21,31 @@
state.screen = screen;
return state;
});
setScreenNameInUrl(screen);
}
onMount(() => {
const screen = getScreenNameInUrl();
if (screen) {
setScreen(screen);
} else {
setScreenNameInUrl("home");
}
});
function getScreenNameInUrl(): AppState["screen"] | undefined {
const hash = window.location.hash;
if (hash) {
const screen = hash.substring(1) as AppState["screen"];
if (!ScreenNames.includes(screen)) {
return;
}
return screen;
}
}
function setScreenNameInUrl(screen: AppState["screen"]) {
window.location.hash = screen;
}
</script>
+2
View File
@@ -1,3 +1,5 @@
export const ScreenNames = ["search", "home", "bookmarks", "settings", "info", "remote"] as const;
export type AppState = {
screen: "search" | "home" | "bookmarks" | "settings" | "info" | "remote"
}