2026-07-26 15:34:08 +07:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2026-07-28 12:17:58 +07:00
|
|
|
import { SubNav } from "@/components/layout/sub-nav";
|
2026-08-01 09:19:39 +07:00
|
|
|
import { VoiceActivityTimeline } from "@/components/voice/activity-timeline";
|
|
|
|
|
import { VoiceConnectionCard } from "@/components/voice/connection-card";
|
|
|
|
|
import { MicControl } from "@/components/voice/mic-control";
|
|
|
|
|
import { SpeakerWaveform } from "@/components/voice/speaker-waveform";
|
2026-07-26 16:14:32 +07:00
|
|
|
import {
|
|
|
|
|
useGuilds,
|
2026-07-26 16:55:20 +07:00
|
|
|
useMicTransmit,
|
2026-07-26 16:14:32 +07:00
|
|
|
useSpeakers,
|
|
|
|
|
useVoiceChannels,
|
2026-07-26 16:55:20 +07:00
|
|
|
useVoiceConnect,
|
|
|
|
|
useVoiceDisconnect,
|
2026-07-26 16:14:32 +07:00
|
|
|
useVoiceStatus,
|
|
|
|
|
} from "@/hooks";
|
2026-08-01 09:19:39 +07:00
|
|
|
import { useWebSocket } from "@/lib/ws/context";
|
2026-07-28 12:17:58 +07:00
|
|
|
|
|
|
|
|
type VoiceTab = "connection" | "activity";
|
2026-07-26 15:34:08 +07:00
|
|
|
|
|
|
|
|
export default function VoicePage() {
|
|
|
|
|
const ws = useWebSocket();
|
2026-07-26 16:55:20 +07:00
|
|
|
const { data: voiceStatus } = useVoiceStatus();
|
|
|
|
|
const { data: guilds = [] } = useGuilds();
|
2026-07-27 21:54:31 +07:00
|
|
|
const [selectedGuild, setSelectedGuild] = useState("");
|
|
|
|
|
const { data: voiceChannels = [] } = useVoiceChannels(selectedGuild);
|
2026-07-26 16:14:32 +07:00
|
|
|
const { speakers, subscribe } = useSpeakers();
|
2026-07-26 16:55:20 +07:00
|
|
|
const connectMut = useVoiceConnect();
|
|
|
|
|
const disconnectMut = useVoiceDisconnect();
|
|
|
|
|
const micMut = useMicTransmit();
|
2026-07-26 15:34:08 +07:00
|
|
|
const [selectedChannel, setSelectedChannel] = useState("");
|
|
|
|
|
const [micActive, setMicActive] = useState(false);
|
2026-07-28 12:17:58 +07:00
|
|
|
const [volume, setVolume] = useState(75);
|
|
|
|
|
const [tab, setTab] = useState<VoiceTab>("connection");
|
2026-07-26 15:34:08 +07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-07-26 16:14:32 +07:00
|
|
|
const unsub = subscribe(ws);
|
|
|
|
|
return () => unsub();
|
|
|
|
|
}, [ws, subscribe]);
|
2026-07-26 15:34:08 +07:00
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const handleMicToggle = useCallback(
|
|
|
|
|
async (checked: boolean) => {
|
|
|
|
|
setMicActive(checked);
|
|
|
|
|
try {
|
|
|
|
|
await micMut.mutateAsync(checked);
|
|
|
|
|
} catch {
|
|
|
|
|
setMicActive(!checked);
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
},
|
2026-07-27 21:54:31 +07:00
|
|
|
[micMut],
|
2026-07-26 16:14:32 +07:00
|
|
|
);
|
2026-07-26 15:34:08 +07:00
|
|
|
|
2026-07-28 12:17:58 +07:00
|
|
|
const handleGuildChange = useCallback((guildId: string | null) => {
|
|
|
|
|
if (!guildId) {
|
|
|
|
|
setSelectedGuild("");
|
|
|
|
|
setSelectedChannel("");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setSelectedGuild(guildId);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-07-26 15:34:08 +07:00
|
|
|
const activeSpeakers = speakers.filter((s) => s.speaking);
|
2026-07-27 21:54:31 +07:00
|
|
|
const connected = voiceStatus?.connected ?? false;
|
2026-07-26 15:34:08 +07:00
|
|
|
|
|
|
|
|
return (
|
2026-07-28 12:17:58 +07:00
|
|
|
<div className="space-y-4 animate-fade-in-up">
|
|
|
|
|
<SubNav
|
|
|
|
|
tabs={[
|
|
|
|
|
{ id: "connection", label: "Connection", icon: undefined },
|
|
|
|
|
{ id: "activity", label: "Activity", icon: undefined },
|
|
|
|
|
]}
|
|
|
|
|
activeTab={tab}
|
|
|
|
|
onTabChange={(t) => setTab(t as VoiceTab)}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
<VoiceConnectionCard
|
|
|
|
|
connected={connected}
|
|
|
|
|
activeChannelName={voiceStatus?.activeChannelName}
|
2026-07-28 12:17:58 +07:00
|
|
|
guilds={guilds}
|
|
|
|
|
voiceChannels={voiceChannels}
|
|
|
|
|
selectedGuild={selectedGuild}
|
|
|
|
|
selectedChannel={selectedChannel}
|
|
|
|
|
onGuildChange={handleGuildChange}
|
|
|
|
|
onChannelChange={(v) => setSelectedChannel(v ?? "")}
|
2026-08-01 09:19:39 +07:00
|
|
|
onConnect={() =>
|
|
|
|
|
connectMut.mutate({
|
|
|
|
|
guildId: selectedGuild,
|
|
|
|
|
channelId: selectedChannel,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-07-28 12:17:58 +07:00
|
|
|
onDisconnect={() => disconnectMut.mutate(undefined)}
|
|
|
|
|
connecting={connectMut.isPending}
|
2026-07-27 21:54:31 +07:00
|
|
|
/>
|
2026-07-28 12:17:58 +07:00
|
|
|
|
|
|
|
|
{tab === "connection" && (
|
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
|
|
|
|
|
<SpeakerWaveform speakers={activeSpeakers} />
|
|
|
|
|
<MicControl
|
|
|
|
|
connected={connected}
|
|
|
|
|
active={micActive}
|
|
|
|
|
onToggle={handleMicToggle}
|
|
|
|
|
volume={volume}
|
|
|
|
|
onVolumeChange={setVolume}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{tab === "activity" && <VoiceActivityTimeline />}
|
2026-07-26 15:34:08 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|