8058
Ir
irvc / files
irvc / web / src / lib / settings.ts
2.3 KB
1import { useCallback, useEffect, useState } from "react"; 2 3// "normal" is our own pinned 16px default. "default" means the browser's root 4// font size, which the user may have changed. Old stored "default" values are 5// intentionally left as-is (no migration). 6export type FontSize = "default" | "normal" | "large"; 7 8export type ScreenShareQuality = "720p" | "1080p"; 9 10export type AudioPresetName = 11 | "telephone" 12 | "speech" 13 | "music" 14 | "musicStereo" 15 | "musicHighQuality" 16 | "musicHighQualityStereo"; 17 18export interface Settings { 19 echoCancellation: boolean; 20 noiseSuppression: boolean; 21 autoGainControl: boolean; 22 voiceIsolation: boolean; 23 audioPreset: AudioPresetName; 24 micDeviceId: string; 25 speakerDeviceId: string; 26 deafened: boolean; 27 gateThreshold: number; 28 joinSound: boolean; 29 typingIndicator: boolean; 30 hidePreviews: boolean; 31 collapsePreviews: boolean; 32 ringSound: boolean; 33 screenShareQuality: ScreenShareQuality; 34 fontSize: FontSize; 35} 36 37export const defaultSettings: Settings = { 38 echoCancellation: true, 39 noiseSuppression: true, 40 autoGainControl: true, 41 voiceIsolation: false, 42 audioPreset: "speech", 43 micDeviceId: "", 44 speakerDeviceId: "", 45 deafened: false, 46 gateThreshold: -60, 47 joinSound: true, 48 typingIndicator: true, 49 hidePreviews: false, 50 collapsePreviews: false, 51 ringSound: true, 52 screenShareQuality: "720p", 53 fontSize: "normal", 54}; 55 56const key = "irvc.settings"; 57 58// Text size is a document-level preference, applied on the root element so 59// every rem-based size follows it. "default" leaves the browser's root size. 60const fontSizes: Record<FontSize, string> = { default: "", normal: "16px", large: "18px" }; 61 62function load(): Settings { 63 try { 64 return { ...defaultSettings, ...JSON.parse(localStorage.getItem(key) ?? "{}") }; 65 } catch { 66 return defaultSettings; 67 } 68} 69 70// useSettings persists voice + UI preferences to localStorage. The empty 71// deviceId means "system default" for both mic and speaker. 72export function useSettings() { 73 const [settings, setSettings] = useState<Settings>(load); 74 75 useEffect(() => { 76 localStorage.setItem(key, JSON.stringify(settings)); 77 document.documentElement.style.fontSize = fontSizes[settings.fontSize]; 78 }, [settings]); 79 80 const update = useCallback((patch: Partial<Settings>) => { 81 setSettings((s) => ({ ...s, ...patch })); 82 }, []); 83 84 return { settings, update }; 85}