router fix v2
This commit is contained in:
1 parent
594f6779e6
commit
ced4297e0d
2 files changed
+24
-29
No files matched your search
+7
-6
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { cultivar } from './lib/store.svelte';
|
||||
import { router } from './lib/router.svelte';
|
||||
import { router, routeStore } from './lib/router.svelte';
|
||||
import type { Route } from './lib/router.svelte';
|
||||
import Overview from './routes/Overview.svelte';
|
||||
import BagDetail from './routes/BagDetail.svelte';
|
||||
import Onboarding from './routes/Onboarding.svelte';
|
||||
@@ -35,14 +36,14 @@
|
||||
return () => cultivar.stopMockWs();
|
||||
});
|
||||
|
||||
const showOnboarding = $derived(!cultivar.state.ui.onboarding_complete || router.current.page === 'onboarding');
|
||||
const showDetail = $derived(router.current.page === 'bag');
|
||||
const showReference = $derived(router.current.page === 'reference');
|
||||
const selectedBagId = $derived(showDetail ? (router.current as Extract<typeof router.current, { page: 'bag' }>).bagId : null);
|
||||
const showOnboarding = $derived(!cultivar.state.ui.onboarding_complete || $routeStore.page === 'onboarding');
|
||||
const showDetail = $derived($routeStore.page === 'bag');
|
||||
const showReference = $derived($routeStore.page === 'reference');
|
||||
const selectedBagId = $derived(showDetail ? ($routeStore as Extract<Route, { page: 'bag' }>).bagId : null);
|
||||
</script>
|
||||
|
||||
<div id="app-root">
|
||||
{#if router.current.page === 'overview' || showDetail || showReference}
|
||||
{#if $routeStore.page === 'overview' || showDetail || showReference}
|
||||
<Overview
|
||||
onBagSelect={handleBagSelect}
|
||||
onOpenReference={handleOpenReference}
|
||||
|
||||
+17
-23
@@ -1,3 +1,5 @@
|
||||
import { writable, type Writable } from 'svelte/store';
|
||||
|
||||
export type Route =
|
||||
| { page: 'overview' }
|
||||
| { page: 'reference' }
|
||||
@@ -35,26 +37,12 @@ function routeToHash(route: Route): string {
|
||||
}
|
||||
}
|
||||
|
||||
function createRouter() {
|
||||
let route = $state<Route>(parseHash(window.location.hash));
|
||||
|
||||
function createRouter(route: Writable<Route>) {
|
||||
// Sync from URL to state
|
||||
$effect(() => {
|
||||
const handleHashChange = () => {
|
||||
route = parseHash(window.location.hash);
|
||||
route.set(parseHash(window.location.hash));
|
||||
};
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('hashchange', handleHashChange);
|
||||
window.addEventListener('popstate', handleHashChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('hashchange', handleHashChange);
|
||||
window.removeEventListener('popstate', handleHashChange);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Sync from state to URL
|
||||
const navigate = (newRoute: Route) => {
|
||||
const newHash = routeToHash(newRoute);
|
||||
@@ -63,21 +51,27 @@ function createRouter() {
|
||||
if (newHash !== currentHash) {
|
||||
window.history.pushState(null, '', newHash || window.location.pathname);
|
||||
}
|
||||
route = newRoute;
|
||||
route.set(newRoute);
|
||||
};
|
||||
|
||||
// Initialize and listen
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('hashchange', handleHashChange);
|
||||
window.addEventListener('popstate', handleHashChange);
|
||||
|
||||
// Ensure initial URL is clean if empty
|
||||
$effect(() => {
|
||||
if (typeof window !== 'undefined' && !window.location.hash && route.page !== 'overview') {
|
||||
navigate(route);
|
||||
route.subscribe(initialRoute => {
|
||||
if (!window.location.hash && initialRoute.page !== 'overview') {
|
||||
navigate(initialRoute);
|
||||
}
|
||||
})();
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
get current() { return route; },
|
||||
navigate,
|
||||
back: () => window.history.back(),
|
||||
};
|
||||
}
|
||||
|
||||
export const router = createRouter();
|
||||
export const routeStore = writable<Route>(parseHash(window.location.hash));
|
||||
export const router = createRouter(routeStore);
|
||||
Reference in new issue
Block a user