-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmedia-room.tsx
101 lines (85 loc) Β· 2 KB
/
media-room.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"use client";
import axios from "@/lib/axios";
import { useCallStore } from "@/store/call-store";
import { useUser } from "@clerk/nextjs";
import { LiveKitRoom, VideoConference } from "@livekit/components-react";
import "@livekit/components-styles";
import { Loader2 } from "lucide-react";
import { useRouter } from "next/navigation";
import qs from "query-string";
import { useEffect, useState } from "react";
interface MediaRoomProps {
chatId: string;
audio: boolean;
video: boolean;
apiUrl: string;
type: "channel" | "conversation";
profileId: string;
}
const MediaRoom = ({
chatId,
audio,
video,
type,
profileId,
}: MediaRoomProps) => {
const { user } = useUser();
const [token, setToken] = useState("");
const router = useRouter();
const { ongoingCall } = useCallStore();
const handleOnDisconnected = () => {
router.back();
if (type === "conversation") {
const url = qs.stringifyUrl({
url: "/api/socket/call",
});
const payload = {
callId: ongoingCall?.id,
isEnded: true,
endedBy: profileId,
};
axios.patch(url, payload);
return;
}
};
useEffect(() => {
if (!user?.fullName) {
return;
}
const name = user?.fullName?.trim();
(async () => {
try {
const res = await fetch(`/api/livekit?room=${chatId}&username=${name}`);
const data = await res.json();
setToken(data.token);
} catch (error) {
console.error("[MediaRoom]", error);
}
})();
}, [chatId, user]);
if (token === "") {
return (
<div className="flex flex-col flex-1 justify-center items-center">
<Loader2
className="animate-spin h-7 w-7 text-zinc-500 my-4"
size={48}
/>
<p className="text-zinc-500 dark:text-zinc-400 text-sm">Loading...</p>
</div>
);
}
return (
<LiveKitRoom
data-lk-theme="default"
serverUrl={process.env.NEXT_PUBLIC_LIVEKIT_URL}
token={token}
connect={true}
video={video}
audio={audio}
onDisconnected={handleOnDisconnected}
>
<VideoConference />
</LiveKitRoom>
);
};
export default MediaRoom;