mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 21:10:59 +00:00
fix(settings): forward moved anchors on hash changes too, prove targets exist
A same-page link to a moved anchor changes only the hash, so the arrival check never forwarded it; the shell now also listens for hashchange. The legacy-routes guard walks the component tree of every forward target's page and fails when the anchor it forwards to is not rendered there.
This commit is contained in:
1 parent
b25ee09f7d
commit
709d28523c
3 files changed
+152
-3
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
usePathname: () => "/settings/privacy",
|
||||
useRouter: () => ({ replace: vi.fn() }),
|
||||
}));
|
||||
|
||||
import { watchAnchorForwards } from "../settings-anchor-forward";
|
||||
|
||||
function fakeWindow(hash: string) {
|
||||
const target = new EventTarget();
|
||||
return Object.assign(target, { location: { hash } });
|
||||
}
|
||||
|
||||
describe("watchAnchorForwards", () => {
|
||||
it("forwards a moved anchor on arrival", () => {
|
||||
const replace = vi.fn();
|
||||
watchAnchorForwards("/settings/privacy", replace, fakeWindow("#sessions"));
|
||||
expect(replace).toHaveBeenCalledWith("/settings/security#sessions");
|
||||
});
|
||||
|
||||
it("forwards when a same-page link changes only the hash", () => {
|
||||
const replace = vi.fn();
|
||||
const win = fakeWindow("#retention");
|
||||
const stop = watchAnchorForwards("/settings/privacy", replace, win);
|
||||
expect(replace).not.toHaveBeenCalled();
|
||||
|
||||
win.location.hash = "#trusted-devices";
|
||||
win.dispatchEvent(new Event("hashchange"));
|
||||
expect(replace).toHaveBeenCalledWith("/settings/security#trusted-devices");
|
||||
|
||||
stop();
|
||||
win.location.hash = "#sessions";
|
||||
win.dispatchEvent(new Event("hashchange"));
|
||||
expect(replace).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -7,7 +7,8 @@
|
||||
* moved while its page stayed cannot be: the `#anchor` never reaches the
|
||||
* server. Bookmarks, cross-links in older notifications and the docs point at
|
||||
* `/settings/privacy#sessions` and the like, so the shell forwards a known
|
||||
* anchor on a known page to the card's new home, once, on arrival.
|
||||
* anchor on a known page to the card's new home, on arrival and when a
|
||||
* same-page link changes only the hash.
|
||||
*
|
||||
* `SETTINGS_ANCHOR_FORWARDS` is also read by the legacy-routes guard, which
|
||||
* proves every target lands on a page that exists.
|
||||
@@ -56,14 +57,34 @@ export function forwardedAnchorTarget(
|
||||
return SETTINGS_ANCHOR_FORWARDS[pathname]?.[anchor] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward now, and again whenever only the hash changes: a same-page link to
|
||||
* a moved anchor is no navigation, so an arrival check alone never sees it.
|
||||
* Returns the unsubscribe.
|
||||
*/
|
||||
export function watchAnchorForwards(
|
||||
pathname: string,
|
||||
replace: (target: string) => void,
|
||||
win: Pick<Window, "addEventListener" | "removeEventListener"> & {
|
||||
location: { hash: string };
|
||||
} = window,
|
||||
): () => void {
|
||||
const forward = () => {
|
||||
const target = forwardedAnchorTarget(pathname, win.location.hash);
|
||||
if (target) replace(target);
|
||||
};
|
||||
forward();
|
||||
win.addEventListener("hashchange", forward);
|
||||
return () => win.removeEventListener("hashchange", forward);
|
||||
}
|
||||
|
||||
export function SettingsAnchorForward() {
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (!pathname) return;
|
||||
const target = forwardedAnchorTarget(pathname, window.location.hash);
|
||||
if (target) router.replace(target);
|
||||
return watchAnchorForwards(pathname, (target) => router.replace(target));
|
||||
}, [pathname, router]);
|
||||
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user