-
Notifications
You must be signed in to change notification settings - Fork 0
/
Room.tsx
82 lines (77 loc) · 2.74 KB
/
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
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
import React from 'react';
import { styled } from '@material-ui/core/styles';
import { LocalDataTrack } from 'twilio-video';
import useVideoContext from '../../hooks/useVideoContext/useVideoContext';
import useParticipants from '../../hooks/useParticipants/useParticipants';
import useParticipantPositions from '../../hooks/useParticipantPositions/useParticipantPositions';
import Participant from '../Participant/Participant';
// Change this into a grid. Video size should be constrained by container, container should not flex to fit video.
const Container = styled('div')({
display: 'flex',
flexDirection: 'column',
height: '100%',
width: '100%',
});
const Row = styled('div')({
height: '100%',
width: '100%',
display: 'flex',
});
const Cell = styled('div')({
height: '100%',
width: '100%',
border: '1px solid',
});
export default function Room() {
const {
localTracks,
room: { localParticipant },
} = useVideoContext();
const participants = useParticipants();
const { participantPositions, setParticipantPositions } = useParticipantPositions();
const participantsInGrid = new Array(5).fill(null).map(() => new Array(5).fill(null));
[...participants, localParticipant].forEach(participant => {
const position = participantPositions[participant.sid];
if (position) {
participantsInGrid[position.x][position.y] = participant;
}
});
const localPosition = participantPositions[localParticipant.sid];
return (
<Container>
{participantsInGrid.map((participantRow, x) => (
<Row key={x}>
{participantRow.map((participant, y) => {
const position = `${x},${y}`;
let volume = 0.5;
if (localPosition && (x !== localPosition.x || y !== localPosition.y)) {
volume = 1 / (Math.pow(x - localPosition.x, 2) + Math.pow(y - localPosition.y, 2));
}
return (
<Cell
key={participant ? participant.sid : y}
onClick={() => {
if (!participant) {
const track = localTracks.find(_track => _track.kind === 'data') as LocalDataTrack;
track.send(position);
setParticipantPositions(prevParticipantPosition => ({
...prevParticipantPosition,
[localParticipant.sid]: {
x,
y,
},
}));
}
}}
>
{participant && (
<Participant participant={participant} isSelected={false} onClick={() => {}} volume={volume} />
)}
</Cell>
);
})}
</Row>
))}
</Container>
);
}