| 1 |
/** |
| 2 |
* Optional URL params accepted by navigateMenu. Most callers just pass |
| 3 |
* a subpage string (and optionally a `tab` string for the 2nd arg); |
| 4 |
* pages that need to deep-link into an action (`?action=view&id=42`) |
| 5 |
* can pass an object here instead. |
| 6 |
*/ |
| 7 |
export type NavigateMenuParams = { |
| 8 |
tab?: string; |
| 9 |
action?: string; |
| 10 |
id?: string | number; |
| 11 |
} & Record<string, string | number | undefined>; |
| 12 |
|
| 13 |
/** |
| 14 |
* Primary sidebar navigation: replaces the query string with a clean Yatra URL (drops action, id, etc.) |
| 15 |
* so it matches {@link getUrl} in Layout and avoids full page reloads. |
| 16 |
* |
| 17 |
* Accepts the second argument as either a plain tab string OR an |
| 18 |
* options object. The object form lets pages deep-link into an action |
| 19 |
* (`navigateMenu("bookings", { action: "view", id: bookingId })`) |
| 20 |
* without dropping out to the lower-level `useNavigate()` hook. |
| 21 |
*/ |
| 22 |
export function navigateMenu( |
| 23 |
subpage: string, |
| 24 |
tabOrParams?: string | NavigateMenuParams, |
| 25 |
): void { |
| 26 |
const pathname = window.location.pathname; |
| 27 |
const page = |
| 28 |
new URLSearchParams(window.location.search).get("page") || "yatra"; |
| 29 |
const sp = new URLSearchParams(); |
| 30 |
sp.set("page", page); |
| 31 |
|
| 32 |
if (subpage === "dashboard") { |
| 33 |
// Match Layout getUrl("dashboard"): only ?page=yatra |
| 34 |
} else { |
| 35 |
sp.set("subpage", subpage); |
| 36 |
if (typeof tabOrParams === "string") { |
| 37 |
if (tabOrParams !== "") { |
| 38 |
sp.set("tab", tabOrParams); |
| 39 |
} |
| 40 |
} else if (tabOrParams && typeof tabOrParams === "object") { |
| 41 |
Object.entries(tabOrParams).forEach(([key, value]) => { |
| 42 |
if (value === undefined || value === null || value === "") return; |
| 43 |
sp.set(key, String(value)); |
| 44 |
}); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
const newUrl = `${pathname}?${sp.toString()}`; |
| 49 |
window.history.pushState({}, "", newUrl); |
| 50 |
window.dispatchEvent(new PopStateEvent("popstate")); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Navigation hook for updating URL parameters |
| 55 |
*/ |
| 56 |
export const useNavigate = () => { |
| 57 |
const navigate = (params: { |
| 58 |
subpage?: string; |
| 59 |
tab?: string; |
| 60 |
action?: string; |
| 61 |
tabMode?: "specific" | "recurring"; |
| 62 |
id?: number | string; |
| 63 |
trip_id?: number | string; |
| 64 |
[key: string]: string | number | undefined; |
| 65 |
}) => { |
| 66 |
const urlParams = new URLSearchParams(window.location.search); |
| 67 |
|
| 68 |
const setParam = (key: string, value?: string | number) => { |
| 69 |
if (value === undefined || value === null) { |
| 70 |
urlParams.delete(key); |
| 71 |
} else { |
| 72 |
urlParams.set(key, value.toString()); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
const standardKeys = ["subpage", "tab", "action", "id"]; |
| 77 |
|
| 78 |
standardKeys.forEach((key) => { |
| 79 |
if (Object.prototype.hasOwnProperty.call(params, key)) { |
| 80 |
setParam(key, params[key]); |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
// Handle custom parameters (like trip_id, tab_mode) |
| 85 |
Object.keys(params).forEach((key) => { |
| 86 |
if (!standardKeys.includes(key)) { |
| 87 |
setParam(key, params[key]); |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
const newUrl = `${window.location.pathname}?${urlParams.toString()}`; |
| 92 |
window.history.pushState({}, "", newUrl); |
| 93 |
window.dispatchEvent(new PopStateEvent("popstate")); |
| 94 |
}; |
| 95 |
|
| 96 |
return { navigate, navigateMenu }; |
| 97 |
}; |
| 98 |
|