8058
Ir
irvc / files
irvc / web / src / components / VoiceBar.tsx
7.1 KB
1import { useEffect, useState } from "react"; 2import { 3 HeadphoneOff, 4 Headphones, 5 Mic, 6 MicOff, 7 PhoneOff, 8 ScreenShare, 9 ScreenShareOff, 10 Video, 11 VideoOff, 12 Volume2, 13 VolumeX, 14} from "lucide-react"; 15import { cn } from "cn"; 16import { Dot, IconButton, Menu, MenuItem, VoiceMarks } from "@/ui"; 17import type { VoiceOccupant } from "../lib/types"; 18import type { Voice } from "../lib/voice"; 19import { userColor } from "../lib/scheme"; 20 21const volumesKey = "irvc.peerVolumes"; 22 23function loadVolumes(): Record<string, number> { 24 try { 25 return JSON.parse(localStorage.getItem(volumesKey) ?? "{}"); 26 } catch { 27 return {}; 28 } 29} 30 31// Screen sharing needs getDisplayMedia: absent on mobile and on plain http. 32const canShare = typeof navigator.mediaDevices?.getDisplayMedia === "function"; 33 34// VoiceBar is the persistent "you are in a call" panel. It docks in the 35// sidebar on desktop and at the bottom of the screen on mobile, so the call 36// controls never depend on the sidebar being open. 37export default function VoiceBar({ 38 voice, 39 self, 40 deafened, 41 onDeafen, 42 openShare, 43 onOpenShare, 44 occupants, 45 className, 46}: { 47 voice: Voice; 48 self: string; 49 deafened: boolean; 50 onDeafen: (value: boolean) => void; 51 openShare?: string | null; 52 onOpenShare?: (id: string) => void; 53 occupants?: VoiceOccupant[]; 54 className?: string; 55}) { 56 const [volumes, setVolumes] = useState<Record<string, number>>(loadVolumes); 57 const [muted, setMuted] = useState<Set<string>>(new Set()); 58 const { peers, shares, speaking } = voice; 59 60 // Volumes live on participant objects, which are recreated on every 61 // connect, so re-apply them whenever the roster or the map changes. 62 useEffect(() => { 63 peers.forEach((p) => voice.setPeerVolume(p, muted.has(p) ? 0 : (volumes[p] ?? 1))); 64 }, [peers, volumes, muted, voice]); 65 66 if (!voice.channel) return null; 67 68 function setVolume(name: string, v: number) { 69 setVolumes((prev) => { 70 const next = { ...prev, [name]: v }; 71 localStorage.setItem(volumesKey, JSON.stringify(next)); 72 return next; 73 }); 74 } 75 76 function togglePeerMute(name: string) { 77 setMuted((prev) => { 78 const next = new Set(prev); 79 if (next.has(name)) next.delete(name); 80 else next.add(name); 81 return next; 82 }); 83 } 84 85 const status = 86 voice.status === "live" 87 ? "connected" 88 : voice.status === "connecting" 89 ? "connecting…" 90 : voice.error || "disconnected"; 91 92 return ( 93 <div className={cn("rounded-lg border border-line bg-raised p-2", className)}> 94 <div className="flex items-center gap-2 px-1"> 95 {voice.channel.type === "dm" ? ( 96 <Dot on={voice.status === "live"} /> 97 ) : ( 98 <Volume2 size={14} className={voice.status === "live" ? "text-accent" : "text-faint"} /> 99 )} 100 <span className="min-w-0 flex-1 truncate text-sm font-medium">{voice.channel.name}</span> 101 <span className="max-w-1/2 shrink-0 truncate text-xs text-faint">{status}</span> 102 </div> 103 104 {voice.status === "live" && ( 105 <ul className="mt-1 space-y-0.5"> 106 <li className="row py-1 text-sm"> 107 <Dot on={speaking.includes(self)} /> 108 <span className="flex-1 truncate" style={{ color: userColor(self) }}> 109 {self} 110 </span> 111 <VoiceMarks muted={voice.muted} deafened={deafened} /> 112 </li> 113 {peers.map((p) => { 114 const them = occupants?.find((o) => o.name === p); 115 return ( 116 <li key={p} className="flex items-center gap-1"> 117 <PeerMenu 118 name={p} 119 speaking={speaking.includes(p)} 120 muted={muted.has(p)} 121 volume={volumes[p] ?? 1} 122 onVolume={setVolume} 123 onToggleMute={togglePeerMute} 124 /> 125 <VoiceMarks muted={!!them?.muted} deafened={!!them?.deafened} /> 126 {muted.has(p) && <VolumeX size={12} className="shrink-0 text-faint" />} 127 </li> 128 ); 129 })} 130 {shares.map((s) => ( 131 <li key={s.id}> 132 <button 133 type="button" 134 onClick={() => onOpenShare?.(s.id)} 135 className={cn( 136 "row w-full py-1 text-sm", 137 openShare === s.id && "text-accent", 138 )} 139 > 140 <ScreenShare size={12} /> 141 <span className="flex-1 truncate"> 142 {s.self ? "your screen" : `${s.name}'s screen`} 143 </span> 144 </button> 145 </li> 146 ))} 147 </ul> 148 )} 149 150 <div className="mt-1.5 flex items-center gap-1"> 151 <IconButton 152 label={voice.muted ? "unmute microphone" : "mute microphone"} 153 variant={voice.muted ? "danger" : "ghost"} 154 onClick={voice.toggleMute} 155 > 156 {voice.muted ? <MicOff /> : <Mic />} 157 </IconButton> 158 <IconButton 159 label={deafened ? "undeafen" : "deafen"} 160 variant={deafened ? "danger" : "ghost"} 161 onClick={() => onDeafen(!deafened)} 162 > 163 {deafened ? <HeadphoneOff /> : <Headphones />} 164 </IconButton> 165 {voice.channel.type === "dm" && ( 166 <IconButton 167 label={voice.cameraOn ? "turn camera off" : "turn camera on"} 168 variant={voice.cameraOn ? "secondary" : "ghost"} 169 onClick={voice.toggleCamera} 170 > 171 {voice.cameraOn ? <Video /> : <VideoOff />} 172 </IconButton> 173 )} 174 <IconButton 175 label={!canShare ? "screen share needs https" : voice.sharing ? "stop sharing" : "share screen"} 176 variant={voice.sharing ? "secondary" : "ghost"} 177 disabled={!canShare} 178 onClick={voice.toggleShare} 179 > 180 {voice.sharing ? <ScreenShareOff /> : <ScreenShare />} 181 </IconButton> 182 <IconButton label="leave call" variant="danger" className="ml-auto" onClick={voice.leave}> 183 <PhoneOff /> 184 </IconButton> 185 </div> 186 </div> 187 ); 188} 189 190function PeerMenu({ 191 name, 192 speaking, 193 muted, 194 volume, 195 onVolume, 196 onToggleMute, 197}: { 198 name: string; 199 speaking: boolean; 200 muted: boolean; 201 volume: number; 202 onVolume: (name: string, value: number) => void; 203 onToggleMute: (name: string) => void; 204}) { 205 return ( 206 <Menu 207 align="start" 208 className="min-w-0 flex-1" 209 trigger={ 210 <button type="button" aria-label={`${name} volume`} className="row w-full py-1 text-sm"> 211 <Dot on={speaking} /> 212 <span className="flex-1 truncate" style={{ color: userColor(name) }}> 213 {name} 214 </span> 215 </button> 216 } 217 > 218 {() => ( 219 <> 220 <div className="px-2.5 py-1.5 text-xs text-faint">volume {Math.round(volume * 100)}%</div> 221 <input 222 type="range" 223 min={0} 224 max={1} 225 step={0.01} 226 value={volume} 227 onChange={(e) => onVolume(name, Number(e.target.value))} 228 className="w-full accent-[var(--accent)]" 229 /> 230 <MenuItem onClick={() => onToggleMute(name)}>{muted ? "unmute" : "mute"}</MenuItem> 231 </> 232 )} 233 </Menu> 234 ); 235}