2026-08-07 10:44:03 +07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-26 10:39:00 +07:00
|
|
|
import { Mic, PhoneOff, Radio, Volume2 } from "lucide-react";
|
2026-08-24 16:32:16 +07:00
|
|
|
import { useEffect, useState } from "react";
|
2026-08-15 20:03:55 +07:00
|
|
|
import { useAmbient } from "@/components/ambient/ambient-context";
|
2026-08-24 16:32:16 +07:00
|
|
|
import { Button, GlassPanel, toast } from "@/components/primitives";
|
2026-08-24 15:44:10 +07:00
|
|
|
import {
|
2026-08-24 16:32:16 +07:00
|
|
|
ErrorState,
|
2026-08-26 03:37:14 +07:00
|
|
|
PageTransition,
|
2026-08-24 16:32:16 +07:00
|
|
|
SectionHeader,
|
|
|
|
|
SkeletonPanel,
|
|
|
|
|
} from "@/components/shared";
|
|
|
|
|
import { GuildChannelPicker } from "@/components/shared/guild-picker";
|
2026-08-15 20:03:55 +07:00
|
|
|
import { VoiceStage } from "@/components/voice/voice-stage";
|
|
|
|
|
import {
|
|
|
|
|
useMicTransmit,
|
|
|
|
|
useSpeakers,
|
2026-08-07 10:44:03 +07:00
|
|
|
useVoiceConnect,
|
|
|
|
|
useVoiceDisconnect,
|
|
|
|
|
useVoiceListen,
|
2026-08-15 20:03:55 +07:00
|
|
|
useVoiceStatus,
|
2026-08-07 10:44:03 +07:00
|
|
|
} from "@/hooks";
|
2026-08-25 23:23:19 +07:00
|
|
|
import { useStaggerReveal } from "@/hooks/use-gsap-animation";
|
2026-08-15 17:53:48 +07:00
|
|
|
import type { Guild, VoiceStatus } from "@/lib/types";
|
2026-08-15 20:03:55 +07:00
|
|
|
import { useWebSocket } from "@/lib/ws/context";
|
2026-08-07 10:44:03 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
export function VoiceView({
|
|
|
|
|
initialStatus,
|
|
|
|
|
initialGuilds,
|
|
|
|
|
}: {
|
|
|
|
|
initialStatus?: VoiceStatus;
|
|
|
|
|
initialGuilds?: Guild[];
|
|
|
|
|
}) {
|
2026-08-07 10:44:03 +07:00
|
|
|
const ws = useWebSocket();
|
2026-08-25 13:46:15 +07:00
|
|
|
const {
|
|
|
|
|
data: status,
|
|
|
|
|
isLoading,
|
|
|
|
|
error,
|
|
|
|
|
mutate,
|
|
|
|
|
} = useVoiceStatus(initialStatus);
|
2026-08-14 10:49:44 +07:00
|
|
|
const connect = useVoiceConnect();
|
|
|
|
|
const disconnect = useVoiceDisconnect();
|
2026-08-14 12:35:44 +07:00
|
|
|
const mic = useMicTransmit(ws);
|
2026-08-15 17:53:48 +07:00
|
|
|
const listen = useVoiceListen(ws);
|
|
|
|
|
const { speakers, subscribe } = useSpeakers(initialStatus?.activeSpeakers);
|
|
|
|
|
const ambient = useAmbient();
|
|
|
|
|
|
|
|
|
|
const [guildId, setGuildId] = useState<string | null>(
|
|
|
|
|
initialStatus?.activeGuildId ?? initialGuilds?.[0]?.id ?? null,
|
|
|
|
|
);
|
|
|
|
|
const [channelId, setChannelId] = useState<string | null>(
|
|
|
|
|
initialStatus?.activeChannelId ?? null,
|
|
|
|
|
);
|
2026-08-22 12:53:18 +07:00
|
|
|
const [micVol, setMicVol] = useState(100);
|
|
|
|
|
const [listenVol, setListenVol] = useState(75);
|
2026-08-26 23:16:57 +07:00
|
|
|
const [micActive, setMicActive] = useState(false);
|
|
|
|
|
const [listenActive, setListenActive] = useState(false);
|
|
|
|
|
|
|
|
|
|
const toggleMic = async () => {
|
|
|
|
|
const next = !micActive;
|
|
|
|
|
try {
|
|
|
|
|
await mic.mutateAsync(next);
|
|
|
|
|
setMicActive(next);
|
|
|
|
|
toast({
|
|
|
|
|
title: next ? "Mic activated" : "Mic deactivated",
|
|
|
|
|
tone: next ? "signal" : "neutral",
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
toast({ title: "Mic toggle failed", description: String(e), tone: "vermilion" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const toggleListen = () => {
|
|
|
|
|
const next = !listenActive;
|
|
|
|
|
listen.toggle(next);
|
|
|
|
|
setListenActive(next);
|
|
|
|
|
toast({
|
|
|
|
|
title: next ? "Monitor activated" : "Monitor deactivated",
|
|
|
|
|
tone: next ? "signal" : "neutral",
|
|
|
|
|
});
|
|
|
|
|
};
|
2026-08-07 10:44:03 +07:00
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
const containerRef = useStaggerReveal<HTMLDivElement>(".voice-tile", {
|
|
|
|
|
stagger: 0.04,
|
|
|
|
|
y: 8,
|
2026-08-25 23:23:19 +07:00
|
|
|
dependencies: [status],
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-07 10:44:03 +07:00
|
|
|
useEffect(() => {
|
|
|
|
|
const unsub = subscribe(ws);
|
2026-08-14 10:49:44 +07:00
|
|
|
return unsub;
|
|
|
|
|
}, [subscribe, ws]);
|
2026-08-07 10:44:03 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
useEffect(() => {
|
2026-08-26 03:37:14 +07:00
|
|
|
if (status?.connected) ambient.set("signal", 0.45, "voice active");
|
|
|
|
|
else ambient.set("vermilion", 0.25, "voice disconnected");
|
2026-08-15 17:53:48 +07:00
|
|
|
}, [status?.connected, ambient]);
|
2026-08-14 11:28:08 +07:00
|
|
|
|
2026-08-25 13:46:15 +07:00
|
|
|
if (error && !status)
|
|
|
|
|
return <ErrorState error={error} onRetry={() => void mutate()} />;
|
2026-08-18 09:21:12 +07:00
|
|
|
if (!status && isLoading)
|
|
|
|
|
return (
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="space-y-4">
|
|
|
|
|
<SkeletonPanel rows={3} />
|
|
|
|
|
<SkeletonPanel rows={5} />
|
2026-08-24 16:32:16 +07:00
|
|
|
</div>
|
2026-08-18 09:21:12 +07:00
|
|
|
);
|
2026-08-15 17:53:48 +07:00
|
|
|
|
|
|
|
|
const connected = status?.connected ?? false;
|
|
|
|
|
|
|
|
|
|
const onConnect = async () => {
|
|
|
|
|
if (!guildId || !channelId) {
|
2026-08-26 03:37:14 +07:00
|
|
|
toast({ title: "Select target channel", tone: "vermilion" });
|
2026-08-15 17:53:48 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await connect.mutateAsync({ guildId, channelId });
|
2026-08-26 03:37:14 +07:00
|
|
|
toast({ title: "Connected to voice channel", tone: "signal" });
|
2026-08-15 17:53:48 +07:00
|
|
|
} catch (e) {
|
2026-08-15 20:03:55 +07:00
|
|
|
toast({
|
2026-08-26 03:37:14 +07:00
|
|
|
title: "Connection failed",
|
2026-08-15 20:03:55 +07:00
|
|
|
description: String(e),
|
|
|
|
|
tone: "vermilion",
|
|
|
|
|
});
|
2026-08-14 12:35:44 +07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
const onDisconnect = async () => {
|
2026-08-15 17:53:48 +07:00
|
|
|
try {
|
2026-08-26 03:37:14 +07:00
|
|
|
await disconnect.mutateAsync();
|
|
|
|
|
toast({ title: "Disconnected from voice", tone: "neutral" });
|
2026-08-15 17:53:48 +07:00
|
|
|
} catch (e) {
|
2026-08-26 03:37:14 +07:00
|
|
|
toast({
|
|
|
|
|
title: "Disconnect failed",
|
|
|
|
|
description: String(e),
|
|
|
|
|
tone: "vermilion",
|
|
|
|
|
});
|
2026-08-15 17:53:48 +07:00
|
|
|
}
|
2026-08-14 11:28:08 +07:00
|
|
|
};
|
2026-08-07 10:44:03 +07:00
|
|
|
|
|
|
|
|
return (
|
2026-08-26 03:37:14 +07:00
|
|
|
<PageTransition>
|
|
|
|
|
<div ref={containerRef} className="space-y-4">
|
|
|
|
|
{/* Precision Sub-Header Bar */}
|
2026-08-26 14:57:08 +07:00
|
|
|
<div className="voice-tile flex flex-wrap items-center justify-between gap-3 border-b border-hairline pb-3">
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="flex items-center gap-2.5">
|
2026-08-25 23:23:19 +07:00
|
|
|
<span
|
2026-08-26 03:37:14 +07:00
|
|
|
className={`h-2 w-2 rounded-full ${
|
2026-08-24 16:32:16 +07:00
|
|
|
connected
|
2026-08-26 14:57:08 +07:00
|
|
|
? "bg-success shadow-[0_0_8px_var(--color-status-success)]"
|
|
|
|
|
: "bg-ink-muted"
|
2026-08-26 03:37:14 +07:00
|
|
|
}`}
|
2026-08-24 16:32:16 +07:00
|
|
|
/>
|
2026-08-26 14:57:08 +07:00
|
|
|
<h1 className="font-mono text-xs font-semibold tracking-wide text-ink uppercase">
|
2026-08-26 03:37:14 +07:00
|
|
|
Voice Spectrum & Stage Controls
|
|
|
|
|
</h1>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2 font-mono text-[11px]">
|
2026-08-26 14:57:08 +07:00
|
|
|
<span className="text-ink-muted">STAGE:</span>
|
2026-08-26 03:37:14 +07:00
|
|
|
<span
|
|
|
|
|
className={`rounded px-1.5 py-0.5 font-medium border ${
|
|
|
|
|
connected
|
2026-08-26 14:57:08 +07:00
|
|
|
? "border-success/30 bg-success/10 text-success"
|
|
|
|
|
: "border-hairline bg-surface-2 text-ink-muted"
|
2026-08-26 03:37:14 +07:00
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{connected ? "LIVE_BRIDGE" : "OFFLINE"}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-08-24 16:32:16 +07:00
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
{/* Channel Router Stage */}
|
|
|
|
|
<div className="voice-tile">
|
|
|
|
|
<GlassPanel className="p-4">
|
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
|
|
|
<div className="flex-1 min-w-[240px]">
|
|
|
|
|
<GuildChannelPicker
|
|
|
|
|
guildId={guildId}
|
|
|
|
|
channelId={channelId}
|
|
|
|
|
guildsInitial={initialGuilds}
|
|
|
|
|
onChange={(g, c) => {
|
|
|
|
|
setGuildId(g);
|
|
|
|
|
setChannelId(c);
|
|
|
|
|
}}
|
|
|
|
|
mode="voice"
|
|
|
|
|
/>
|
2026-08-24 16:32:16 +07:00
|
|
|
</div>
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
{connected ? (
|
|
|
|
|
<Button
|
|
|
|
|
variant="danger"
|
|
|
|
|
size="md"
|
|
|
|
|
onClick={onDisconnect}
|
|
|
|
|
className="gap-1.5"
|
|
|
|
|
>
|
|
|
|
|
<PhoneOff className="size-3.5" />
|
|
|
|
|
Disconnect
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="md"
|
|
|
|
|
onClick={onConnect}
|
|
|
|
|
className="gap-1.5"
|
|
|
|
|
>
|
|
|
|
|
<Radio className="size-3.5" />
|
|
|
|
|
Connect Bridge
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2026-08-24 16:32:16 +07:00
|
|
|
</div>
|
2026-08-26 03:37:14 +07:00
|
|
|
</div>
|
|
|
|
|
</GlassPanel>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Voice Stage Grid */}
|
|
|
|
|
<div className="grid gap-3 lg:grid-cols-3">
|
|
|
|
|
<GlassPanel className="voice-tile lg:col-span-2">
|
|
|
|
|
<SectionHeader
|
|
|
|
|
eyebrow="Participants"
|
|
|
|
|
title="Active Stage Members"
|
|
|
|
|
/>
|
|
|
|
|
<div className="mt-4 min-h-[160px]">
|
|
|
|
|
<VoiceStage speakers={speakers} />
|
|
|
|
|
</div>
|
|
|
|
|
</GlassPanel>
|
|
|
|
|
|
|
|
|
|
<GlassPanel className="voice-tile flex flex-col justify-between">
|
|
|
|
|
<div>
|
2026-08-26 10:39:00 +07:00
|
|
|
<SectionHeader eyebrow="Telemetry" title="Input / Output Mix" />
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="mt-4 space-y-4">
|
2026-08-26 23:16:57 +07:00
|
|
|
{/* Mic Toggle */}
|
2026-08-26 03:37:14 +07:00
|
|
|
<div>
|
2026-08-26 23:16:57 +07:00
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={toggleMic}
|
|
|
|
|
className={`flex items-center gap-1.5 rounded-[8px] px-3 py-1.5 text-xs font-medium transition-all ${
|
|
|
|
|
micActive
|
|
|
|
|
? "bg-signal/15 text-signal border border-signal/40 glow-pulse"
|
|
|
|
|
: "bg-surface-2 text-ink-muted border border-hairline hover:border-signal/30 hover:text-ink"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<Mic className="size-3.5" />
|
|
|
|
|
{micActive ? "MIC LIVE" : "MIC OFF"}
|
|
|
|
|
</button>
|
2026-08-26 14:57:08 +07:00
|
|
|
<span className="font-mono text-[11px] text-ink-muted">
|
2026-08-26 03:37:14 +07:00
|
|
|
{micVol}%
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<input
|
|
|
|
|
type="range"
|
|
|
|
|
min={0}
|
|
|
|
|
max={150}
|
|
|
|
|
value={micVol}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
const v = Number(e.target.value);
|
|
|
|
|
setMicVol(v);
|
|
|
|
|
mic.setVolume(v / 100);
|
|
|
|
|
}}
|
2026-08-26 14:57:08 +07:00
|
|
|
className="mt-2 h-1.5 w-full appearance-none rounded-full bg-surface-2 accent-signal"
|
2026-08-26 03:37:14 +07:00
|
|
|
/>
|
2026-08-26 23:16:57 +07:00
|
|
|
{/* Live mic level meter */}
|
|
|
|
|
{micActive && (
|
|
|
|
|
<div className="mt-1.5 h-1 w-full overflow-hidden rounded-full bg-surface-2">
|
|
|
|
|
<div
|
|
|
|
|
className="h-full rounded-full bg-signal transition-all duration-100"
|
|
|
|
|
style={{ width: `${Math.min(100, mic.micLevel * 100)}%` }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-08-26 03:37:14 +07:00
|
|
|
</div>
|
|
|
|
|
|
2026-08-26 23:16:57 +07:00
|
|
|
{/* Listen Toggle */}
|
2026-08-26 03:37:14 +07:00
|
|
|
<div>
|
2026-08-26 23:16:57 +07:00
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={toggleListen}
|
|
|
|
|
className={`flex items-center gap-1.5 rounded-[8px] px-3 py-1.5 text-xs font-medium transition-all ${
|
|
|
|
|
listenActive
|
|
|
|
|
? "bg-success/15 text-success border border-success/40 glow-pulse"
|
|
|
|
|
: "bg-surface-2 text-ink-muted border border-hairline hover:border-success/30 hover:text-ink"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<Volume2 className="size-3.5" />
|
|
|
|
|
{listenActive ? "MONITOR LIVE" : "MONITOR OFF"}
|
|
|
|
|
</button>
|
2026-08-26 14:57:08 +07:00
|
|
|
<span className="font-mono text-[11px] text-ink-muted">
|
2026-08-26 03:37:14 +07:00
|
|
|
{listenVol}%
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<input
|
|
|
|
|
type="range"
|
|
|
|
|
min={0}
|
|
|
|
|
max={150}
|
|
|
|
|
value={listenVol}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
const v = Number(e.target.value);
|
|
|
|
|
setListenVol(v);
|
|
|
|
|
listen.setVolume(v / 100);
|
|
|
|
|
}}
|
2026-08-26 14:57:08 +07:00
|
|
|
className="mt-2 h-1.5 w-full appearance-none rounded-full bg-surface-2 accent-success"
|
2026-08-26 03:37:14 +07:00
|
|
|
/>
|
2026-08-26 23:16:57 +07:00
|
|
|
{/* Per-speaker level meters */}
|
|
|
|
|
{listenActive && listen.levels.size > 0 && (
|
|
|
|
|
<div className="mt-2 space-y-1">
|
|
|
|
|
{Array.from(listen.levels.entries()).map(([hash, level]) => (
|
|
|
|
|
<div key={hash} className="flex items-center gap-2">
|
|
|
|
|
<span className="font-mono text-[9px] text-ink-faint w-8">
|
|
|
|
|
#{hash.toString(16).slice(-3)}
|
|
|
|
|
</span>
|
|
|
|
|
<div className="h-1 flex-1 overflow-hidden rounded-full bg-surface-2">
|
|
|
|
|
<div
|
|
|
|
|
className="h-full rounded-full bg-success transition-all duration-100"
|
|
|
|
|
style={{ width: `${Math.min(100, level * 100)}%` }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-08-26 03:37:14 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-08-26 14:57:08 +07:00
|
|
|
<div className="mt-6 border-t border-hairline pt-3">
|
|
|
|
|
<div className="font-mono text-[10px] text-ink-muted">
|
2026-08-26 03:37:14 +07:00
|
|
|
CODEC: OPUS 48KHZ · LOW_LATENCY
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</GlassPanel>
|
|
|
|
|
</div>
|
2026-08-24 16:32:16 +07:00
|
|
|
</div>
|
2026-08-26 03:37:14 +07:00
|
|
|
</PageTransition>
|
2026-08-07 10:44:03 +07:00
|
|
|
);
|
|
|
|
|
}
|