| @@ -1,9 +1,29 @@ | ||
| 1 | 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 | +/** | |
| 2 | 14 | * Primary sidebar navigation: replaces the query string with a clean Yatra URL (drops action, id, etc.) |
| 3 | 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. | |
| 4 | 21 | */ |
| 5 | -export function navigateMenu(subpage: string, tab?: string): void { | |
| 22 | +export function navigateMenu( | |
| 23 | + subpage: string, | |
| 24 | + tabOrParams?: string | NavigateMenuParams, | |
| 25 | +): void { | |
| 6 | 26 | const pathname = window.location.pathname; |
| 7 | 27 | const page = |
| 8 | 28 | new URLSearchParams(window.location.search).get("page") || "yatra"; |
| 9 | 29 | const sp = new URLSearchParams(); |
| @@ -12,10 +32,17 @@ | ||
| 12 | 32 | if (subpage === "dashboard") { |
| 13 | 33 | // Match Layout getUrl("dashboard"): only ?page=yatra |
| 14 | 34 | } else { |
| 15 | 35 | sp.set("subpage", subpage); |
| 16 | - if (tab !== undefined && tab !== null && tab !== "") { | |
| 17 | - sp.set("tab", tab); | |
| 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 | + }); | |
| 18 | 45 | } |
| 19 | 46 | } |
| 20 | 47 | |
| 21 | 48 | const newUrl = `${pathname}?${sp.toString()}`; |