@@ -1,104 +1,64 @@
1-import { useEffect, useRef, useState } from "react";
2-import { Mic, MicOff } from "lucide-react";
3-import { Room, RoomEvent, Track } from "livekit-client";
1+import { Mic, MicOff, PhoneOff, Volume2 } from "lucide-react";
42 import { Button } from "@/components/ui/button";
5-import { api } from "../lib/api";
63 import type { Channel } from "../lib/types";
4+import type { Voice } from "../lib/voice";
75
8-interface VoiceToken {
9- url: string;
10- token: string;
11- room: string;
12-}
13-
14-export default function VoiceRoom({ channel, username }: { channel: Channel; username: string }) {
15- const [status, setStatus] = useState<"connecting" | "live" | "error">("connecting");
16- const [error, setError] = useState("");
17- const [peers, setPeers] = useState<string[]>([]);
18- const [muted, setMuted] = useState(false);
19- const roomRef = useRef<Room | null>(null);
20-
21- useEffect(() => {
22- let room: Room | null = null;
23- let cancelled = false;
24- const audio: HTMLElement[] = [];
25-
26- (async () => {
27- try {
28- const t = await api<VoiceToken>(`/channels/${channel.id}/voice`, { method: "POST" });
29- if (cancelled) return;
30- room = new Room();
31- roomRef.current = room;
32-
33- const sync = () => {
34- setPeers([...room!.remoteParticipants.values()].map((p) => p.name || p.identity));
35- };
6+export default function VoiceRoom({
7+ channel,
8+ username,
9+ voice,
10+}: {
11+ channel: Channel;
12+ username: string;
13+ voice: Voice;
14+}) {
15+ const active = voice.channel?.id === channel.id;
3616
37- room.on(RoomEvent.ParticipantConnected, sync);
38- room.on(RoomEvent.ParticipantDisconnected, sync);
39- room.on(RoomEvent.TrackSubscribed, (track) => {
40- if (track.kind === Track.Kind.Audio) audio.push(track.attach());
41- });
42- room.on(RoomEvent.Disconnected, () => {
43- if (!cancelled) {
44- setStatus("error");
45- setError("disconnected");
46- }
47- });
48-
49- await room.connect(t.url, t.token);
50- await room.localParticipant.setMicrophoneEnabled(true);
51- setStatus("live");
52- sync();
53- } catch (err) {
54- if (!cancelled) {
55- setStatus("error");
56- setError((err as Error).message);
57- }
58- }
59- })();
60-
61- return () => {
62- cancelled = true;
63- audio.forEach((el) => el.remove());
64- room?.disconnect();
65- roomRef.current = null;
66- };
67- }, [channel.id]);
68-
69- async function toggleMute() {
70- const room = roomRef.current;
71- if (!room) return;
72- await room.localParticipant.setMicrophoneEnabled(muted);
73- setMuted(!muted);
17+ if (!active) {
18+ return (
19+ <div className="flex flex-1 flex-col items-center justify-center gap-4 text-sm">
20+ <div className="text-sepia/50">not in {channel.name}</div>
21+ <Button variant="outline" onClick={() => voice.join(channel)}>
22+ <Volume2 />
23+ join {channel.name}
24+ </Button>
25+ </div>
26+ );
7427 }
7528
7629 return (
7730 <div className="flex flex-1 flex-col items-center justify-center gap-5 text-sm">
7831 <div className="text-sepia/50">
79- {status === "live"
80- ? `connected to ${channel.name}`
81- : status === "connecting"
82- ? "connecting…"
83- : "voice error"}
32+ {voice.status === "connecting"
33+ ? "connecting…"
34+ : voice.status === "error"
35+ ? "voice error"
36+ : `connected to ${channel.name}`}
8437 </div>
85- {error && <div className="text-gold">{error}</div>}
38+ {voice.error && <div className="text-gold">{voice.error}</div>}
8639 <div className="flex w-56 flex-col gap-1 rounded border border-sepia/15 p-3">
8740 <div className="flex items-center gap-2 text-sepia">
8841 <span className="h-2 w-2 rounded-full bg-gold" />
8942 {username} <span className="text-sepia/40">(you)</span>
43+ {voice.muted && <MicOff size={12} className="text-sepia/50" />}
9044 </div>
91- {peers.map((p) => (
45+ {voice.peers.map((p) => (
9246 <div key={p} className="flex items-center gap-2 text-sepia">
9347 <span className="h-2 w-2 rounded-full bg-gold" />
9448 {p}
9549 </div>
9650 ))}
9751 </div>
98- <Button variant="outline" onClick={toggleMute}>
99- {muted ? <MicOff /> : <Mic />}
100- {muted ? "unmute" : "mute"}
101- </Button>
52+ <div className="flex gap-2">
53+ <Button variant="outline" onClick={() => voice.toggleMute()}>
54+ {voice.muted ? <MicOff /> : <Mic />}
55+ {voice.muted ? "unmute" : "mute"}
56+ </Button>
57+ <Button variant="ghost" onClick={() => voice.leave()}>
58+ <PhoneOff />
59+ leave
60+ </Button>
61+ </div>
10262 </div>
10363 );
10464 }