@@ -21,99 +21,34 @@ import AccessibleButton from "../../../components/views/elements/AccessibleButto
21
21
import { VoiceBroadcastPreRecording } from "../../models/VoiceBroadcastPreRecording" ;
22
22
import { Icon as LiveIcon } from "../../../../res/img/element-icons/live.svg" ;
23
23
import { _t } from "../../../languageHandler" ;
24
- import IconizedContextMenu , {
25
- IconizedContextMenuOptionList ,
26
- IconizedContextMenuRadio ,
27
- } from "../../../components/views/context_menus/IconizedContextMenu" ;
28
- import { requestMediaPermissions } from "../../../utils/media/requestMediaPermissions" ;
29
- import MediaDeviceHandler from "../../../MediaDeviceHandler" ;
30
- import { toLeftOrRightOf } from "../../../components/structures/ContextMenu" ;
24
+ import { useAudioDeviceSelection } from "../../../hooks/useAudioDeviceSelection" ;
25
+ import { DevicesContextMenu } from "../../../components/views/audio_messages/DevicesContextMenu" ;
31
26
32
27
interface Props {
33
28
voiceBroadcastPreRecording : VoiceBroadcastPreRecording ;
34
29
}
35
30
36
- interface State {
37
- devices : MediaDeviceInfo [ ] ;
38
- device : MediaDeviceInfo | null ;
39
- showDeviceSelect : boolean ;
40
- }
41
-
42
31
export const VoiceBroadcastPreRecordingPip : React . FC < Props > = ( {
43
32
voiceBroadcastPreRecording,
44
33
} ) => {
45
- const shouldRequestPermissionsRef = useRef < boolean > ( true ) ;
46
- const pipRef = useRef < HTMLDivElement > ( null ) ;
47
- const [ state , setState ] = useState < State > ( {
48
- devices : [ ] ,
49
- device : null ,
50
- showDeviceSelect : false ,
51
- } ) ;
52
-
53
- if ( shouldRequestPermissionsRef . current ) {
54
- shouldRequestPermissionsRef . current = false ;
55
- requestMediaPermissions ( false ) . then ( ( stream : MediaStream | undefined ) => {
56
- MediaDeviceHandler . getDevices ( ) . then ( ( { audioinput } ) => {
57
- MediaDeviceHandler . getDefaultDevice ( audioinput ) ;
58
- const deviceFromSettings = MediaDeviceHandler . getAudioInput ( ) ;
59
- const device = audioinput . find ( ( d ) => {
60
- return d . deviceId === deviceFromSettings ;
61
- } ) || audioinput [ 0 ] ;
62
- setState ( {
63
- ...state ,
64
- devices : audioinput ,
65
- device,
66
- } ) ;
67
- stream ?. getTracks ( ) . forEach ( t => t . stop ( ) ) ;
68
- } ) ;
69
- } ) ;
70
- }
71
-
72
- const onDeviceOptionClick = ( device : MediaDeviceInfo ) => {
73
- setState ( {
74
- ...state ,
75
- device,
76
- showDeviceSelect : false ,
77
- } ) ;
78
- } ;
34
+ const pipRef = useRef < HTMLDivElement | null > ( null ) ;
35
+ const { currentDevice, currentDeviceLabel, devices, setDevice } = useAudioDeviceSelection ( ) ;
36
+ const [ showDeviceSelect , setShowDeviceSelect ] = useState < boolean > ( false ) ;
79
37
80
- const onMicrophoneLineClick = ( ) => {
81
- setState ( {
82
- ...state ,
83
- showDeviceSelect : true ,
84
- } ) ;
38
+ const onDeviceSelect = ( device : MediaDeviceInfo | null ) => {
39
+ setShowDeviceSelect ( false ) ;
40
+ setDevice ( device ) ;
85
41
} ;
86
42
87
- const deviceOptions = state . devices . map ( ( d : MediaDeviceInfo ) => {
88
- return < IconizedContextMenuRadio
89
- key = { d . deviceId }
90
- active = { d . deviceId === state . device ?. deviceId }
91
- onClick = { ( ) => onDeviceOptionClick ( d ) }
92
- label = { d . label }
93
- /> ;
94
- } ) ;
95
-
96
- const devicesMenu = state . showDeviceSelect && pipRef . current
97
- ? < IconizedContextMenu
98
- mountAsChild = { false }
99
- onFinished = { ( ) => { } }
100
- { ...toLeftOrRightOf ( pipRef . current . getBoundingClientRect ( ) , 0 ) }
101
- >
102
- < IconizedContextMenuOptionList >
103
- { deviceOptions }
104
- </ IconizedContextMenuOptionList >
105
- </ IconizedContextMenu >
106
- : null ;
107
-
108
43
return < div
109
44
className = "mx_VoiceBroadcastBody mx_VoiceBroadcastBody--pip"
110
45
ref = { pipRef }
111
46
>
112
47
< VoiceBroadcastHeader
113
48
onCloseClick = { voiceBroadcastPreRecording . cancel }
114
- onMicrophoneLineClick = { onMicrophoneLineClick }
49
+ onMicrophoneLineClick = { ( ) => setShowDeviceSelect ( true ) }
115
50
room = { voiceBroadcastPreRecording . room }
116
- microphoneLabel = { state . device ?. label || _t ( 'Default Device' ) }
51
+ microphoneLabel = { currentDeviceLabel }
117
52
showClose = { true }
118
53
/>
119
54
< AccessibleButton
@@ -124,6 +59,13 @@ export const VoiceBroadcastPreRecordingPip: React.FC<Props> = ({
124
59
< LiveIcon className = "mx_Icon mx_Icon_16" />
125
60
{ _t ( "Go live" ) }
126
61
</ AccessibleButton >
127
- { devicesMenu }
62
+ {
63
+ showDeviceSelect && < DevicesContextMenu
64
+ containerRef = { pipRef }
65
+ currentDevice = { currentDevice }
66
+ devices = { devices }
67
+ onDeviceSelect = { onDeviceSelect }
68
+ />
69
+ }
128
70
</ div > ;
129
71
} ;
0 commit comments