Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MicrophoneButton.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* eslint-disable lines-around-comment */
  2. import React, { useCallback, useState } from 'react';
  3. import { View, TouchableOpacity } from 'react-native';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. import {
  6. createShortcutEvent,
  7. sendAnalytics,
  8. ACTION_SHORTCUT_PRESSED as PRESSED,
  9. ACTION_SHORTCUT_RELEASED as RELEASED
  10. // @ts-ignore
  11. } from '../../../../analytics';
  12. // @ts-ignore
  13. import { getFeatureFlag, AUDIO_MUTE_BUTTON_ENABLED } from '../../../../base/flags';
  14. // @ts-ignore
  15. import { Icon, IconMicrophone, IconMicrophoneEmptySlash } from '../../../../base/icons';
  16. // @ts-ignore
  17. import { MEDIA_TYPE } from '../../../../base/media';
  18. // @ts-ignore
  19. import { isLocalTrackMuted } from '../../../../base/tracks';
  20. // @ts-ignore
  21. import { isAudioMuteButtonDisabled } from '../../../../toolbox/functions.any';
  22. // @ts-ignore
  23. import { muteLocal } from '../../../../video-menu/actions';
  24. // @ts-ignore
  25. import styles from './styles';
  26. const LONG_PRESS = 'long.press';
  27. /**
  28. * Implements a round audio mute/unmute button of a custom size.
  29. *
  30. * @returns {JSX.Element} - The audio mute round button.
  31. */
  32. const MicrophoneButton = () : JSX.Element|null => {
  33. const dispatch = useDispatch();
  34. const audioMuted = useSelector((state: any) => isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO));
  35. const disabled = useSelector(isAudioMuteButtonDisabled);
  36. const enabledFlag = useSelector(state => getFeatureFlag(state, AUDIO_MUTE_BUTTON_ENABLED, true));
  37. const [ longPress, setLongPress ] = useState(false);
  38. if (!enabledFlag) {
  39. return null;
  40. }
  41. const onPressIn = useCallback(() => {
  42. !disabled && dispatch(muteLocal(!audioMuted, MEDIA_TYPE.AUDIO));
  43. }, [ audioMuted, disabled ]);
  44. const onLongPress = useCallback(() => {
  45. if (!disabled && !audioMuted) {
  46. sendAnalytics(createShortcutEvent(
  47. 'push.to.talk',
  48. PRESSED,
  49. {},
  50. LONG_PRESS));
  51. setLongPress(true);
  52. }
  53. }, [ audioMuted, disabled, setLongPress ]);
  54. const onPressOut = useCallback(() => {
  55. if (longPress) {
  56. setLongPress(false);
  57. sendAnalytics(createShortcutEvent(
  58. 'push.to.talk',
  59. RELEASED,
  60. {},
  61. LONG_PRESS
  62. ));
  63. dispatch(muteLocal(true, MEDIA_TYPE.AUDIO));
  64. }
  65. }, [ longPress, setLongPress ]);
  66. return (
  67. <TouchableOpacity
  68. onLongPress = { onLongPress }
  69. onPressIn = { onPressIn }
  70. onPressOut = { onPressOut } >
  71. <View
  72. style = { [
  73. styles.microphoneStyles.container,
  74. !audioMuted && styles.microphoneStyles.unmuted
  75. ] }>
  76. <View
  77. style = { styles.microphoneStyles.iconContainer }>
  78. <Icon
  79. src = { audioMuted ? IconMicrophoneEmptySlash : IconMicrophone }
  80. style = { styles.microphoneStyles.icon } />
  81. </View>
  82. </View>
  83. </TouchableOpacity>
  84. );
  85. };
  86. export default MicrophoneButton;