diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-15 18:01:57 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-15 18:01:57 -0800 |
| commit | 50d01525ac9f67c5a3e680a3f807c204f6a1cdbd (patch) | |
| tree | 066df58d556ed8a7573f4bc8b7141a528957a3cf /frontend-vanilla/src/router.ts | |
| parent | c652ac6a2cd23ef29f48465be09c2b674783e8e9 (diff) | |
| download | neko-50d01525ac9f67c5a3e680a3f807c204f6a1cdbd.tar.gz neko-50d01525ac9f67c5a3e680a3f807c204f6a1cdbd.tar.bz2 neko-50d01525ac9f67c5a3e680a3f807c204f6a1cdbd.zip | |
Vanilla JS (v3): Implement Tags, Filters, and Infinite Scroll
Diffstat (limited to 'frontend-vanilla/src/router.ts')
| -rw-r--r-- | frontend-vanilla/src/router.ts | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/frontend-vanilla/src/router.ts b/frontend-vanilla/src/router.ts index 08a9e02..46fbe06 100644 --- a/frontend-vanilla/src/router.ts +++ b/frontend-vanilla/src/router.ts @@ -1,6 +1,7 @@ export type Route = { path: string; params: Record<string, string>; + query: URLSearchParams; }; export class Router extends EventTarget { @@ -14,7 +15,8 @@ export class Router extends EventTarget { } getCurrentRoute(): Route { - const path = window.location.pathname.replace(/^\/v3\//, ''); + const url = new URL(window.location.href); + const path = url.pathname.replace(/^\/v3\//, ''); const segments = path.split('/').filter(Boolean); let routePath = '/'; @@ -25,14 +27,32 @@ export class Router extends EventTarget { params.feedId = segments[1]; } else if (segments[0] === 'tag' && segments[1]) { routePath = '/tag'; - params.tagName = segments[1]; + params.tagName = decodeURIComponent(segments[1]); } - return { path: routePath, params }; + return { path: routePath, params, query: url.searchParams }; } - navigate(path: string) { - window.history.pushState({}, '', `/v3${path}`); + navigate(path: string, query?: Record<string, string>) { + let url = `/v3${path}`; + if (query) { + const params = new URLSearchParams(query); + url += `?${params.toString()}`; + } + window.history.pushState({}, '', url); + this.handleRouteChange(); + } + + updateQuery(updates: Record<string, string>) { + const url = new URL(window.location.href); + for (const [key, value] of Object.entries(updates)) { + if (value) { + url.searchParams.set(key, value); + } else { + url.searchParams.delete(key); + } + } + window.history.pushState({}, '', url.toString()); this.handleRouteChange(); } } |
