()
| 32 | * 3. sessionStorage fallback (generates unique ID per tab/window) |
| 33 | */ |
| 34 | export function getWindowId(): string { |
| 35 | if (globalWindowId) return globalWindowId |
| 36 | |
| 37 | // Try URL params first (dev mode) |
| 38 | const urlParams = new URLSearchParams(window.location.search) |
| 39 | let id = urlParams.get("windowId") |
| 40 | |
| 41 | // Try hash params (production file:// URLs) |
| 42 | if (!id && window.location.hash) { |
| 43 | const hashParams = new URLSearchParams(window.location.hash.slice(1)) |
| 44 | id = hashParams.get("windowId") |
| 45 | } |
| 46 | |
| 47 | // Fallback: use sessionStorage to preserve ID across page refresh |
| 48 | // This handles cases like page refresh where URL params may be lost |
| 49 | if (!id) { |
| 50 | id = sessionStorage.getItem("windowId") |
| 51 | if (!id) { |
| 52 | // Default to "main" - this is the expected ID for the primary window |
| 53 | // Using a stable default prevents orphan localStorage keys |
| 54 | id = "main" |
| 55 | sessionStorage.setItem("windowId", id) |
| 56 | } |
| 57 | } else { |
| 58 | // Store the ID in sessionStorage so it persists across navigation/refresh |
| 59 | sessionStorage.setItem("windowId", id) |
| 60 | } |
| 61 | |
| 62 | globalWindowId = id |
| 63 | console.log("[WindowContext] Window ID:", id) |
| 64 | return id |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get initial window params (chatId, subChatId) passed when opening a new window. |
no test coverage detected