refactor: preserve URL search params during navigation

Introduce `useNavigateWithParams` to ensure that existing URL search
parameters are maintained when navigating between pages. This is
applied to the Article component, FileTree, and journal links to
prevent losing state (like session or player info) during client-side
navigation.

Also intercepts markdown anchor links in the Article component to use
client-side navigation.
This commit is contained in:
2026-07-07 23:26:44 +08:00
parent 48776424a7
commit 779722fe85
4 changed files with 54 additions and 9 deletions
+21
View File
@@ -0,0 +1,21 @@
/**
* useNavigateWithParams — wraps navigate() to preserve the current URL
* search params (e.g. ?session=abc&player=alice) when navigating to a
* new path.
*/
import { useNavigate, useLocation } from "@solidjs/router";
export function useNavigateWithParams() {
const navigate = useNavigate();
const location = useLocation();
return (to: string) => {
// Split hash from the path so we can re-attach it after search params
const hashIdx = to.indexOf("#");
const path = hashIdx >= 0 ? to.slice(0, hashIdx) : to;
const hash = hashIdx >= 0 ? to.slice(hashIdx) : "";
navigate(path + location.search + hash);
};
}