Skip to content

Commit

Permalink
use location hash to store current tab
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed Jan 9, 2024
1 parent 5335a02 commit 29969db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 27 additions & 1 deletion playlet-web/src/lib/BottomNavigation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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>

Expand Down
2 changes: 2 additions & 0 deletions playlet-web/src/lib/Types.ts
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down

0 comments on commit 29969db

Please sign in to comment.