8058
Ir
irvc / files
irvc / web / src / views / Settings.tsx
11.1 KB
1import { useEffect, useRef, useState } from "react"; 2import { Moon, Sun } from "lucide-react"; 3import { Room } from "livekit-client"; 4import { Button, Field, Section, Select, TextInput, Toggle } from "@/ui"; 5import type { AudioPresetName, FontSize, ScreenShareQuality, Settings as Prefs } from "../lib/settings"; 6import { 7 TOKENS, 8 effective, 9 presets, 10 tokenLabels, 11 type Mode, 12 type PresetId, 13 type ThemePrefs, 14 type Token, 15} from "../lib/theme"; 16import type { Scheme } from "../lib/scheme"; 17import { useMicPreview, type Voice } from "../lib/voice"; 18 19const minDb = -60; 20const maxDb = -10; 21 22// dbPct maps a dBFS value onto 0..100 across the slider's range. 23const dbPct = (db: number) => 24 Math.max(0, Math.min(1, (db - minDb) / (maxDb - minDb))) * 100; 25 26const audioPresets: { value: AudioPresetName; label: string }[] = [ 27 { value: "telephone", label: "telephone · 12 kbps" }, 28 { value: "speech", label: "speech · 24 kbps" }, 29 { value: "music", label: "music · 48 kbps" }, 30 { value: "musicStereo", label: "music stereo · 64 kbps" }, 31 { value: "musicHighQuality", label: "high quality · 96 kbps" }, 32 { value: "musicHighQualityStereo", label: "high quality stereo · 128 kbps" }, 33]; 34 35// useDevices lists available input/output devices. Labels stay blank until the 36// user grants mic permission (i.e. joins voice), which is fine. 37function useDevices(kind: MediaDeviceKind) { 38 const [devices, setDevices] = useState<MediaDeviceInfo[]>([]); 39 useEffect(() => { 40 let alive = true; 41 const refresh = async () => { 42 const list = await Room.getLocalDevices(kind, false).catch(() => []); 43 if (alive) setDevices(list.filter((d) => d.deviceId)); 44 }; 45 refresh(); 46 navigator.mediaDevices.addEventListener("devicechange", refresh); 47 return () => { 48 alive = false; 49 navigator.mediaDevices.removeEventListener("devicechange", refresh); 50 }; 51 }, [kind]); 52 return devices; 53} 54 55export default function SettingsView({ 56 settings, 57 update, 58 voice, 59 theme, 60}: { 61 settings: Prefs; 62 update: (patch: Partial<Prefs>) => void; 63 voice: Voice; 64 theme: { 65 prefs: ThemePrefs; 66 setMode: (m: Mode) => void; 67 setPreset: (p: PresetId) => void; 68 setToken: (t: Token, color: string) => void; 69 setScheme: (s: Scheme) => void; 70 setSkittles: (v: boolean) => void; 71 }; 72}) { 73 const mics = useDevices("audioinput"); 74 const speakers = useDevices("audiooutput"); 75 // Outside a call there is no published track to meter, so open the mic for a 76 // preview. In a call, reuse the live level to avoid capturing twice. 77 const preview = useMicPreview(settings, voice.status !== "live"); 78 const meter = voice.status === "live" ? voice.micLevel : preview; 79 const { prefs, setMode, setPreset, setToken, setScheme, setSkittles } = theme; 80 81 return ( 82 <div className="mx-auto w-full max-w-2xl space-y-6 p-4 md:p-6"> 83 <Section label="Appearance"> 84 <Field label="text size"> 85 <Select 86 value={settings.fontSize} 87 onChange={(v) => update({ fontSize: v as FontSize })} 88 options={[ 89 { value: "default", label: "default" }, 90 { value: "normal", label: "medium" }, 91 { value: "large", label: "help i'm old" }, 92 ]} 93 /> 94 </Field> 95 <Toggle label="skittles" checked={prefs.skittles} onChange={setSkittles} /> 96 <Field label="palette"> 97 <Select 98 value={prefs.preset} 99 onChange={(v) => setPreset(v as PresetId)} 100 options={presets.map((p) => ({ value: p.id, label: p.label }))} 101 /> 102 </Field> 103 {prefs.preset === "custom" ? ( 104 <> 105 <div className="grid grid-cols-1 gap-x-4 gap-y-1 sm:grid-cols-2"> 106 {TOKENS.map((t) => ( 107 <ColorField 108 key={t} 109 token={t} 110 mode={prefs.mode} 111 preset={prefs.base} 112 override={prefs.overrides[prefs.mode][t]} 113 onChange={(color) => setToken(t, color)} 114 /> 115 ))} 116 </div> 117 <Field label="load palette"> 118 <SchemePicker current={prefs.scheme?.name} onPick={setScheme} /> 119 </Field> 120 </> 121 ) : ( 122 <div className="mt-2 flex gap-2 px-2"> 123 {(["light", "dark"] as const).map((m) => ( 124 <Button 125 key={m} 126 variant={prefs.mode === m ? "primary" : "secondary"} 127 className="flex-1" 128 onClick={() => setMode(m)} 129 > 130 {m === "light" ? <Sun /> : <Moon />} {m} 131 </Button> 132 ))} 133 </div> 134 )} 135 </Section> 136 137 <Section label="Chat"> 138 <Toggle 139 label="typing indicator" 140 checked={settings.typingIndicator} 141 onChange={(v) => update({ typingIndicator: v })} 142 /> 143 <Toggle 144 label="hide previews" 145 checked={settings.hidePreviews} 146 onChange={(v) => update({ hidePreviews: v })} 147 /> 148 <Toggle 149 label="collapse previews by default" 150 checked={settings.collapsePreviews} 151 disabled={settings.hidePreviews} 152 onChange={(v) => update({ collapsePreviews: v })} 153 /> 154 </Section> 155 156 <Section label="Sounds"> 157 <Toggle 158 label="join/leave tone" 159 checked={settings.joinSound} 160 onChange={(v) => update({ joinSound: v })} 161 /> 162 <Toggle 163 label="ringing" 164 checked={settings.ringSound} 165 onChange={(v) => update({ ringSound: v })} 166 /> 167 </Section> 168 169 <Section label="Voice activation"> 170 <div className="px-2"> 171 <Level meter={meter} threshold={settings.gateThreshold} /> 172 </div> 173 <div className="px-2"> 174 <input 175 type="range" 176 min={minDb} 177 max={maxDb} 178 step={1} 179 value={settings.gateThreshold} 180 onChange={(e) => update({ gateThreshold: Number(e.target.value) })} 181 className="w-full accent-[var(--accent)]" 182 /> 183 </div> 184 <p className="px-2 text-xs text-faint">transmits above {settings.gateThreshold} dB</p> 185 </Section> 186 187 <Section label="Devices"> 188 <Field label="microphone"> 189 <Select 190 value={settings.micDeviceId} 191 onChange={(v) => update({ micDeviceId: v })} 192 options={[ 193 { value: "", label: "system default" }, 194 ...mics.map((d) => ({ value: d.deviceId, label: d.label || "microphone" })), 195 ]} 196 /> 197 </Field> 198 <Field label="speaker"> 199 <Select 200 value={settings.speakerDeviceId} 201 onChange={(v) => update({ speakerDeviceId: v })} 202 options={[ 203 { value: "", label: "system default" }, 204 ...speakers.map((d) => ({ value: d.deviceId, label: d.label || "speaker" })), 205 ]} 206 /> 207 </Field> 208 </Section> 209 210 <Section label="Quality"> 211 <Field label="audio"> 212 <Select 213 value={settings.audioPreset} 214 onChange={(v) => update({ audioPreset: v as AudioPresetName })} 215 options={audioPresets} 216 /> 217 </Field> 218 <Field label="screen share"> 219 <Select 220 value={settings.screenShareQuality} 221 onChange={(v) => update({ screenShareQuality: v as ScreenShareQuality })} 222 options={[ 223 { value: "720p", label: "720p · 30 fps" }, 224 { value: "1080p", label: "1080p · 30 fps" }, 225 ]} 226 /> 227 </Field> 228 </Section> 229 230 <Section label="Microphone processing"> 231 <Toggle 232 label="echo cancellation" 233 checked={settings.echoCancellation} 234 onChange={(v) => update({ echoCancellation: v })} 235 /> 236 <Toggle 237 label="noise suppression" 238 checked={settings.noiseSuppression} 239 onChange={(v) => update({ noiseSuppression: v })} 240 /> 241 <Toggle 242 label="auto gain" 243 checked={settings.autoGainControl} 244 onChange={(v) => update({ autoGainControl: v })} 245 /> 246 <Toggle 247 label="voice isolation" 248 checked={settings.voiceIsolation} 249 onChange={(v) => update({ voiceIsolation: v })} 250 /> 251 </Section> 252 </div> 253 ); 254} 255 256function ColorField({ 257 token, 258 mode, 259 preset, 260 override, 261 onChange, 262}: { 263 token: Token; 264 mode: Mode; 265 preset: PresetId; 266 override?: string; 267 onChange: (color: string) => void; 268}) { 269 // Re-read the palette after the theme has been applied to the DOM, so the 270 // swatch never shows the previous mode's or preset's colors. 271 const [value, setValue] = useState(() => effective(token)); 272 useEffect(() => setValue(effective(token)), [token, override, mode, preset]); 273 return ( 274 <label className="flex items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors hover:bg-raised"> 275 <input 276 type="color" 277 value={value} 278 onChange={(e) => onChange(e.target.value)} 279 className="size-7 shrink-0 cursor-pointer rounded border border-line bg-transparent p-0" 280 /> 281 <span className="min-w-0 flex-1 truncate">{tokenLabels[token]}</span> 282 <span className="text-xs text-faint">{override ? "custom" : "palette"}</span> 283 </label> 284 ); 285} 286 287// Level draws the live mic level and marks where the gate threshold sits, so 288// the slider can be placed above the room's background noise. 289function Level({ meter, threshold }: { meter: () => number; threshold: number }) { 290 const ref = useRef<HTMLDivElement>(null); 291 useEffect(() => { 292 let raf = 0; 293 const tick = () => { 294 if (ref.current) ref.current.style.width = `${dbPct(meter())}%`; 295 raf = requestAnimationFrame(tick); 296 }; 297 raf = requestAnimationFrame(tick); 298 return () => cancelAnimationFrame(raf); 299 }, [meter]); 300 301 return ( 302 <div className="relative h-2 overflow-hidden rounded-md border border-line bg-surface"> 303 <div ref={ref} className="h-full bg-accent/70" /> 304 <div className="absolute inset-y-0 w-0.5 bg-fg/60" style={{ left: `${dbPct(threshold)}%` }} /> 305 </div> 306 ); 307} 308 309// SchemePicker is a text box with native autocomplete over the bundled terminal 310// schemes. The list is a separate chunk, only fetched once the box is used. 311function SchemePicker({ current, onPick }: { current?: string; onPick: (s: Scheme) => void }) { 312 const [schemes, setSchemes] = useState<Scheme[]>([]); 313 const [text, setText] = useState(current ?? ""); 314 315 useEffect(() => setText(current ?? ""), [current]); 316 317 function load() { 318 if (!schemes.length) import("../lib/schemes.json").then((m) => setSchemes(m.default as Scheme[])); 319 } 320 321 function change(v: string) { 322 setText(v); 323 const hit = schemes.find((s) => s.n.toLowerCase() === v.trim().toLowerCase()); 324 if (hit) onPick(hit); 325 } 326 327 return ( 328 <> 329 <TextInput 330 list="schemes" 331 value={text} 332 placeholder="search…" 333 autoComplete="off" 334 spellCheck={false} 335 onFocus={load} 336 onChange={(e) => change(e.target.value)} 337 /> 338 <datalist id="schemes"> 339 {schemes.map((s) => ( 340 <option key={s.n} value={s.n} /> 341 ))} 342 </datalist> 343 </> 344 ); 345}