-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathDevicePicker.tsx
More file actions
157 lines (134 loc) · 4.53 KB
/
DevicePicker.tsx
File metadata and controls
157 lines (134 loc) · 4.53 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
import { Box, RadioButton } from '@rocket.chat/fuselage';
import { useSafely } from '@rocket.chat/fuselage-hooks';
import { GenericMenu } from '@rocket.chat/ui-client';
import type { GenericMenuItemProps } from '@rocket.chat/ui-client';
import { useAvailableDevices, useSelectedDevices } from '@rocket.chat/ui-contexts';
import type { ComponentProps, MouseEvent } from 'react';
import { forwardRef, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ActionButton } from '.';
import { useMediaCallViewContext } from '../context/MediaCallViewContext';
import { useDevicePermissionPrompt2, stopTracks } from '../hooks/useDevicePermissionPrompt';
type DevicePickerButtonProps = {
secondary?: boolean;
small?: boolean;
} & Omit<ComponentProps<typeof ActionButton>, 'label' | 'icon'>;
// GenericMenu for some reason passes `small: true` when the button is disabled (??).
// so this is just a wrapper to stop that from happening.
const DevicePickerButton = forwardRef<HTMLButtonElement, DevicePickerButtonProps>(function DevicePickerButton(
{ secondary = false, small: _small, ...props },
ref,
) {
return <ActionButton secondary={secondary} {...props} label='customize' icon='customize' ref={ref} />;
});
const getDefaultDeviceItem = (label: string, type: 'input' | 'output') => ({
content: (
<Box is='span' title={label} fontSize={14}>
{label}
</Box>
),
addon: <RadioButton onChange={() => undefined} checked={true} disabled />,
id: `default-${type}`,
});
// eslint-disable-next-line react/no-multi-comp
const DevicePicker = ({ secondary = false }: { secondary?: boolean }) => {
const { t } = useTranslation();
const { onDeviceChange } = useMediaCallViewContext();
const availableDevices = useAvailableDevices();
const selectedAudioDevices = useSelectedDevices();
const availableInputDevice =
availableDevices?.audioInput?.map<GenericMenuItemProps>((device) => {
if (!device.id || !device.label) {
return getDefaultDeviceItem(t('Default'), 'input');
}
return {
id: `${device.id}-input`,
content: (
<Box is='span' title={device.label} fontSize={14}>
{device.label}
</Box>
),
addon: <RadioButton checked={device.id === selectedAudioDevices?.audioInput?.id} />,
};
}) || [];
const availableOutputDevice =
availableDevices?.audioOutput?.map<GenericMenuItemProps>((device) => {
if (!device.id || !device.label) {
return getDefaultDeviceItem(t('Default'), 'output');
}
return {
id: `${device.id}-output`,
content: (
<Box is='span' title={device.label} fontSize={14}>
{device.label}
</Box>
),
addon: <RadioButton checked={device.id === selectedAudioDevices?.audioOutput?.id} />,
onClick(e?: MouseEvent<HTMLElement>) {
e?.preventDefault();
e?.stopPropagation();
},
};
}) || [];
const micSection = {
title: t('Microphone'),
items: availableInputDevice,
};
const speakerSection = {
title: t('Speaker'),
items: availableOutputDevice,
};
const disabled = availableOutputDevice.length === 0 && availableInputDevice.length === 0;
const [isOpen, setIsOpen] = useSafely(useState(false));
const requestPermission = useDevicePermissionPrompt2();
const onOpenChange = useCallback(
(isOpen: boolean) => {
if (!isOpen) {
setIsOpen(false);
return;
}
void requestPermission({
actionType: 'device-change',
}).then((stream) => {
stopTracks(stream);
setIsOpen(true);
});
},
[requestPermission, setIsOpen],
);
return (
<GenericMenu
title={disabled ? t('Device_settings_not_supported_by_browser') : t('Device_settings_lowercase')}
sections={[micSection, speakerSection]}
disabled={disabled}
placement='top-end'
selectionMode='multiple'
isOpen={isOpen}
onOpenChange={onOpenChange}
onAction={(deviceId) => {
if (typeof deviceId !== 'string') {
return;
}
if (deviceId.includes('-input')) {
const id = deviceId.replace('-input', '');
const device = availableDevices?.audioInput?.find((device) => device.id === id);
if (device) {
onDeviceChange(device);
}
return;
}
if (deviceId.includes('-output')) {
const id = deviceId.replace('-output', '');
const device = availableDevices?.audioOutput?.find((device) => device.id === id);
if (device) {
onDeviceChange(device);
}
return;
}
console.warn('Device Picker - Failed to select device: Invalid deviceId', deviceId);
}}
button={<DevicePickerButton secondary={secondary} tiny={!secondary} />}
/>
);
};
export default DevicePicker;