Spaces:
Sleeping
Sleeping
| /** | |
| * Composable for managing room ID in URL | |
| * Enables shareable room links for VS People multiplayer mode | |
| * | |
| * With hash-based routing, we use query parameters for room IDs: | |
| * e.g., /#/vs-people?room=ABC12345 | |
| */ | |
| export function useRoomHash() { | |
| /** | |
| * Extract room ID from URL query parameter | |
| * @returns Room ID string or null if no room param present | |
| */ | |
| function getRoomIdFromHash(): string | null { | |
| const urlParams = new URLSearchParams(window.location.search); | |
| const roomId = urlParams.get("room"); | |
| return roomId || null; | |
| } | |
| /** | |
| * Update URL with room ID query parameter | |
| * Uses replaceState to avoid adding browser history entry | |
| * @param roomId - 8-character uppercase alphanumeric room ID | |
| */ | |
| function updateHash(roomId: string): void { | |
| const url = new URL(window.location.href); | |
| url.searchParams.set("room", roomId); | |
| window.history.replaceState(null, "", url.toString()); | |
| } | |
| /** | |
| * Remove room ID from URL | |
| * Clears room query parameter from address bar | |
| */ | |
| function clearHash(): void { | |
| const url = new URL(window.location.href); | |
| url.searchParams.delete("room"); | |
| window.history.replaceState(null, "", url.toString()); | |
| } | |
| /** | |
| * Validate room ID format | |
| * Must match backend generation: 8 uppercase alphanumeric characters | |
| * @param roomId - Room ID to validate | |
| * @returns true if valid format, false otherwise | |
| */ | |
| function isValidRoomId(roomId: string): boolean { | |
| return /^[A-Z0-9]{8}$/.test(roomId); | |
| } | |
| return { | |
| getRoomIdFromHash, | |
| updateHash, | |
| clearHash, | |
| isValidRoomId | |
| }; | |
| } | |