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">
|
<script lang="ts">
|
||||||
import { cultivar } from './lib/store.svelte';
|
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 Overview from './routes/Overview.svelte';
|
||||||
import BagDetail from './routes/BagDetail.svelte';
|
import BagDetail from './routes/BagDetail.svelte';
|
||||||
import Onboarding from './routes/Onboarding.svelte';
|
import Onboarding from './routes/Onboarding.svelte';
|
||||||
@@ -35,14 +36,14 @@
|
|||||||
return () => cultivar.stopMockWs();
|
return () => cultivar.stopMockWs();
|
||||||
});
|
});
|
||||||
|
|
||||||
const showOnboarding = $derived(!cultivar.state.ui.onboarding_complete || router.current.page === 'onboarding');
|
const showOnboarding = $derived(!cultivar.state.ui.onboarding_complete || $routeStore.page === 'onboarding');
|
||||||
const showDetail = $derived(router.current.page === 'bag');
|
const showDetail = $derived($routeStore.page === 'bag');
|
||||||
const showReference = $derived(router.current.page === 'reference');
|
const showReference = $derived($routeStore.page === 'reference');
|
||||||
const selectedBagId = $derived(showDetail ? (router.current as Extract<typeof router.current, { page: 'bag' }>).bagId : null);
|
const selectedBagId = $derived(showDetail ? ($routeStore as Extract<Route, { page: 'bag' }>).bagId : null);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div id="app-root">
|
<div id="app-root">
|
||||||
{#if router.current.page === 'overview' || showDetail || showReference}
|
{#if $routeStore.page === 'overview' || showDetail || showReference}
|
||||||
<Overview
|
<Overview
|
||||||
onBagSelect={handleBagSelect}
|
onBagSelect={handleBagSelect}
|
||||||
onOpenReference={handleOpenReference}
|
onOpenReference={handleOpenReference}
|
||||||
|
|||||||
+17
-23
@@ -1,3 +1,5 @@
|
|||||||
|
import { writable, type Writable } from 'svelte/store';
|
||||||
|
|
||||||
export type Route =
|
export type Route =
|
||||||
| { page: 'overview' }
|
| { page: 'overview' }
|
||||||
| { page: 'reference' }
|
| { page: 'reference' }
|
||||||
@@ -35,26 +37,12 @@ function routeToHash(route: Route): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRouter() {
|
function createRouter(route: Writable<Route>) {
|
||||||
let route = $state<Route>(parseHash(window.location.hash));
|
|
||||||
|
|
||||||
// Sync from URL to state
|
// Sync from URL to state
|
||||||
$effect(() => {
|
|
||||||
const handleHashChange = () => {
|
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
|
// Sync from state to URL
|
||||||
const navigate = (newRoute: Route) => {
|
const navigate = (newRoute: Route) => {
|
||||||
const newHash = routeToHash(newRoute);
|
const newHash = routeToHash(newRoute);
|
||||||
@@ -63,21 +51,27 @@ function createRouter() {
|
|||||||
if (newHash !== currentHash) {
|
if (newHash !== currentHash) {
|
||||||
window.history.pushState(null, '', newHash || window.location.pathname);
|
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
|
// Ensure initial URL is clean if empty
|
||||||
$effect(() => {
|
route.subscribe(initialRoute => {
|
||||||
if (typeof window !== 'undefined' && !window.location.hash && route.page !== 'overview') {
|
if (!window.location.hash && initialRoute.page !== 'overview') {
|
||||||
navigate(route);
|
navigate(initialRoute);
|
||||||
|
}
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get current() { return route; },
|
|
||||||
navigate,
|
navigate,
|
||||||
back: () => window.history.back(),
|
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