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:
@@ -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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user