-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
248 lines (224 loc) · 8.53 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import './App.css';
import React, { useEffect, useState, useCallback, useMemo } from 'react';
import DailyIframe from '@daily-co/daily-js';
import { DailyProvider } from '@daily-co/daily-react';
import api from './api';
import { roomUrlFromPageUrl, pageUrlFromRoomUrl } from './utils';
import HomeScreen from './components/HomeScreen/HomeScreen';
import Call from './components/Call/Call';
import Tray from './components/Tray/Tray';
import HairCheck from './components/HairCheck/HairCheck';
import { LanguageContext } from './contexts/Language/LanguageContext';
/* 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. */
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';
const STATE_HAIRCHECK = 'STATE_HAIRCHECK';
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);
const [lang, setLang] = useState({
local: { subtitles: 'english', audio: 'english', spoken: 'english', voice: 'male' },
remote: {},
translators: {},
});
const langs = useMemo(() => [lang, setLang], [lang]);
/**
* Create a new call room. This function will return the newly created room URL.
* We'll need this URL when pre-authorizing (https://docs.daily.co/reference/rn-daily-js/instance-methods/pre-auth)
* or joining (https://docs.daily.co/reference/rn-daily-js/instance-methods/join) a call.
*/
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);
});
}, []);
useEffect(() => {
console.log('whoa, lang just got updated all the way up in app.js: ', lang);
}, [lang]);
/**
* We've created a room, so let's start the hair check. We won't be joining the call yet.
*/
const startHairCheck = useCallback(async (url) => {
const newCallObject = DailyIframe.createCallObject();
setRoomUrl(url);
setCallObject(newCallObject);
setAppState(STATE_HAIRCHECK);
await newCallObject.preAuth({ url }); // add a meeting token here if your room is private
await newCallObject.startCamera();
}, []);
/**
* Once we pass the hair check, we can actually join the call.
*/
const joinCall = useCallback(() => {
callObject.once('joined-meeting', () => {
// Announce my language settings for everyone on the call,
// since daily-python doesn't support session data yet
callObject.sendAppMessage({ msg: 'participant', data: { lang: lang.local } });
callObject.sendAppMessage({ msg: 'request-languages' });
});
callObject.on('app-message', (e) => {
console.log('app message received', e);
if (e.fromId === 'transcription') {
console.log('GOT NATIVE TRANSCRIPTION: ', e.data.text);
} else if (e.data?.msg === 'request-languages') {
callObject.sendAppMessage({ msg: 'participant', data: { lang: lang.local } });
} else if (e.data?.msg === 'participant') {
console.log('got participant lang info', e);
const newRemote = lang.remote;
newRemote[e.fromId] = e.data?.data?.lang;
setLang({ local: lang.local, translators: lang.translators, remote: newRemote });
} else if (e.data?.msg === 'translatorbot') {
const newTranslators = lang.translators;
newTranslators[e.fromId] = e.data?.data;
setLang({ local: lang.local, remote: lang.remote, translators: newTranslators });
}
});
callObject.join({ url: roomUrl });
window.callObject = callObject;
window.call = callObject;
}, [callObject, roomUrl, lang]);
/**
* Start 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();
if (url) {
startHairCheck(url);
}
}, [startHairCheck]);
/**
* 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.
* We can't use the useDailyEvent hook (https://docs.daily.co/reference/daily-react/use-daily-event) for this
* because right now, we're not inside a <DailyProvider/> (https://docs.daily.co/reference/daily-react/daily-provider)
* context yet. We can't access the call object via daily-react just yet, but we will later in Call.js and HairCheck.js!
*/
events.forEach((event) => callObject.on(event, handleNewMeetingState));
// Stop listening for changes in state
return () => {
events.forEach((event) => callObject.off(event, handleNewMeetingState));
};
}, [callObject]);
/**
* Show the call UI if we're either joining, already joined, or have encountered
* an error that is _not_ a room API error.
*/
const showCall = !apiError && [STATE_JOINING, STATE_JOINED, STATE_ERROR].includes(appState);
/* When there's no problems creating the room and startHairCheck() has been successfully called,
* we can show the hair check UI. */
const showHairCheck = !apiError && appState === STATE_HAIRCHECK;
const renderApp = () => {
// If something goes wrong with creating the room.
if (apiError) {
return (
<div className="api-error">
<h1>Error</h1>
<p>
Room could not be created. Check if your `.env` file is set up correctly. For more
information, see the{' '}
<a href="https://github.com/daily-demos/custom-video-daily-react-hooks#readme">
readme
</a>{' '}
:)
</p>
</div>
);
}
// No API errors? Let's check our hair then.
if (showHairCheck) {
return (
<DailyProvider callObject={callObject}>
<HairCheck joinCall={joinCall} cancelCall={startLeavingCall} />
</DailyProvider>
);
}
// No API errors, we passed the hair check, and we've joined the call? Then show the call.
if (showCall) {
return (
<DailyProvider callObject={callObject}>
<Call />
<Tray leaveCall={startLeavingCall} />
</DailyProvider>
);
}
// The default view is the HomeScreen, from where we start the demo.
return <HomeScreen createCall={createCall} startHairCheck={startHairCheck} />;
};
return (
<div className="app">
<LanguageContext.Provider value={langs}>{renderApp()}</LanguageContext.Provider>
</div>
);
}