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.

DialInNumbersForm.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import Button from '@atlaskit/button';
  2. import DropdownMenu, {
  3. DropdownItem, DropdownItemGroup } from '@atlaskit/dropdown-menu';
  4. import PropTypes from 'prop-types';
  5. import React, { Component } from 'react';
  6. import { connect } from 'react-redux';
  7. import { translate } from '../../base/i18n';
  8. import { getLocalParticipant } from '../../base/participants';
  9. import { updateDialInNumbers } from '../actions';
  10. const logger = require('jitsi-meet-logger').getLogger(__filename);
  11. /**
  12. * React {@code Component} responsible for fetching and displaying telephone
  13. * numbers for dialing into a conference. Also supports copying a selected
  14. * dial-in number to the clipboard.
  15. *
  16. * @extends Component
  17. */
  18. class DialInNumbersForm extends Component {
  19. /**
  20. * {@code DialInNumbersForm}'s property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. /**
  26. * The redux state representing the dial-in numbers feature.
  27. */
  28. _dialIn: PropTypes.object,
  29. /**
  30. * The display name of the local user.
  31. */
  32. _localUserDisplayName: PropTypes.string,
  33. /**
  34. * Invoked to send an ajax request for dial-in numbers.
  35. */
  36. dispatch: PropTypes.func,
  37. /**
  38. * The URL of the conference into which this {@code DialInNumbersForm}
  39. * is inviting the local participant.
  40. */
  41. inviteURL: PropTypes.string,
  42. /**
  43. * Invoked to obtain translated strings.
  44. */
  45. t: PropTypes.func
  46. };
  47. /**
  48. * Initializes a new {@code DialInNumbersForm} instance.
  49. *
  50. * @param {Object} props - The read-only properties with which the new
  51. * instance is to be initialized.
  52. */
  53. constructor(props) {
  54. super(props);
  55. this.state = {
  56. /**
  57. * Whether or not the dropdown should be open.
  58. *
  59. * @type {boolean}
  60. */
  61. isDropdownOpen: false,
  62. /**
  63. * The dial-in number to display as currently selected in the
  64. * dropdown. The value should be an object which has two key/value
  65. * pairs, content and number. The value of "content" will display in
  66. * the dropdown while the value of "number" is a substring of
  67. * "content" which will be copied to clipboard.
  68. *
  69. * @type {object}
  70. */
  71. selectedNumber: null
  72. };
  73. /**
  74. * The internal reference to the DOM/HTML element backing the React
  75. * {@code Component} text area. It is necessary for the implementation
  76. * of copying to the clipboard.
  77. *
  78. * @private
  79. * @type {HTMLTextAreaElement}
  80. */
  81. this._copyElement = null;
  82. // Bind event handlers so they are only bound once for every instance.
  83. this._onCopyClick = this._onCopyClick.bind(this);
  84. this._onOpenChange = this._onOpenChange.bind(this);
  85. this._onSelect = this._onSelect.bind(this);
  86. this._setCopyElement = this._setCopyElement.bind(this);
  87. }
  88. /**
  89. * Sets a default number to display in the dropdown trigger.
  90. *
  91. * @inheritdoc
  92. * returns {void}
  93. */
  94. componentWillMount() {
  95. const { numbers } = this.props._dialIn;
  96. if (numbers) {
  97. this._setDefaultNumber(numbers);
  98. } else {
  99. this.props.dispatch(updateDialInNumbers());
  100. }
  101. }
  102. /**
  103. * Monitors for number updates and sets a default number to display in the
  104. * dropdown trigger if not already set.
  105. *
  106. * @inheritdoc
  107. * returns {void}
  108. */
  109. componentWillReceiveProps(nextProps) {
  110. if (!this.state.selectedNumber && nextProps._dialIn.numbers) {
  111. this._setDefaultNumber(nextProps._dialIn.numbers);
  112. }
  113. }
  114. /**
  115. * Implements React's {@link Component#render()}. Returns null if the
  116. * component is not ready for display.
  117. *
  118. * @inheritdoc
  119. * @returns {ReactElement|null}
  120. */
  121. render() {
  122. const { _dialIn, t } = this.props;
  123. const { conferenceID, numbers, numbersEnabled } = _dialIn;
  124. const { selectedNumber } = this.state;
  125. if (!conferenceID || !numbers || !numbersEnabled || !selectedNumber) {
  126. return null;
  127. }
  128. const items = this._renderDropdownItems(numbers);
  129. return (
  130. <div className = 'form-control dial-in-numbers'>
  131. <label className = 'form-control__label'>
  132. { t('invite.howToDialIn') }
  133. <span className = 'dial-in-numbers-conference-id'>
  134. { conferenceID }
  135. </span>
  136. </label>
  137. <div className = 'form-control__container'>
  138. <div className = 'form-control__input-container'>
  139. { this._createDropdownMenu(items, selectedNumber) }
  140. </div>
  141. <Button
  142. appearance = 'default'
  143. onClick = { this._onCopyClick }
  144. type = 'button'>
  145. { t('dialog.copy') }
  146. </Button>
  147. </div>
  148. <textarea
  149. className = 'dial-in-numbers-copy'
  150. readOnly = { true }
  151. ref = { this._setCopyElement }
  152. tabIndex = '-1'
  153. value = { this._generateCopyText() } />
  154. </div>
  155. );
  156. }
  157. /**
  158. * Creates a {@code DropdownMenu} instance.
  159. *
  160. * @param {Array} items - The content to display within the dropdown.
  161. * @param {string} triggerText - The text to display within the
  162. * trigger element.
  163. * @returns {ReactElement}
  164. */
  165. _createDropdownMenu(items, triggerText) {
  166. return (
  167. <DropdownMenu
  168. isOpen = { this.state.isDropdownOpen }
  169. onOpenChange = { this._onOpenChange }
  170. shouldFitContainer = { true }
  171. trigger = { triggerText || '' }
  172. triggerButtonProps = {{
  173. className: 'dropdown-button-trigger',
  174. shouldFitContainer: true }}
  175. triggerType = 'button'>
  176. <DropdownItemGroup>
  177. { items }
  178. </DropdownItemGroup>
  179. </DropdownMenu>
  180. );
  181. }
  182. /**
  183. * Formats the region and number string.
  184. *
  185. * @param {string} region - The region string.
  186. * @param {string} number - The number string.
  187. * @returns {string} - The new formatted string.
  188. * @private
  189. */
  190. _formatRegionNumber(region, number) {
  191. return `${region}: ${number}`;
  192. }
  193. /**
  194. * Creates a message describing how to dial in to the conference.
  195. *
  196. * @private
  197. * @returns {string}
  198. */
  199. _generateCopyText() {
  200. const { t } = this.props;
  201. const welcome = t('invite.invitedYouTo', {
  202. inviteURL: this.props.inviteURL,
  203. userName: this.props._localUserDisplayName
  204. });
  205. const callNumber = t('invite.callNumber', {
  206. number: this.state.selectedNumber
  207. });
  208. const stepOne = `1) ${callNumber}`;
  209. const enterID = t('invite.enterID', {
  210. conferenceID: this.props._dialIn.conferenceID
  211. });
  212. const stepTwo = `2) ${enterID}`;
  213. return `${welcome}\n${stepOne}\n${stepTwo}`;
  214. }
  215. /**
  216. * Copies part of the number displayed in the dropdown trigger into the
  217. * clipboard. Only the value specified in selectedNumber.number, which
  218. * should be a substring of the displayed value, will be copied.
  219. *
  220. * @private
  221. * @returns {void}
  222. */
  223. _onCopyClick() {
  224. try {
  225. this._copyElement.select();
  226. document.execCommand('copy');
  227. this._copyElement.blur();
  228. } catch (err) {
  229. logger.error('error when copying the text', err);
  230. }
  231. }
  232. /**
  233. * Sets the internal state to either open or close the dropdown. If the
  234. * dropdown is disabled, the state will always be set to false.
  235. *
  236. * @param {Object} dropdownEvent - The even returned from clicking on the
  237. * dropdown trigger.
  238. * @private
  239. * @returns {void}
  240. */
  241. _onOpenChange(dropdownEvent) {
  242. this.setState({
  243. isDropdownOpen: dropdownEvent.isOpen
  244. });
  245. }
  246. /**
  247. * Updates the internal state of the currently selected number.
  248. *
  249. * @param {Object} selection - Event from choosing an dropdown option.
  250. * @private
  251. * @returns {void}
  252. */
  253. _onSelect(selection) {
  254. this.setState({
  255. isDropdownOpen: false,
  256. selectedNumber: selection
  257. });
  258. }
  259. /**
  260. * Renders a DropDownItem for the given id and text.
  261. *
  262. * @param {string} id - The key identifier of the DropdownItem.
  263. * @param {string} text - The text to display in the dropdown item.
  264. * @returns {React.Component}
  265. * @private
  266. */
  267. _renderDropDownItem(id, text) {
  268. return (
  269. /**
  270. * Arrow functions are not allowed in props, but I leave this until
  271. * I figure a better way to implement the same thing.
  272. */
  273. /* eslint-disable react/jsx-no-bind */
  274. <DropdownItem
  275. key = { id }
  276. onClick = { () => this._onSelect(text || id) }>
  277. { text }
  278. </DropdownItem>
  279. /* eslint-disable react/jsx-no-bind */
  280. );
  281. }
  282. /**
  283. * Detects whether the response from dialInNumbersUrl returned an array or
  284. * an object with dial-in numbers and calls the appropriate method to
  285. * transform the numbers into the format expected by
  286. * {@code DropdownMenu}.
  287. *
  288. * @param {Array<string>|Object} dialInNumbers - The numbers returned from
  289. * requesting dialInNumbersUrl.
  290. * @private
  291. * @returns {Array<Object>}
  292. */
  293. _renderDropdownItems(dialInNumbers) {
  294. if (Array.isArray(dialInNumbers)) {
  295. return dialInNumbers.map(number =>
  296. this._renderDropDownItem(number)
  297. );
  298. }
  299. const phoneRegions = Object.keys(dialInNumbers);
  300. if (!phoneRegions.length) {
  301. return [];
  302. }
  303. const dropdownItems = phoneRegions.map(region => {
  304. const numbers = dialInNumbers[region];
  305. return numbers.map(number =>
  306. this._renderDropDownItem(number,
  307. this._formatRegionNumber(region, number))
  308. );
  309. });
  310. return Array.prototype.concat(...dropdownItems);
  311. }
  312. /**
  313. * Sets the internal reference to the DOM/HTML element backing the React
  314. * {@code Component} text area.
  315. *
  316. * @param {HTMLTextAreaElement} element - The DOM/HTML element for this
  317. * {@code Component}'s text area.
  318. * @private
  319. * @returns {void}
  320. */
  321. _setCopyElement(element) {
  322. this._copyElement = element;
  323. }
  324. /**
  325. * Updates the internal state of the currently selected number by defaulting
  326. * to the first available number.
  327. *
  328. * @param {Object} dialInNumbers - The array or object of numbers to parse.
  329. * @private
  330. * @returns {void}
  331. */
  332. _setDefaultNumber(dialInNumbers) {
  333. let number = '';
  334. if (Array.isArray(dialInNumbers)) {
  335. number = dialInNumbers[0];
  336. } else if (Object.keys(dialInNumbers).length > 0) {
  337. const region = Object.keys(dialInNumbers)[0];
  338. number = this._formatRegionNumber(region, dialInNumbers[region]);
  339. }
  340. this.setState({
  341. selectedNumber: number
  342. });
  343. }
  344. }
  345. /**
  346. * Maps (parts of) the Redux state to the associated
  347. * {@code DialInNumbersForm}'s props.
  348. *
  349. * @param {Object} state - The Redux state.
  350. * @private
  351. * @returns {{
  352. * _dialIn: Object,
  353. * _localUserDisplayName: string
  354. * }}
  355. */
  356. function _mapStateToProps(state) {
  357. return {
  358. _localUserDisplayName: getLocalParticipant(state).name,
  359. _dialIn: state['features/invite']
  360. };
  361. }
  362. export default translate(connect(_mapStateToProps)(DialInNumbersForm));