File size: 1,552 Bytes
15f353f
77c3cea
15f353f
77c3cea
 
 
15f353f
 
 
 
77c3cea
 
15f353f
 
77c3cea
 
 
15f353f
 
 
 
77c3cea
15f353f
 
 
 
77c3cea
 
 
15f353f
 
 
 
77c3cea
 
15f353f
 
77c3cea
 
 
15f353f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
 * 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
	};
}