import { useEffect, useState } from "react"; /** Tiny pathname router (no dependency): /login, /register, /chat, /. */ export function normalizePath(p: string): string { if (p.length > 1 && p.endsWith("/")) return p.slice(0, -1); return p || "/"; } export function getPath(): string { return normalizePath(window.location.pathname); } /** Client-side navigation without reload (same-origin path only). */ export function navigate(to: string, replace = false): void { const dest = normalizePath(to); if (getPath() === dest) return; if (replace) window.history.replaceState(null, "", dest); else window.history.pushState(null, "", dest); window.dispatchEvent(new PopStateEvent("popstate")); } export function useRoute(): string { const [route, setRoute] = useState(getPath); useEffect(() => { const h = () => setRoute(getPath()); window.addEventListener("popstate", h); return () => window.removeEventListener("popstate", h); }, []); return route; }