diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-15 17:44:55 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-15 17:44:55 -0800 |
| commit | c652ac6a2cd23ef29f48465be09c2b674783e8e9 (patch) | |
| tree | c5c05a71a1d5b8155b05dad4a512b18ff7258f47 /frontend-vanilla/src/router.ts | |
| parent | 90c1a68d6478138f538094fc83e48da8ddd21fa0 (diff) | |
| download | neko-c652ac6a2cd23ef29f48465be09c2b674783e8e9.tar.gz neko-c652ac6a2cd23ef29f48465be09c2b674783e8e9.tar.bz2 neko-c652ac6a2cd23ef29f48465be09c2b674783e8e9.zip | |
Vanilla JS (v3): Implement 3-pane layout, item fetching, reading, and testing
Diffstat (limited to 'frontend-vanilla/src/router.ts')
| -rw-r--r-- | frontend-vanilla/src/router.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/frontend-vanilla/src/router.ts b/frontend-vanilla/src/router.ts new file mode 100644 index 0000000..08a9e02 --- /dev/null +++ b/frontend-vanilla/src/router.ts @@ -0,0 +1,40 @@ +export type Route = { + path: string; + params: Record<string, string>; +}; + +export class Router extends EventTarget { + constructor() { + super(); + window.addEventListener('popstate', () => this.handleRouteChange()); + } + + private handleRouteChange() { + this.dispatchEvent(new CustomEvent('route-changed', { detail: this.getCurrentRoute() })); + } + + getCurrentRoute(): Route { + const path = window.location.pathname.replace(/^\/v3\//, ''); + const segments = path.split('/').filter(Boolean); + + let routePath = '/'; + const params: Record<string, string> = {}; + + if (segments[0] === 'feed' && segments[1]) { + routePath = '/feed'; + params.feedId = segments[1]; + } else if (segments[0] === 'tag' && segments[1]) { + routePath = '/tag'; + params.tagName = segments[1]; + } + + return { path: routePath, params }; + } + + navigate(path: string) { + window.history.pushState({}, '', `/v3${path}`); + this.handleRouteChange(); + } +} + +export const router = new Router(); |
