/
App.js
183 lines (165 loc) · 5.49 KB
/
App.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import './App.css';
import React, { useEffect, useState, useCallback } from 'react';
import DailyIframe from '@daily-co/daily-js';
import { DailyProvider } from '@daily-co/daily-react-hooks';
import api from './api';
import { roomUrlFromPageUrl, pageUrlFromRoomUrl } from './utils';
import HomeScreen from './components/HomeScreen/HomeScreen';
import Call from './components/Call/Call';
import Header from './components/Header/Header';
import Tray from './components/Tray/Tray';
/* We decide what UI to show to users based on the state of the app, which is dependent on the state of the call object: see line 137. */
const STATE_IDLE = 'STATE_IDLE';
const STATE_CREATING = 'STATE_CREATING';
const STATE_JOINING = 'STATE_JOINING';
const STATE_JOINED = 'STATE_JOINED';
const STATE_LEAVING = 'STATE_LEAVING';
const STATE_ERROR = 'STATE_ERROR';
export default function App() {
const [appState, setAppState] = useState(STATE_IDLE);
const [roomUrl, setRoomUrl] = useState(null);
const [callObject, setCallObject] = useState(null);
const [apiError, setApiError] = useState(false);
/**
* Show the call UI if we're either joining, already joined, or are showing
* an error.
*/
const showCall = [STATE_JOINING, STATE_JOINED, STATE_ERROR].includes(
appState,
);
/**
* Creates a new call room.
*/
const createCall = useCallback(() => {
setAppState(STATE_CREATING);
return api
.createRoom()
.then((room) => room.url)
.catch((error) => {
console.error('Error creating room', error);
setRoomUrl(null);
setAppState(STATE_IDLE);
setApiError(true);
});
}, []);
/**
* Starts joining an existing call.
*/
const startJoiningCall = useCallback((url) => {
const newCallObject = DailyIframe.createCallObject();
setRoomUrl(url);
setCallObject(newCallObject);
setAppState(STATE_JOINING);
newCallObject.join({ url });
}, []);
/**
* Starts leaving the current call.
*/
const startLeavingCall = useCallback(() => {
if (!callObject) return;
// If we're in the error state, we've already "left", so just clean up
if (appState === STATE_ERROR) {
callObject.destroy().then(() => {
setRoomUrl(null);
setCallObject(null);
setAppState(STATE_IDLE);
});
} else {
/* This will trigger a `left-meeting` event, which in turn will trigger the full clean-up as seen in handleNewMeetingState() below. */
setAppState(STATE_LEAVING);
callObject.leave();
}
}, [callObject, appState]);
/**
* If a room's already specified in the page's URL when the component mounts,
* join the room.
*/
useEffect(() => {
const url = roomUrlFromPageUrl();
url && startJoiningCall(url);
}, [startJoiningCall]);
/**
* Update the page's URL to reflect the active call when roomUrl changes.
*/
useEffect(() => {
const pageUrl = pageUrlFromRoomUrl(roomUrl);
if (pageUrl === window.location.href) return;
window.history.replaceState(null, null, pageUrl);
}, [roomUrl]);
/**
* Update app state based on reported meeting state changes.
*
* NOTE: Here we're showing how to completely clean up a call with destroy().
* This isn't strictly necessary between join()s, but is good practice when
* you know you'll be done with the call object for a while, and you're no
* longer listening to its events.
*/
useEffect(() => {
if (!callObject) return;
const events = ['joined-meeting', 'left-meeting', 'error', 'camera-error'];
function handleNewMeetingState() {
switch (callObject.meetingState()) {
case 'joined-meeting':
setAppState(STATE_JOINED);
break;
case 'left-meeting':
callObject.destroy().then(() => {
setRoomUrl(null);
setCallObject(null);
setAppState(STATE_IDLE);
});
break;
case 'error':
setAppState(STATE_ERROR);
break;
default:
break;
}
}
// Use initial state
handleNewMeetingState();
// Listen for changes in state
for (const event of events) {
/*
We can't use the useDailyEvent hook (https://docs.daily.co/reference/daily-react-hooks/use-daily-event) for this
because right now, we're not inside a <DailyProvider/> (https://docs.daily.co/reference/daily-react-hooks/daily-provider)
context yet. We can't access the call object via daily-react-hooks just yet, but we will later in Call.js.
*/
callObject.on(event, handleNewMeetingState);
}
// Stop listening for changes in state
return function cleanup() {
for (const event of events) {
callObject.off(event, handleNewMeetingState);
}
};
}, [callObject]);
return (
<div className="app">
<Header />
{apiError ? (
<div className="api-error">
<h1>Error</h1>
<p>
Room could not be created. Please check your local configuration in
`api.js`. For more information, check out the{' '}
<a href="https://github.com/daily-demos/call-object-react-daily-hooks/blob/main/README.md">
readme
</a>{' '}
:)
</p>
</div>
) : showCall ? (
<DailyProvider callObject={callObject}>
<Call />
<Tray leaveCall={startLeavingCall} />
</DailyProvider>
) : (
<HomeScreen
createCall={createCall}
startJoiningCall={startJoiningCall}
/>
)}
</div>
);
}