@@ -0,0 +1,88 @@
1+import { useEffect, useState } from "react";
2+import { MicOff, Volume2, VolumeX } from "lucide-react";
3+import { Button } from "@/components/ui/button";
4+import type { Voice } from "../lib/voice";
5+
6+const storageKey = "irvc.peerVolumes";
7+
8+function load(): Record<string, number> {
9+ try {
10+ return JSON.parse(localStorage.getItem(storageKey) ?? "{}");
11+ } catch {
12+ return {};
13+ }
14+}
15+
16+export default function VoicePeers({ voice, username }: { voice: Voice; username: string }) {
17+ const { peers, muted: selfMuted, setPeerVolume } = voice;
18+ const [volumes, setVolumes] = useState<Record<string, number>>(load);
19+ const [muted, setMuted] = useState<Set<string>>(new Set());
20+ const [open, setOpen] = useState<string | null>(null);
21+
22+ // Re-apply on join and on any volume/mute change: the volume map lives on
23+ // each participant, which is a new object every time someone connects.
24+ useEffect(() => {
25+ peers.forEach((p) => setPeerVolume(p, muted.has(p) ? 0 : (volumes[p] ?? 1)));
26+ }, [peers, volumes, muted, setPeerVolume]);
27+
28+ function setVolume(name: string, v: number) {
29+ setVolumes((prev) => {
30+ const next = { ...prev, [name]: v };
31+ localStorage.setItem(storageKey, JSON.stringify(next));
32+ return next;
33+ });
34+ }
35+
36+ function toggleMute(name: string) {
37+ setMuted((prev) => {
38+ const next = new Set(prev);
39+ if (next.has(name)) next.delete(name);
40+ else next.add(name);
41+ return next;
42+ });
43+ }
44+
45+ return (
46+ <>
47+ <div className="flex items-center gap-1.5 text-xs text-sepia/70">
48+ <span className="h-1.5 w-1.5 rounded-full bg-gold" />
49+ {username} <span className="text-sepia/40">(you)</span>
50+ {selfMuted && <MicOff size={10} className="text-sepia/50" />}
51+ </div>
52+ {peers.map((p) => (
53+ <div key={p}>
54+ <button
55+ onClick={() => setOpen((o) => (o === p ? null : p))}
56+ className="flex w-full items-center gap-1.5 text-left text-xs text-sepia/70 hover:text-sepia"
57+ >
58+ <span className="h-1.5 w-1.5 rounded-full bg-gold" />
59+ {p}
60+ {muted.has(p) && <VolumeX size={10} className="text-sepia/50" />}
61+ </button>
62+ {open === p && (
63+ <div className="flex flex-col gap-1 py-1 pl-3.5">
64+ <input
65+ type="range"
66+ min={0}
67+ max={1}
68+ step={0.01}
69+ value={volumes[p] ?? 1}
70+ onChange={(e) => setVolume(p, Number(e.target.value))}
71+ className="w-full accent-gold"
72+ />
73+ <Button
74+ size="xs"
75+ variant="ghost"
76+ className="w-full justify-start gap-1.5 text-xs text-sepia/50"
77+ onClick={() => toggleMute(p)}
78+ >
79+ {muted.has(p) ? <Volume2 size={12} /> : <VolumeX size={12} />}
80+ {muted.has(p) ? "unmute" : "mute"}
81+ </Button>
82+ </div>
83+ )}
84+ </div>
85+ ))}
86+ </>
87+ );
88+}