(key: K)
| 46 | * @returns The stored value, or the default if not found |
| 47 | */ |
| 48 | export function getConfig<K extends keyof ConfigValues>(key: K): ConfigValues[K] { |
| 49 | if (typeof window === 'undefined') return DEFAULT_CONFIG[key]; |
| 50 | |
| 51 | try { |
| 52 | const storedValue = localStorage.getItem(`robotics_config_${key}`); |
| 53 | if (storedValue === null) return DEFAULT_CONFIG[key]; |
| 54 | |
| 55 | // Parse the stored value |
| 56 | const parsed = JSON.parse(storedValue); |
| 57 | |
| 58 | // Handle each config key separately with proper type checking |
| 59 | switch (key) { |
| 60 | case 'videoFormat': |
| 61 | // Check if the value is a valid video format |
| 62 | if (parsed === 'mp4' || parsed === 'webm') { |
| 63 | return parsed as ConfigValues[K]; |
| 64 | } |
| 65 | return DEFAULT_CONFIG.videoFormat as ConfigValues[K]; |
| 66 | |
| 67 | case 'robotAddress': |
| 68 | // Basic validation for robot address |
| 69 | if (typeof parsed === 'string' && ( |
| 70 | parsed.startsWith('ws://') || |
| 71 | parsed.startsWith('wss://') || |
| 72 | parsed.startsWith('http://') || |
| 73 | parsed.startsWith('https://') || |
| 74 | /^[0-9\.]+$/.test(parsed) || // IP without protocol |
| 75 | parsed.includes('.') // domain without protocol |
| 76 | )) { |
| 77 | return parsed as ConfigValues[K]; |
| 78 | } |
| 79 | return DEFAULT_CONFIG.robotAddress as ConfigValues[K]; |
| 80 | |
| 81 | case 'overlayColor': |
| 82 | // Check if the value is a valid overlay color |
| 83 | if ( |
| 84 | parsed === 'white' || |
| 85 | parsed === 'black' || |
| 86 | parsed === 'red' || |
| 87 | parsed === 'purple' || |
| 88 | parsed === 'blue' || |
| 89 | parsed === 'green' |
| 90 | ) { |
| 91 | return parsed as ConfigValues[K]; |
| 92 | } |
| 93 | return DEFAULT_CONFIG.overlayColor as ConfigValues[K]; |
| 94 | |
| 95 | case 'gamepadMappings': |
| 96 | // Validate gamepad mappings object |
| 97 | if (typeof parsed === 'object' && parsed !== null) { |
| 98 | return parsed as ConfigValues[K]; |
| 99 | } |
| 100 | return DEFAULT_CONFIG.gamepadMappings as ConfigValues[K]; |
| 101 | |
| 102 | case 'gamepadAxisMappings': |
| 103 | // Validate gamepad axis mappings object |
| 104 | if (typeof parsed === 'object' && parsed !== null) { |
| 105 | return parsed as ConfigValues[K]; |
no outgoing calls
no test coverage detected