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

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