"use client" import { useState, useEffect, use, useCallback } from "react" import { Phone, PhoneOff, Mic, MicOff, Volume2, VolumeX, Users, Loader2 } from "lucide-react" import { useWebRTCCall } from "@/hooks/useWebRTCCall" export default function CallRoomPage({ params }: { params: Promise<{ roomId: string }> }) { const { roomId } = use(params) const { callState, isMuted, isSpeakerOn, callDuration, error, isReady, participants, joinRoom, endCall, toggleMute, toggleSpeaker, formatDuration, } = useWebRTCCall({ anonymous: true }) const [displayName, setDisplayName] = useState("") const [joined, setJoined] = useState(false) const [micError, setMicError] = useState(null) const [connectionTimeout, setConnectionTimeout] = useState(false) useEffect(() => { if (isReady) return const timer = setTimeout(() => { if (!isReady) setConnectionTimeout(true) }, 10000) return () => clearTimeout(timer) }, [isReady]) const handleJoin = useCallback(async () => { if (!displayName.trim()) return setMicError(null) try { await navigator.mediaDevices.getUserMedia({ audio: true }) joinRoom(roomId, displayName.trim() || "Anonymous") setJoined(true) } catch { setMicError("Microphone access is required to join the call. Please allow microphone access in your browser settings.") } }, [displayName, roomId, joinRoom]) const handleEnd = useCallback(() => { endCall() }, [endCall]) if (!joined) { return (
{connectionTimeout ? ( <>

This call is no longer available.

) : !isReady ? ( <>

Connecting to call...

) : ( <>

Join Call

{micError && (

{micError}

)} setDisplayName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") handleJoin() }} placeholder="Enter your name" className="bg-[#FAFAF6] dark:bg-[#1A1A1A] border border-[#D4D8CC] dark:border-[#333333] text-[#2D3020] dark:text-white placeholder-[#8A9078] dark:placeholder-[#555555] rounded-xl px-4 py-2.5 text-sm w-full mb-4 outline-none transition-colors focus:border-[#C84B4B] dark:focus:border-[#FF4444]" /> )}
) } return (
{callState === "calling" && (

Calling

Connecting to call room...

)} {callState === "waiting" && (

Waiting for participant

Share the call link to invite someone...

{participants.length > 0 && (
Participants ({participants.length})
{participants.map((p) => (
{p.label}
))}
)}
)} {callState === "participant_joined" && (

Participant joined

{participants.length > 0 && (
Participants ({participants.length})
{participants.map((p) => (
{p.label}
))}
)}

Connecting...

)} {callState === "connecting" && (

Connecting

Establishing secure connection...

)} {callState === "connected" && (

Connected

{participants.length > 0 && (
Participants ({participants.length})
{participants.map((p) => (
{p.label}
))}
)}
{formatDuration(callDuration)}
)} {(callState === "ended" || callState === "idle") && (

Call ended

{callDuration > 0 && (

Duration: {formatDuration(callDuration)}

)}
)} {error && (

{error}

)}
) }