xonecas.io
4828
Sr

stream-together / commit 50865c7

Start refactoring UI into smaller components

Added loading indicator
Refactored buttons into their own files
Added a component for call settings

sean2024-06-28 50865c73a0c45c3acc1ea618ef3bd14274d3f553 parent fc5e8e3

7 files changed

components/agora-host-video.tsx+58−109
@@ -11,20 +11,20 @@ import AgoraRTC, {
1111 useRemoteUsers,
1212 } from "agora-rtc-react";
1313 import { useCallback, useEffect, useState } from "react";
14-import { AiOutlineAudio as UnmuteIcon } from "react-icons/ai";
15-import { AiOutlineAudioMuted as MuteIcon } from "react-icons/ai";
16-import { CiStreamOn as StreamOnIcon } from "react-icons/ci";
17-import { CiStreamOff as StreamOffIcon } from "react-icons/ci";
18-import { GoGear as GearIcon } from "react-icons/go";
19-import { IoVideocamOutline as VideoOnIcon } from "react-icons/io5";
20-import { IoVideocamOffOutline as VideoOffIcon } from "react-icons/io5";
14+import { ImSpinner10 } from "react-icons/im";
2115
2216 import useNoiseReduction from "@/hooks/agora-ai-noise-reduction";
2317 import useVirtualBackground from "@/hooks/agora-virtual-background";
2418
19+import EndStreamButton from "./end-stream-button";
2520 import { Modal } from "./modal";
21+import MuteButton from "./mute-button";
2622 import PrimaryButton from "./primary-button";
2723 import SecondaryButton from "./secondary-button";
24+import SettingsButton from "./settings-button";
25+import StartStreamButton from "./start-stream-button";
26+import StreamSettings from "./stream-settings";
27+import VideoToggleButton from "./video-toggle-button";
2828
2929 const appId = process.env.NEXT_PUBLIC_APP_ID ?? "";
3030 const token = process.env.NEXT_PUBLIC_TOKEN ?? "";
@@ -111,108 +111,57 @@ export default function AgoraHost() {
111111
112112 return (
113113 <div className="flex flex-col md:flex-row gap-8 w-full justify-center items-center">
114- <div className="rounded-3xl overflow-hidden bg-black w-full max-w-2xl aspect-[4/3]">
115- <LocalUser
116- audioTrack={localMicrophoneTrack}
117- videoTrack={localCameraTrack}
118- cameraOn={videoStarted}
119- micOn={audioStarted}
120- >
121- <div className="flex flex-col justify-between h-full p-2">
122- <p className="p-2">Your broadcast</p>
123- {callStarted && isConnected ? (
124- <div className="flex flex-row justify-center gap-4">
125- <button
126- onClick={handleToggleAudio}
127- title={audioStarted ? "Mute" : "Unmute"}
128- >
129- <SecondaryButton>
130- {audioStarted ? (
131- <UnmuteIcon className="p-2" size="3em" />
132- ) : (
133- <MuteIcon className="p-2" size="3em" />
134- )}
135- </SecondaryButton>
136- </button>
137- <button
138- onClick={handleToggleVideo}
139- title={videoStarted ? "Turn video off" : "Turn video on"}
140- >
141- <SecondaryButton>
142- {videoStarted ? (
143- <VideoOffIcon className="p-2" size="3em" />
144- ) : (
145- <VideoOnIcon className="p-2" size="3em" />
146- )}
147- </SecondaryButton>
148- </button>
149- <button onClick={handleEndCall} title="End stream">
150- <PrimaryButton>
151- <StreamOffIcon className="p-1" size="3em" />
152- </PrimaryButton>
153- </button>
154- </div>
155- ) : callStarted ? (
156- <div className="p-4">Loading...</div>
157- ) : (
158- <div className="flex flex-row justify-center gap-4">
159- <button onClick={handleStarCall} title="Go live!">
160- <PrimaryButton>
161- <div className="flex flex-row items-center px-2 gap-2">
162- <p>Go live!</p>
163- <StreamOnIcon className="p-1" size="3em" />
164- </div>
165- </PrimaryButton>
166- </button>
167- </div>
168- )}
169- </div>
170- </LocalUser>
171- <Modal
172- openButton={
173- <button onClick={() => setShowSettingsModal(!showSettingsModal)}>
174- <SecondaryButton>
175- <div className="py-2 px-4 flex flex-row gap-2 items-center">
176- <GearIcon size="2em" />
177- <p>Settings</p>
114+ <div className="w-full flex flex-col gap-2 max-w-2xl">
115+ <div className="rounded-3xl overflow-hidden bg-black w-full aspect-[4/3]">
116+ <LocalUser
117+ audioTrack={localMicrophoneTrack}
118+ videoTrack={localCameraTrack}
119+ cameraOn={videoStarted}
120+ micOn={audioStarted}
121+ >
122+ <div className="flex flex-col justify-between h-full p-2">
123+ <p className="p-2">Your broadcast</p>
124+ {callStarted && isConnected ? (
125+ <div className="flex flex-row justify-center gap-4">
126+ <MuteButton
127+ audioStarted={audioStarted}
128+ handleToggleAudio={handleToggleAudio}
129+ />
130+ <VideoToggleButton
131+ videoStarted={videoStarted}
132+ handleToggleVideo={handleToggleVideo}
133+ />
134+ <EndStreamButton handleEndCall={handleEndCall} />
178135 </div>
179- </SecondaryButton>
180- </button>
181- }
182- showModal={showSettingsModal}
183- >
184- <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4">
185- <div>
186- <h4 className="font-bold pb-4">Select your video device:</h4>
187- {availableCameras.map((c) => {
188- return (
189- <label key={c.id} className="flex flex-row gap-2">
190- <input
191- type="radio"
192- name="selected-camera"
193- value={c.id}
194- checked={c.id === selectedCameraId}
195- onChange={(e) => {
196- const id = e.target.value;
197- setSelectedCameraId(id);
198- }}
199- />
200- <div>{c.label}</div>
201- </label>
202- );
203- })}
204- </div>
205-
206- <div>
207- <h4 className="font-bold pb-4">Blur your background?</h4>
208- <div className="flex flex-row justify-start">
209- <button onClick={handleBlurBackground}>
210- <SecondaryButton>
211- <p className="px-4 py-2">Toggle</p>
212- </SecondaryButton>
213- </button>
214- </div>
136+ ) : callStarted ? (
137+ <div className="flex flex-col flex-grow items-center justify-center">
138+ <ImSpinner10 size="4rem" className="animate-spin" />
139+ </div>
140+ ) : (
141+ <div className="flex flex-row flex-grow justify-center">
142+ <StartStreamButton handleStartCall={handleStarCall} />
143+ </div>
144+ )}
215145 </div>
146+ </LocalUser>
147+ </div>
148+
149+ <div className="text-center">
150+ <Modal
151+ openButton={
152+ <SettingsButton
153+ showSettingsModal={showSettingsModal}
154+ setShowSettingsModal={setShowSettingsModal}
155+ />
156+ }
157+ showModal={showSettingsModal}
158+ >
159+ <StreamSettings
160+ availableCameras={availableCameras}
161+ selectedCameraId={selectedCameraId}
162+ setSelectedCameraId={setSelectedCameraId}
163+ handleBlurBackground={handleBlurBackground}
164+ />
216165
217166 <div className="text-center">
218167 <button onClick={() => setShowSettingsModal(false)}>
@@ -221,8 +170,8 @@ export default function AgoraHost() {
221170 </PrimaryButton>
222171 </button>
223172 </div>
224- </div>
225- </Modal>
173+ </Modal>
174+ </div>
226175 </div>
227176
228177 {otherHosts.map((host) => {
components/end-stream-button.tsx+18−0
@@ -0,0 +1,18 @@
1+import { CiStreamOff as StreamOffIcon } from "react-icons/ci";
2+
3+import PrimaryButton from "./primary-button";
4+
5+export default function EndStreamButton({
6+ handleEndCall,
7+}: Readonly<{ handleEndCall: () => void }>) {
8+ return (
9+ <button onClick={handleEndCall} title="End stream">
10+ <PrimaryButton>
11+ <div className="flex flex-row items-center gap-2 p-2">
12+ <StreamOffIcon size="1.5em" />
13+ <p>End</p>
14+ </div>
15+ </PrimaryButton>
16+ </button>
17+ );
18+}
components/mute-button.tsx+30−0
@@ -0,0 +1,30 @@
1+import { TbMicrophone as UnmuteIcon } from "react-icons/tb";
2+import { TbMicrophoneOff as MuteIcon } from "react-icons/tb";
3+
4+import SecondaryButton from "./secondary-button";
5+
6+export default function MuteButton({
7+ audioStarted,
8+ handleToggleAudio,
9+}: Readonly<{
10+ audioStarted: boolean;
11+ handleToggleAudio: () => void;
12+}>) {
13+ return (
14+ <button
15+ onClick={handleToggleAudio}
16+ title={audioStarted ? "Mute" : "Unmute"}
17+ >
18+ <SecondaryButton>
19+ <div className="flex flex-row items-center gap-2 p-2">
20+ {audioStarted ? (
21+ <UnmuteIcon size="1.5em" />
22+ ) : (
23+ <MuteIcon size="1.5em" />
24+ )}
25+ <p>{audioStarted ? "Mute" : "Unmute"}</p>
26+ </div>
27+ </SecondaryButton>
28+ </button>
29+ );
30+}
components/settings-button.tsx+22−0
@@ -0,0 +1,22 @@
1+import { GoGear as GearIcon } from "react-icons/go";
2+
3+import SecondaryButton from "./secondary-button";
4+
5+export default function SettingsButton({
6+ showSettingsModal,
7+ setShowSettingsModal,
8+}: Readonly<{
9+ showSettingsModal: boolean;
10+ setShowSettingsModal: (arg0: boolean) => void;
11+}>) {
12+ return (
13+ <button onClick={() => setShowSettingsModal(!showSettingsModal)}>
14+ <SecondaryButton>
15+ <div className="p-2 flex flex-row gap-2 items-center">
16+ <GearIcon size="1.5em" />
17+ <p>Settings</p>
18+ </div>
19+ </SecondaryButton>
20+ </button>
21+ );
22+}
components/start-stream-button.tsx+18−0
@@ -0,0 +1,18 @@
1+import { CiStreamOn as StreamOnIcon } from "react-icons/ci";
2+
3+import PrimaryButton from "./primary-button";
4+
5+export default function StartStreamButton({
6+ handleStartCall,
7+}: Readonly<{ handleStartCall: () => void }>) {
8+ return (
9+ <button onClick={handleStartCall} title="End stream">
10+ <PrimaryButton>
11+ <div className="flex flex-row items-center gap-2 p-2">
12+ <StreamOnIcon size="1.5em" />
13+ <p>Go live!</p>
14+ </div>
15+ </PrimaryButton>
16+ </button>
17+ );
18+}
components/stream-settings.tsx+53−0
@@ -0,0 +1,53 @@
1+import PrimaryButton from "./primary-button";
2+import SecondaryButton from "./secondary-button";
3+
4+export default function StreamSettings({
5+ availableCameras,
6+ selectedCameraId,
7+ setSelectedCameraId,
8+ // availableMicrophones,
9+ // selectedMicrophoneId,
10+ // setSelectedMicrophone,
11+ handleBlurBackground,
12+}: Readonly<{
13+ availableCameras: { id: string; label: string }[];
14+ selectedCameraId: string;
15+ setSelectedCameraId: (agr0: string) => void;
16+ handleBlurBackground: () => void;
17+}>) {
18+ return (
19+ <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4 text-left">
20+ <div>
21+ <h4 className="font-bold pb-4">Select your video device:</h4>
22+ {availableCameras.map((c) => {
23+ return (
24+ <label key={c.id} className="flex flex-row gap-2">
25+ <input
26+ type="radio"
27+ name="selected-camera"
28+ value={c.id}
29+ checked={c.id === selectedCameraId}
30+ onChange={(e) => {
31+ const id = e.target.value;
32+ setSelectedCameraId(id);
33+ }}
34+ />
35+ <div>{c.label}</div>
36+ </label>
37+ );
38+ })}
39+ </div>
40+
41+ <div>
42+ <h4 className="font-bold pb-4">Blur your background?</h4>
43+ <div className="flex flex-row justify-start">
44+ <button onClick={handleBlurBackground}>
45+ <SecondaryButton>
46+ <p className="px-4 py-2">Toggle</p>
47+ </SecondaryButton>
48+ </button>
49+ </div>
50+ </div>
51+ </div>
52+ );
53+}
components/video-toggle-button.tsx+30−0
@@ -0,0 +1,30 @@
1+import { IoVideocamOutline as VideoOnIcon } from "react-icons/io5";
2+import { IoVideocamOffOutline as VideoOffIcon } from "react-icons/io5";
3+
4+import SecondaryButton from "./secondary-button";
5+
6+export default function VideoToggleButton({
7+ videoStarted,
8+ handleToggleVideo,
9+}: Readonly<{
10+ videoStarted: boolean;
11+ handleToggleVideo: () => void;
12+}>) {
13+ return (
14+ <button
15+ onClick={handleToggleVideo}
16+ title={videoStarted ? "Turn video off" : "Turn video on"}
17+ >
18+ <SecondaryButton>
19+ <div className="flex flex-row items-center gap-2 p-2">
20+ {videoStarted ? (
21+ <VideoOnIcon size="1.5em" />
22+ ) : (
23+ <VideoOffIcon size="1.5em" />
24+ )}
25+ <p>{videoStarted ? "Turn video off" : "Turn video on"}</p>
26+ </div>
27+ </SecondaryButton>
28+ </button>
29+ );
30+}