You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpeakerEntry.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import clsx from 'clsx';
  2. import React, { useRef } from 'react';
  3. import { IconCheck } from '../../../../base/icons/svg';
  4. import Button from '../../../../base/ui/components/web/Button';
  5. import ContextMenuItem from '../../../../base/ui/components/web/ContextMenuItem';
  6. import { BUTTON_TYPES } from '../../../../base/ui/constants.any';
  7. import logger from '../../../logger';
  8. const TEST_SOUND_PATH = 'sounds/ring.mp3';
  9. /**
  10. * The type of the React {@code Component} props of {@link SpeakerEntry}.
  11. */
  12. interface IProps {
  13. /**
  14. * The text label for the entry.
  15. */
  16. children: string;
  17. /**
  18. * The deviceId of the speaker.
  19. */
  20. deviceId: string;
  21. /**
  22. * Flag controlling the selection state of the entry.
  23. */
  24. index: number;
  25. /**
  26. * Flag controlling the selection state of the entry.
  27. */
  28. isSelected: boolean;
  29. /**
  30. * Flag controlling the selection state of the entry.
  31. */
  32. length: number;
  33. listHeaderId: string;
  34. /**
  35. * Click handler for the component.
  36. */
  37. onClick: Function;
  38. }
  39. /**
  40. * Implements a React {@link Component} which displays an audio
  41. * output settings entry. The user can click and play a test sound.
  42. *
  43. * @param {IProps} props - Component props.
  44. * @returns {JSX.Element}
  45. */
  46. const SpeakerEntry = (props: IProps) => {
  47. const audioRef = useRef<HTMLAudioElement | null>(null);
  48. /**
  49. * Click handler for the entry.
  50. *
  51. * @returns {void}
  52. */
  53. function _onClick() {
  54. props.onClick(props.deviceId);
  55. }
  56. /**
  57. * Key pressed handler for the entry.
  58. *
  59. * @param {Object} e - The event.
  60. * @private
  61. *
  62. * @returns {void}
  63. */
  64. function _onKeyPress(e: React.KeyboardEvent) {
  65. if (e.key === ' ') {
  66. e.preventDefault();
  67. props.onClick(props.deviceId);
  68. }
  69. }
  70. /**
  71. * Click handler for Test button.
  72. * Sets the current audio output id and plays a sound.
  73. *
  74. * @param {Object} e - The synthetic event.
  75. * @returns {void}
  76. */
  77. async function _onTestButtonClick(e: React.KeyboardEvent | React.MouseEvent) {
  78. e.stopPropagation();
  79. try { // @ts-ignore
  80. await audioRef.current?.setSinkId(props.deviceId);
  81. audioRef.current?.play();
  82. } catch (err) {
  83. logger.log('Could not set sink id', err);
  84. }
  85. }
  86. const { children, isSelected, index, deviceId, length, listHeaderId } = props;
  87. const deviceTextId = `choose_speaker${deviceId}`;
  88. const labelledby = `${listHeaderId} ${deviceTextId} `;
  89. /* eslint-disable react/jsx-no-bind */
  90. return (
  91. <li
  92. aria-checked = { isSelected }
  93. aria-labelledby = { labelledby }
  94. aria-posinset = { index }
  95. aria-setsize = { length }
  96. className = 'audio-preview-speaker'
  97. onClick = { _onClick }
  98. onKeyPress = { _onKeyPress }
  99. role = 'radio'
  100. tabIndex = { 0 }>
  101. <ContextMenuItem
  102. accessibilityLabel = ''
  103. icon = { isSelected ? IconCheck : undefined }
  104. selected = { isSelected }
  105. text = { children }
  106. textClassName = { clsx('audio-preview-entry-text', !isSelected && 'left-margin') }>
  107. <Button
  108. className = 'audio-preview-test-button'
  109. label = 'Test'
  110. onClick = { _onTestButtonClick }
  111. onKeyPress = { _onTestButtonClick }
  112. type = { BUTTON_TYPES.SECONDARY } />
  113. </ContextMenuItem>
  114. <audio
  115. preload = 'auto'
  116. ref = { audioRef }
  117. src = { TEST_SOUND_PATH } />
  118. </li>
  119. );
  120. };
  121. export default SpeakerEntry;