-
Notifications
You must be signed in to change notification settings - Fork 51
/
media-room.tsx
53 lines (50 loc) · 1.37 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
"use client";
import { useUser } from "@clerk/nextjs";
import { Loader2 } from "lucide-react";
import { useEffect, useState } from "react";
import { LiveKitRoom, VideoConference } from "@livekit/components-react";
import "@livekit/components-styles";
interface props {
chatId: string;
video: boolean;
audio: boolean;
}
export const MediaRoom = ({ chatId, video, audio }: props) => {
const { user } = useUser();
const [token, setToken] = useState("");
useEffect(() => {
if (!user?.fullName) return;
const name = user.fullName;
(async () => {
try {
const response = await fetch(
`/api/livekit?room=${chatId}&username=${name}`
);
const data = await response.json();
setToken(data.token);
} catch (error) {
console.log(error);
}
})();
}, [user?.fullName, chatId]);
if (token === "") {
return (
<div className="flex flex-col flex-1 justify-center items-center">
<Loader2 className="h-7 w-7 text-zinc-500 animate-spin my-4" />
<p className="text-xs dark:text-zinc-400 text-zinc-500">Loading ...</p>
</div>
);
}
return (
<LiveKitRoom
data-lk-theme="default"
serverUrl={process.env.NEXT_PUBLIC_LIVEKIT_URL}
token={token}
connect={true}
video={video}
audio={audio}
>
<VideoConference />
</LiveKitRoom>
);
};