| 1 |
/** |
| 2 |
* Primary sidebar navigation: replaces the query string with a clean Yatra URL (drops action, id, etc.) |
| 3 |
* so it matches {@link getUrl} in Layout and avoids full page reloads. |
| 4 |
*/ |
| 5 |
export function navigateMenu(subpage: string, tab?: string): void { |
| 6 |
const pathname = window.location.pathname; |
| 7 |
const page = |
| 8 |
new URLSearchParams(window.location.search).get("page") || "yatra"; |
| 9 |
const sp = new URLSearchParams(); |
| 10 |
sp.set("page", page); |
| 11 |
|
| 12 |
if (subpage === "dashboard") { |
| 13 |
// Match Layout getUrl("dashboard"): only ?page=yatra |
| 14 |
} else { |
| 15 |
sp.set("subpage", subpage); |
| 16 |
if (tab !== undefined && tab !== null && tab !== "") { |
| 17 |
sp.set("tab", tab); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
const newUrl = `${pathname}?${sp.toString()}`; |
| 22 |
window.history.pushState({}, "", newUrl); |
| 23 |
window.dispatchEvent(new PopStateEvent("popstate")); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Navigation hook for updating URL parameters |
| 28 |
*/ |
| 29 |
export const useNavigate = () => { |
| 30 |
const navigate = (params: { |
| 31 |
subpage?: string; |
| 32 |
tab?: string; |
| 33 |
action?: string; |
| 34 |
tabMode?: "specific" | "recurring"; |
| 35 |
id?: number | string; |
| 36 |
trip_id?: number | string; |
| 37 |
[key: string]: string | number | undefined; |
| 38 |
}) => { |
| 39 |
const urlParams = new URLSearchParams(window.location.search); |
| 40 |
|
| 41 |
const setParam = (key: string, value?: string | number) => { |
| 42 |
if (value === undefined || value === null) { |
| 43 |
urlParams.delete(key); |
| 44 |
} else { |
| 45 |
urlParams.set(key, value.toString()); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
const standardKeys = ["subpage", "tab", "action", "id"]; |
| 50 |
|
| 51 |
standardKeys.forEach((key) => { |
| 52 |
if (Object.prototype.hasOwnProperty.call(params, key)) { |
| 53 |
setParam(key, params[key]); |
| 54 |
} |
| 55 |
}); |
| 56 |
|
| 57 |
// Handle custom parameters (like trip_id, tab_mode) |
| 58 |
Object.keys(params).forEach((key) => { |
| 59 |
if (!standardKeys.includes(key)) { |
| 60 |
setParam(key, params[key]); |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
const newUrl = `${window.location.pathname}?${urlParams.toString()}`; |
| 65 |
window.history.pushState({}, "", newUrl); |
| 66 |
window.dispatchEvent(new PopStateEvent("popstate")); |
| 67 |
}; |
| 68 |
|
| 69 |
return { navigate, navigateMenu }; |
| 70 |
}; |
| 71 |
|