-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathControlBar.tsx
More file actions
235 lines (218 loc) · 7.71 KB
/
Copy pathControlBar.tsx
File metadata and controls
235 lines (218 loc) · 7.71 KB
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
import { Track } from 'livekit-client';
import * as React from 'react';
import { MediaDeviceMenu } from './MediaDeviceMenu';
import { DisconnectButton } from '../components/controls/DisconnectButton';
import { TrackToggle } from '../components/controls/TrackToggle';
import { ChatIcon, GearIcon, LeaveIcon } from '../assets/icons';
import { ChatToggle } from '../components/controls/ChatToggle';
import { EmojiReactionButton } from '../components/controls/EmojiReactionButton';
import { useLocalParticipantPermissions, usePersistentUserChoices } from '../hooks';
import { useMediaQuery } from '../hooks/internal';
import { useMaybeLayoutContext } from '../context';
import { supportsScreenSharing } from '@livekit/components-core';
import { mergeProps } from '../utils';
import { StartMediaButton } from '../components/controls/StartMediaButton';
import { SettingsMenuToggle } from '../components/controls/SettingsMenuToggle';
/** @public */
export type ControlBarControls = {
microphone?: boolean;
camera?: boolean;
chat?: boolean;
screenShare?: boolean;
leave?: boolean;
settings?: boolean;
reactions?: boolean;
};
const trackSourceToProtocol = (source: Track.Source) => {
// NOTE: this mapping avoids importing the protocol package as that leads to a significant bundle size increase
switch (source) {
case Track.Source.Camera:
return 1;
case Track.Source.Microphone:
return 2;
case Track.Source.ScreenShare:
return 3;
default:
return 0;
}
};
/** @public */
export interface ControlBarProps extends React.HTMLAttributes<HTMLDivElement> {
onDeviceError?: (error: { source: Track.Source; error: Error }) => void;
variation?: 'minimal' | 'verbose' | 'textOnly';
controls?: ControlBarControls;
/**
* If `true`, the user's device choices will be persisted.
* This will enable the user to have the same device choices when they rejoin the room.
* @defaultValue true
* @alpha
*/
saveUserChoices?: boolean;
}
/**
* The `ControlBar` prefab gives the user the basic user interface to control their
* media devices (camera, microphone and screen share), open the `Chat` and leave the room.
*
* @remarks
* This component is build with other LiveKit components like `TrackToggle`,
* `DeviceSelectorButton`, `DisconnectButton` and `StartAudio`.
*
* @example
* ```tsx
* <LiveKitRoom>
* <ControlBar />
* </LiveKitRoom>
* ```
* @public
*/
export function ControlBar({
variation,
controls,
saveUserChoices = true,
onDeviceError,
...props
}: ControlBarProps) {
const [isChatOpen, setIsChatOpen] = React.useState(false);
const layoutContext = useMaybeLayoutContext();
React.useEffect(() => {
if (layoutContext?.widget.state?.showChat !== undefined) {
setIsChatOpen(layoutContext?.widget.state?.showChat);
}
}, [layoutContext?.widget.state?.showChat]);
const isTooLittleSpace = useMediaQuery(`(max-width: ${isChatOpen ? 1000 : 760}px)`);
const defaultVariation = isTooLittleSpace ? 'minimal' : 'verbose';
variation ??= defaultVariation;
const visibleControls = { leave: true, ...controls };
const localPermissions = useLocalParticipantPermissions();
if (!localPermissions) {
visibleControls.camera = false;
visibleControls.chat = false;
visibleControls.microphone = false;
visibleControls.screenShare = false;
} else {
const canPublishSource = (source: Track.Source) => {
return (
localPermissions.canPublish &&
(localPermissions.canPublishSources.length === 0 ||
localPermissions.canPublishSources.includes(trackSourceToProtocol(source)))
);
};
visibleControls.camera ??= canPublishSource(Track.Source.Camera);
visibleControls.microphone ??= canPublishSource(Track.Source.Microphone);
visibleControls.screenShare ??= canPublishSource(Track.Source.ScreenShare);
visibleControls.chat ??= localPermissions.canPublishData && controls?.chat;
visibleControls.reactions ??= localPermissions.canPublishData && controls?.reactions;
}
const showIcon = React.useMemo(
() => variation === 'minimal' || variation === 'verbose',
[variation],
);
const showText = React.useMemo(
() => variation === 'textOnly' || variation === 'verbose',
[variation],
);
const browserSupportsScreenSharing = supportsScreenSharing();
const [isScreenShareEnabled, setIsScreenShareEnabled] = React.useState(false);
const onScreenShareChange = React.useCallback(
(enabled: boolean) => {
setIsScreenShareEnabled(enabled);
},
[setIsScreenShareEnabled],
);
const htmlProps = mergeProps({ className: 'lk-control-bar' }, props);
const {
saveAudioInputEnabled,
saveVideoInputEnabled,
saveAudioInputDeviceId,
saveVideoInputDeviceId,
} = usePersistentUserChoices({ preventSave: !saveUserChoices });
const microphoneOnChange = React.useCallback(
(enabled: boolean, isUserInitiated: boolean) =>
isUserInitiated ? saveAudioInputEnabled(enabled) : null,
[saveAudioInputEnabled],
);
const cameraOnChange = React.useCallback(
(enabled: boolean, isUserInitiated: boolean) =>
isUserInitiated ? saveVideoInputEnabled(enabled) : null,
[saveVideoInputEnabled],
);
return (
<div {...htmlProps}>
{visibleControls.microphone && (
<div className="lk-button-group">
<TrackToggle
source={Track.Source.Microphone}
showIcon={showIcon}
onChange={microphoneOnChange}
onDeviceError={(error) => onDeviceError?.({ source: Track.Source.Microphone, error })}
>
{showText && 'Microphone'}
</TrackToggle>
<div className="lk-button-group-menu">
<MediaDeviceMenu
kind="audioinput"
onActiveDeviceChange={(_kind, deviceId) =>
saveAudioInputDeviceId(deviceId ?? 'default')
}
/>
</div>
</div>
)}
{visibleControls.camera && (
<div className="lk-button-group">
<TrackToggle
source={Track.Source.Camera}
showIcon={showIcon}
onChange={cameraOnChange}
onDeviceError={(error) => onDeviceError?.({ source: Track.Source.Camera, error })}
>
{showText && 'Camera'}
</TrackToggle>
<div className="lk-button-group-menu">
<MediaDeviceMenu
kind="videoinput"
onActiveDeviceChange={(_kind, deviceId) =>
saveVideoInputDeviceId(deviceId ?? 'default')
}
/>
</div>
</div>
)}
{visibleControls.screenShare && browserSupportsScreenSharing && (
<TrackToggle
source={Track.Source.ScreenShare}
captureOptions={{ audio: true, selfBrowserSurface: 'include' }}
showIcon={showIcon}
onChange={onScreenShareChange}
onDeviceError={(error) => onDeviceError?.({ source: Track.Source.ScreenShare, error })}
>
{showText && (isScreenShareEnabled ? 'Stop screen share' : 'Share screen')}
</TrackToggle>
)}
{visibleControls.chat && (
<ChatToggle>
{showIcon && <ChatIcon />}
{showText && 'Chat'}
</ChatToggle>
)}
{visibleControls.reactions && (
<EmojiReactionButton showIcon={showIcon} showText={showText}>
{showText && 'Reactions'}
</EmojiReactionButton>
)}
{visibleControls.settings && (
<SettingsMenuToggle>
{showIcon && <GearIcon />}
{showText && 'Settings'}
</SettingsMenuToggle>
)}
{visibleControls.leave && (
<DisconnectButton>
{showIcon && <LeaveIcon />}
{showText && 'Leave'}
</DisconnectButton>
)}
<StartMediaButton />
</div>
);
}