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 13KB

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