-
Notifications
You must be signed in to change notification settings - Fork 38
/
component.tsx
631 lines (570 loc) · 23.6 KB
/
component.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import React, {CSSProperties} from 'react';
import moment from 'moment-timezone';
import {compareSemVer} from 'semver-parser';
import {OverlayTrigger, Tooltip} from 'react-bootstrap';
import {UserProfile} from 'mattermost-redux/types/users';
import {Channel} from 'mattermost-redux/types/channels';
import {getUserDisplayName, getScreenStream, isDMChannel} from 'src/utils';
import {UserState} from 'src/types/types';
import Avatar from '../avatar/avatar';
import CompassIcon from '../../components/icons/compassIcon';
import LeaveCallIcon from '../../components/icons/leave_call_icon';
import MutedIcon from '../../components/icons/muted_icon';
import UnmutedIcon from '../../components/icons/unmuted_icon';
import ScreenIcon from '../../components/icons/screen_icon';
import RaisedHandIcon from '../../components/icons/raised_hand';
import UnraisedHandIcon from '../../components/icons/unraised_hand';
import ParticipantsIcon from '../../components/icons/participants';
import './component.scss';
interface Props {
show: boolean,
currentUserID: string,
profiles: UserProfile[],
pictures: {
[key: string]: string,
},
statuses: {
[key: string]: UserState,
},
callStartAt: number,
hideExpandedView: () => void,
showScreenSourceModal: () => void,
screenSharingID: string,
channel: Channel,
connectedDMUser: UserProfile | undefined,
}
interface State {
intervalID?: NodeJS.Timer,
screenStream: MediaStream | null,
showParticipantsList: boolean,
}
export default class ExpandedView extends React.PureComponent<Props, State> {
private screenPlayer = React.createRef<HTMLVideoElement>()
constructor(props: Props) {
super(props);
this.screenPlayer = React.createRef();
this.state = {
screenStream: null,
showParticipantsList: false,
};
if (window.opener) {
const callsClient = window.opener.callsClient;
callsClient.on('close', () => window.close());
window.addEventListener('unload', () => {
if (this.state.screenStream && this.state.screenStream === callsClient.getLocalScreenStream()) {
callsClient.unshareScreen();
}
});
}
}
getCallDuration = () => {
const dur = moment.utc(moment().diff(moment(this.props.callStartAt)));
if (dur.hours() === 0) {
return dur.format('mm:ss');
}
return dur.format('HH:mm:ss');
}
onDisconnectClick = () => {
this.props.hideExpandedView();
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
if (callsClient) {
callsClient.disconnect();
if (window.opener) {
window.close();
}
}
}
onMuteToggle = () => {
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
if (callsClient.isMuted()) {
callsClient.unmute();
} else {
callsClient.mute();
}
}
onShareScreenToggle = async () => {
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
if (this.props.screenSharingID === this.props.currentUserID) {
callsClient.unshareScreen();
this.setState({
screenStream: null,
});
} else if (!this.props.screenSharingID) {
if (window.desktop && compareSemVer(window.desktop.version, '5.1.0') >= 0) {
this.props.showScreenSourceModal();
} else {
const stream = await getScreenStream();
callsClient.setScreenStream(stream);
this.setState({
screenStream: stream,
});
}
}
}
onRaiseHandToggle = () => {
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
if (callsClient.isHandRaised) {
callsClient.unraiseHand();
} else {
callsClient.raiseHand();
}
}
onParticipantsListToggle = () => {
this.setState({
showParticipantsList: !this.state.showParticipantsList,
});
}
public componentDidUpdate(prevProps: Props, prevState: State) {
if (document.title.indexOf('Call') === -1 && this.props.channel) {
console.log('missing title');
if (isDMChannel(this.props.channel) && this.props.connectedDMUser) {
document.title = `Call - ${getUserDisplayName(this.props.connectedDMUser)}`;
} else if (!isDMChannel(this.props.channel)) {
document.title = `Call - ${this.props.channel.display_name}`;
}
}
if (this.state.screenStream && this.screenPlayer.current && this.screenPlayer?.current.srcObject !== this.state.screenStream) {
this.screenPlayer.current.srcObject = this.state.screenStream;
}
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
if (!this.state.screenStream && callsClient?.getLocalScreenStream()) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({screenStream: callsClient.getLocalScreenStream()});
}
}
public componentDidMount() {
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
callsClient.on('remoteScreenStream', (stream: MediaStream) => {
this.setState({
screenStream: stream,
});
});
const screenStream = callsClient.getLocalScreenStream() || callsClient.getRemoteScreenStream();
// This is needed to force a re-render to periodically update
// the start time.
const id = setInterval(() => this.forceUpdate(), 1000);
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({
intervalID: id,
screenStream,
});
}
public componentWillUnmount() {
if (this.state.intervalID) {
clearInterval(this.state.intervalID);
}
}
renderScreenSharingPlayer = () => {
const isSharing = this.props.screenSharingID === this.props.currentUserID;
let profile;
if (!isSharing) {
for (let i = 0; i < this.props.profiles.length; i++) {
if (this.props.profiles[i].id === this.props.screenSharingID) {
profile = this.props.profiles[i];
break;
}
}
if (!profile) {
return null;
}
}
const msg = isSharing ? 'You are sharing your screen' : `You are viewing ${getUserDisplayName(profile as UserProfile)}'s screen`;
return (
<div style={style.screenContainer as CSSProperties}>
<video
id='screen-player'
ref={this.screenPlayer}
width='100%'
height='100%'
muted={true}
autoPlay={true}
controls={true}
/>
<span
style={{
background: 'black',
padding: '4px 8px',
borderRadius: '4px',
color: 'white',
marginTop: '8px',
}}
>
{msg}
</span>
</div>
);
}
renderParticipants = () => {
return this.props.profiles.map((profile, idx) => {
const status = this.props.statuses[profile.id];
let isMuted = true;
let isSpeaking = false;
let isHandRaised = false;
if (status) {
isMuted = !status.unmuted;
isSpeaking = Boolean(status.voice);
isHandRaised = Boolean(status.raised_hand > 0);
}
const MuteIcon = isMuted ? MutedIcon : UnmutedIcon;
return (
<li
key={'participants_profile_' + idx}
style={{display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', margin: '16px'}}
>
<div style={{position: 'relative'}}>
<Avatar
size={50}
fontSize={18}
border={false}
url={this.props.pictures[profile.id]}
style={{
boxShadow: isSpeaking ? '0px 0px 4px 4px rgba(61, 184, 135, 0.8)' : '',
}}
/>
<div
style={{
position: 'absolute',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
bottom: 0,
right: 0,
background: 'rgba(50, 50, 50, 1)',
borderRadius: '30px',
width: '20px',
height: '20px',
}}
>
<MuteIcon
fill={isMuted ? '#C4C4C4' : '#3DB887'}
style={{width: '14px', height: '14px'}}
stroke={isMuted ? '#C4C4C4' : ''}
/>
</div>
<div
style={{
position: 'absolute',
display: isHandRaised ? 'flex' : 'none',
justifyContent: 'center',
alignItems: 'center',
top: 0,
right: 0,
background: 'rgba(50, 50, 50, 1)',
borderRadius: '30px',
width: '20px',
height: '20px',
fontSize: '12px',
}}
>
{'✋'}
</div>
</div>
<span style={{fontWeight: 600, fontSize: '12px', margin: '8px 0'}}>
{getUserDisplayName(profile)}{profile.id === this.props.currentUserID && ' (you)'}
</span>
</li>
);
});
}
renderParticipantsRHSList = () => {
return this.props.profiles.map((profile, idx) => {
const status = this.props.statuses[profile.id];
let isMuted = true;
let isSpeaking = false;
let isHandRaised = false;
if (status) {
isMuted = !status.unmuted;
isSpeaking = Boolean(status.voice);
isHandRaised = Boolean(status.raised_hand > 0);
}
const MuteIcon = isMuted ? MutedIcon : UnmutedIcon;
return (
<li
key={'participants_rhs_profile_' + idx}
style={{display: 'flex', alignItems: 'center', padding: '4px 8px'}}
>
<Avatar
size={24}
fontSize={10}
url={this.props.pictures[profile.id]}
style={{
boxShadow: isSpeaking ? '0px 0px 4px 4px rgba(61, 184, 135, 0.8)' : '',
marginRight: '8px',
}}
/>
<span style={{fontWeight: 600, fontSize: '12px', margin: '8px 0'}}>
{getUserDisplayName(profile)}{profile.id === this.props.currentUserID && ' (you)'}
</span>
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
marginLeft: 'auto',
gap: '4px',
}}
>
{ isHandRaised &&
<RaisedHandIcon
fill={'rgba(255, 188, 66, 1)'}
style={{width: '14px', height: '14px'}}
/>
}
{ this.props.screenSharingID === profile.id &&
<ScreenIcon
fill={'rgba(210, 75, 78, 1)'}
style={{width: '14px', height: '14px'}}
/>
}
<MuteIcon
fill={isMuted ? '#C4C4C4' : '#3DB887'}
style={{width: '14px', height: '14px'}}
stroke={isMuted ? '#C4C4C4' : ''}
/>
</div>
</li>
);
});
}
render() {
if ((!this.props.show || !window.callsClient) && !window.opener) {
return null;
}
const callsClient = window.opener ? window.opener.callsClient : window.callsClient;
if (!callsClient) {
return null;
}
const isMuted = callsClient.isMuted();
const MuteIcon = isMuted ? MutedIcon : UnmutedIcon;
const muteButtonText = isMuted ? 'Unmute' : 'Mute';
const isHandRaised = callsClient.isHandRaised;
const HandIcon = isHandRaised ? UnraisedHandIcon : RaisedHandIcon;
const raiseHandText = isHandRaised ? 'Lower hand' : 'Raise hand';
const participantsText = 'Show participants list';
const sharingID = this.props.screenSharingID;
const currentID = this.props.currentUserID;
const isSharing = sharingID === currentID;
return (
<div
id='calls-expanded-view'
style={style.root as CSSProperties}
>
<div style={style.main as CSSProperties}>
<div style={{display: 'flex', alignItems: 'center', width: '100%'}}>
<div style={style.topLeftContainer as CSSProperties}>
<span style={{margin: '4px', fontWeight: 600}}>{this.getCallDuration()}</span>
<span style={{margin: '4px'}}>{'•'}</span>
<span style={{margin: '4px'}}>{`${this.props.profiles.length} participants`}</span>
</div>
{
!window.opener &&
<button
className='button-close'
style={style.closeViewButton as CSSProperties}
onClick={this.props.hideExpandedView}
>
<CompassIcon icon='arrow-collapse'/>
</button>
}
</div>
{ !this.props.screenSharingID &&
<ul
id='calls-expanded-view-participants-grid'
style={{
...style.participants,
gridTemplateColumns: `repeat(${Math.min(this.props.profiles.length, 4)}, 1fr)`,
}}
>
{ this.renderParticipants() }
</ul>
}
{ this.props.screenSharingID && this.renderScreenSharingPlayer() }
<div
id='calls-expanded-view-controls'
style={style.controls}
>
<div style={style.leftControls}>
<OverlayTrigger
key='show_participants_list'
placement='top'
overlay={
<Tooltip
id='show-participants-list'
>
{this.state.showParticipantsList ? 'Hide participants list' : 'Show participants list'}
</Tooltip>
}
>
<button
className='button-center-controls'
onClick={this.onParticipantsListToggle}
style={{background: this.state.showParticipantsList ? 'rgba(28, 88, 217, 0.32)' : ''}}
>
<ParticipantsIcon
style={{width: '24px', height: '24px'}}
fill={this.state.showParticipantsList ? 'rgb(28, 88, 217)' : 'white'}
/>
</button>
</OverlayTrigger>
</div>
<div style={style.centerControls}>
<div style={style.buttonContainer as CSSProperties}>
<button
className='button-center-controls'
onClick={this.onRaiseHandToggle}
style={{background: isHandRaised ? 'rgba(255, 188, 66, 0.16)' : ''}}
>
<HandIcon
style={{width: '28px', height: '28px'}}
fill={isHandRaised ? 'rgba(255, 188, 66, 1)' : 'white'}
/>
</button>
<span
style={{fontSize: '14px', fontWeight: 600, marginTop: '12px'}}
>{raiseHandText}</span>
</div>
{ (isSharing || !sharingID) &&
<div style={style.buttonContainer as CSSProperties}>
<button
className='button-center-controls'
onClick={this.onShareScreenToggle}
style={{background: isSharing ? 'rgba(210, 75, 78, 0.12)' : ''}}
>
<ScreenIcon
style={{width: '28px', height: '28px'}}
fill={isSharing ? 'rgba(210, 75, 78, 1)' : 'white'}
/>
</button>
<span
style={{fontSize: '14px', fontWeight: 600, marginTop: '12px'}}
>{isSharing ? 'Stop presenting' : 'Start presenting'}</span>
</div>
}
<div
id='calls-popout-mute-button'
style={style.buttonContainer as CSSProperties}
>
<button
className='button-center-controls'
onClick={this.onMuteToggle}
style={{background: isMuted ? '' : 'rgba(61, 184, 135, 0.16)'}}
>
<MuteIcon
style={{width: '28px', height: '28px'}}
fill={isMuted ? 'white' : 'rgba(61, 184, 135, 1)'}
stroke={isMuted ? 'rgba(210, 75, 78, 1)' : ''}
/>
</button>
<span
style={{fontSize: '14px', fontWeight: 600, marginTop: '12px'}}
>{muteButtonText}</span>
</div>
</div>
{ !window.opener &&
<div style={{flex: '1', display: 'flex', justifyContent: 'flex-end', marginRight: '16px'}}>
<button
className='button-leave'
onClick={this.onDisconnectClick}
>
<LeaveCallIcon
style={{width: '24px', height: '24px'}}
fill='white'
/>
<span
style={{fontSize: '18px', fontWeight: 600, marginLeft: '8px'}}
>{'Leave'}</span>
</button>
</div>
}
{ window.opener && <div style={{flex: '1'}}/>}
</div>
</div>
{ this.state.showParticipantsList &&
<ul style={style.rhs as CSSProperties}>
<span style={{position: 'sticky', top: '0', background: 'inherit', fontWeight: 600, padding: '8px'}}>{'Participants list'}</span>
{ this.renderParticipantsRHSList() }
</ul>
}
</div>
);
}
}
const style = {
root: {
position: 'absolute',
display: 'flex',
top: 0,
left: 0,
width: '100%',
height: '100%',
zIndex: 100,
background: 'rgba(37, 38, 42, 1)',
color: 'white',
},
main: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
flex: '1',
},
closeViewButton: {
fontSize: '24px',
marginLeft: 'auto',
},
participants: {
display: 'grid',
overflow: 'auto',
margin: 'auto',
padding: '0',
},
controls: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
padding: '8px',
width: '100%',
},
leftControls: {
flex: '1',
marginLeft: '16px',
},
centerControls: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
margin: '0 8px',
width: '112px',
},
topLeftContainer: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: '16px',
marginRight: 'auto',
padding: '4px',
},
screenContainer: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
margin: 'auto',
maxWidth: 'calc(100% - 16px)',
maxHeight: 'calc(100% - 200px)',
},
rhs: {
display: 'flex',
flexDirection: 'column',
width: '300px',
background: 'rgba(9, 10, 11, 1)',
margin: 0,
padding: 0,
overflow: 'auto',
},
};