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.

InfoDialog.web.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { setPassword } from '../../../base/conference';
  5. import { getInviteURL } from '../../../base/connection';
  6. import { translate } from '../../../base/i18n';
  7. import {
  8. PARTICIPANT_ROLE,
  9. getLocalParticipant
  10. } from '../../../base/participants';
  11. import { updateDialInNumbers } from '../../actions';
  12. import DialInNumber from './DialInNumber';
  13. import PasswordForm from './PasswordForm';
  14. const logger = require('jitsi-meet-logger').getLogger(__filename);
  15. /**
  16. * A React Component with the contents for a dialog that shows information about
  17. * the current conference.
  18. *
  19. * @extends Component
  20. */
  21. class InfoDialog extends Component {
  22. /**
  23. * {@code InfoDialog} component's property types.
  24. *
  25. * @static
  26. */
  27. static propTypes = {
  28. /**
  29. * Whether or not the current user can modify the current password.
  30. */
  31. _canEditPassword: PropTypes.bool,
  32. /**
  33. * The JitsiConference for which to display a lock state and change the
  34. * password.
  35. *
  36. * @type {JitsiConference}
  37. */
  38. _conference: PropTypes.object,
  39. /**
  40. * The name of the current conference. Used as part of inviting users.
  41. */
  42. _conferenceName: PropTypes.string,
  43. /**
  44. * The redux state representing the dial-in numbers feature.
  45. */
  46. _dialIn: PropTypes.object,
  47. /**
  48. * The current url of the conference to be copied onto the clipboard.
  49. */
  50. _inviteURL: PropTypes.string,
  51. /**
  52. * The value for how the conference is locked (or undefined if not
  53. * locked) as defined by room-lock constants.
  54. */
  55. _locked: PropTypes.string,
  56. /**
  57. * The current known password for the JitsiConference.
  58. */
  59. _password: PropTypes.string,
  60. /**
  61. * Invoked to open a dialog for adding participants to the conference.
  62. */
  63. dispatch: PropTypes.func,
  64. /**
  65. * Callback invoked when the dialog should be closed.
  66. */
  67. onClose: PropTypes.func,
  68. /**
  69. * Callback invoked when a mouse-related event has been detected.
  70. */
  71. onMouseOver: PropTypes.func,
  72. /**
  73. * Invoked to obtain translated strings.
  74. */
  75. t: PropTypes.func
  76. };
  77. /**
  78. * {@code InfoDialog} component's local state.
  79. *
  80. * @type {Object}
  81. * @property {boolean} passwordEditEnabled - Whether or not to show the
  82. * {@code PasswordForm} in its editing state.
  83. * @property {string} phoneNumber - The number to display for dialing into
  84. * the conference.
  85. */
  86. state = {
  87. passwordEditEnabled: false,
  88. phoneNumber: ''
  89. };
  90. /**
  91. * Initializes new {@code InfoDialog} instance.
  92. *
  93. * @param {Object} props - The read-only properties with which the new
  94. * instance is to be initialized.
  95. */
  96. constructor(props) {
  97. super(props);
  98. const { defaultCountry, numbers } = props._dialIn;
  99. if (numbers) {
  100. this.state.phoneNumber
  101. = this._getDefaultPhoneNumber(numbers, defaultCountry);
  102. }
  103. /**
  104. * The internal reference to the DOM/HTML element backing the React
  105. * {@code Component} text area. It is necessary for the implementation
  106. * of copying to the clipboard.
  107. *
  108. * @private
  109. * @type {HTMLTextAreaElement}
  110. */
  111. this._copyElement = null;
  112. // Bind event handlers so they are only bound once for every instance.
  113. this._onCopyInviteURL = this._onCopyInviteURL.bind(this);
  114. this._onPasswordRemove = this._onPasswordRemove.bind(this);
  115. this._onPasswordSubmit = this._onPasswordSubmit.bind(this);
  116. this._onTogglePasswordEditState
  117. = this._onTogglePasswordEditState.bind(this);
  118. this._setCopyElement = this._setCopyElement.bind(this);
  119. }
  120. /**
  121. * Implements {@link Component#componentDidMount()}. Invoked immediately
  122. * after this component is mounted. Requests dial-in numbers if not
  123. * already known.
  124. *
  125. * @inheritdoc
  126. * @returns {void}
  127. */
  128. componentDidMount() {
  129. if (!this.state.phoneNumber) {
  130. this.props.dispatch(updateDialInNumbers());
  131. }
  132. }
  133. /**
  134. * Implements React's {@link Component#componentWillReceiveProps()}. Invoked
  135. * before this mounted component receives new props.
  136. *
  137. * @inheritdoc
  138. * @param {Props} nextProps - New props component will receive.
  139. */
  140. componentWillReceiveProps(nextProps) {
  141. if (!this.props._password && nextProps._password) {
  142. this.setState({ passwordEditEnabled: false });
  143. }
  144. if (!this.state.phoneNumber && nextProps._dialIn.numbers) {
  145. const { defaultCountry, numbers } = nextProps._dialIn;
  146. this.setState({
  147. phoneNumber:
  148. this._getDefaultPhoneNumber(numbers, defaultCountry)
  149. });
  150. }
  151. }
  152. /**
  153. * Implements React's {@link Component#render()}.
  154. *
  155. * @inheritdoc
  156. * @returns {ReactElement}
  157. */
  158. render() {
  159. const { onMouseOver, t } = this.props;
  160. return (
  161. <div
  162. className = 'info-dialog'
  163. onMouseOver = { onMouseOver } >
  164. <div className = 'info-dialog-column'>
  165. <h4 className = 'info-dialog-icon'>
  166. <i className = 'icon-info' />
  167. </h4>
  168. </div>
  169. <div className = 'info-dialog-column'>
  170. <div className = 'info-dialog-title'>
  171. { t('info.title') }
  172. </div>
  173. <div className = 'info-dialog-conference-url'>
  174. { t('info.conferenceURL',
  175. { url: this._getURLToDisplay() }) }
  176. <textarea
  177. className = 'info-dialog-copy-element'
  178. readOnly = { true }
  179. ref = { this._setCopyElement }
  180. tabIndex = '-1'
  181. value = { this._getTextToCopy() } />
  182. </div>
  183. <div className = 'info-dialog-dial-in'>
  184. { this._renderDialInDisplay() }
  185. </div>
  186. <div className = 'info-dialog-password'>
  187. <PasswordForm
  188. editEnabled = { this.state.passwordEditEnabled }
  189. locked = { this.props._locked }
  190. onSubmit = { this._onPasswordSubmit }
  191. password = { this.props._password } />
  192. </div>
  193. <div className = 'info-dialog-action-links'>
  194. <div className = 'info-dialog-action-link'>
  195. <a
  196. className = 'info-copy'
  197. onClick = { this._onCopyInviteURL }>
  198. { t('dialog.copy') }
  199. </a>
  200. </div>
  201. { this._renderPasswordAction() }
  202. </div>
  203. </div>
  204. </div>
  205. );
  206. }
  207. /**
  208. * Sets the internal state of which dial-in number to display.
  209. *
  210. * @param {Array<string>|Object} dialInNumbers - The array or object of
  211. * numbers to choose a number from.
  212. * @param {string} defaultCountry - The country code for the country
  213. * whose phone number should display.
  214. * @private
  215. * @returns {string|null}
  216. */
  217. _getDefaultPhoneNumber(dialInNumbers, defaultCountry = 'US') {
  218. if (Array.isArray(dialInNumbers)) {
  219. // Dumbly return the first number if an array.
  220. return dialInNumbers[0];
  221. } else if (Object.keys(dialInNumbers).length > 0) {
  222. const defaultNumbers = dialInNumbers[defaultCountry];
  223. if (defaultNumbers) {
  224. return defaultNumbers[0];
  225. }
  226. const firstRegion = Object.keys(dialInNumbers)[0];
  227. return firstRegion && firstRegion[0];
  228. }
  229. return null;
  230. }
  231. /**
  232. * Generates the URL for the static dial in info page.
  233. *
  234. * @private
  235. * @returns {string}
  236. */
  237. _getDialInfoPageURL() {
  238. return `${window.location.origin}/static/dialInInfo.html?room=${
  239. encodeURIComponent(this.props._conferenceName)}`;
  240. }
  241. /**
  242. * Creates a message describing how to dial in to the conference.
  243. *
  244. * @private
  245. * @returns {string}
  246. */
  247. _getTextToCopy() {
  248. const { t } = this.props;
  249. let invite = t('info.inviteURL', {
  250. url: this.props._inviteURL
  251. });
  252. if (this._shouldDisplayDialIn()) {
  253. const dial = t('info.invitePhone', {
  254. number: this.state.phoneNumber,
  255. conferenceID: this.props._dialIn.conferenceID
  256. });
  257. const moreNumbers = t('info.invitePhoneAlternatives', {
  258. url: this._getDialInfoPageURL()
  259. });
  260. invite = `${invite}\n${dial}\n${moreNumbers}`;
  261. }
  262. return invite;
  263. }
  264. /**
  265. * Modifies the inviteURL for display in the modal.
  266. *
  267. * @private
  268. * @returns {string}
  269. */
  270. _getURLToDisplay() {
  271. return this.props._inviteURL.replace(/^https?:\/\//i, '');
  272. }
  273. /**
  274. * Callback invoked to copy the contents of {@code this._copyElement} to the
  275. * clipboard.
  276. *
  277. * @private
  278. * @returns {void}
  279. */
  280. _onCopyInviteURL() {
  281. try {
  282. this._copyElement.select();
  283. document.execCommand('copy');
  284. this._copyElement.blur();
  285. } catch (err) {
  286. logger.error('error when copying the text', err);
  287. }
  288. }
  289. /**
  290. * Callback invoked to unlock the current JitsiConference.
  291. *
  292. * @private
  293. * @returns {void}
  294. */
  295. _onPasswordRemove() {
  296. this._onPasswordSubmit('');
  297. }
  298. /**
  299. * Callback invoked to set a password on the current JitsiConference.
  300. *
  301. * @param {string} enteredPassword - The new password to be used to lock the
  302. * current JitsiConference.
  303. * @private
  304. * @returns {void}
  305. */
  306. _onPasswordSubmit(enteredPassword) {
  307. const { _conference } = this.props;
  308. this.props.dispatch(setPassword(
  309. _conference,
  310. _conference.lock,
  311. enteredPassword
  312. ));
  313. }
  314. /**
  315. * Toggles whether or not the password should currently be shown as being
  316. * edited locally.
  317. *
  318. * @private
  319. * @returns {void}
  320. */
  321. _onTogglePasswordEditState() {
  322. this.setState({
  323. passwordEditEnabled: !this.state.passwordEditEnabled
  324. });
  325. }
  326. /**
  327. * Returns a ReactElement for showing how to dial into the conference, if
  328. * dialing in is available.
  329. *
  330. * @private
  331. * @returns {null|ReactElement}
  332. */
  333. _renderDialInDisplay() {
  334. if (!this._shouldDisplayDialIn()) {
  335. return null;
  336. }
  337. return (
  338. <div>
  339. <DialInNumber
  340. conferenceID = { this.props._dialIn.conferenceID }
  341. phoneNumber = { this.state.phoneNumber } />
  342. <a
  343. className = 'more-numbers'
  344. href = { this._getDialInfoPageURL() }
  345. rel = 'noopener noreferrer'
  346. target = '_blank'>
  347. { this.props.t('info.moreNumbers') }
  348. </a>
  349. </div>
  350. );
  351. }
  352. /**
  353. * Returns a ReactElement for interacting with the password field.
  354. *
  355. * @private
  356. * @returns {null|ReactElement}
  357. */
  358. _renderPasswordAction() {
  359. const { t } = this.props;
  360. let className, onClick, textKey;
  361. if (!this.props._canEditPassword) {
  362. // intentionally left blank to prevent rendering anything
  363. } else if (this.state.passwordEditEnabled) {
  364. className = 'cancel-password';
  365. onClick = this._onTogglePasswordEditState;
  366. textKey = 'info.cancelPassword';
  367. } else if (this.props._locked) {
  368. className = 'remove-password';
  369. onClick = this._onPasswordRemove;
  370. textKey = 'dialog.removePassword';
  371. } else {
  372. className = 'add-password';
  373. onClick = this._onTogglePasswordEditState;
  374. textKey = 'info.addPassword';
  375. }
  376. return className && onClick && textKey
  377. ? <div className = 'info-dialog-action-link'>
  378. <a
  379. className = { className }
  380. onClick = { onClick }>
  381. { t(textKey) }
  382. </a>
  383. </div>
  384. : null;
  385. }
  386. /**
  387. * Returns whether or not dial-in related UI should be displayed.
  388. *
  389. * @private
  390. * @returns {boolean}
  391. */
  392. _shouldDisplayDialIn() {
  393. const { conferenceID, numbers, numbersEnabled } = this.props._dialIn;
  394. const { phoneNumber } = this.state;
  395. return Boolean(
  396. conferenceID
  397. && numbers
  398. && numbersEnabled
  399. && phoneNumber);
  400. }
  401. /**
  402. * Sets the internal reference to the DOM/HTML element backing the React
  403. * {@code Component} input.
  404. *
  405. * @param {HTMLInputElement} element - The DOM/HTML element for this
  406. * {@code Component}'s input.
  407. * @private
  408. * @returns {void}
  409. */
  410. _setCopyElement(element) {
  411. this._copyElement = element;
  412. }
  413. }
  414. /**
  415. * Maps (parts of) the Redux state to the associated props for the
  416. * {@code InfoDialog} component.
  417. *
  418. * @param {Object} state - The Redux state.
  419. * @private
  420. * @returns {{
  421. * _canEditPassword: boolean,
  422. * _conference: Object,
  423. * _conferenceName: string,
  424. * _dialIn: Object,
  425. * _inviteURL: string,
  426. * _locked: string,
  427. * _password: string
  428. * }}
  429. */
  430. function _mapStateToProps(state) {
  431. const {
  432. conference,
  433. locked,
  434. password,
  435. room
  436. } = state['features/base/conference'];
  437. const isModerator
  438. = getLocalParticipant(state).role === PARTICIPANT_ROLE.MODERATOR;
  439. let canEditPassword;
  440. if (state['features/base/config'].enableUserRolesBasedOnToken) {
  441. canEditPassword = isModerator && !state['features/base/jwt'].isGuest;
  442. } else {
  443. canEditPassword = isModerator;
  444. }
  445. return {
  446. _canEditPassword: canEditPassword,
  447. _conference: conference,
  448. _conferenceName: room,
  449. _dialIn: state['features/invite'],
  450. _inviteURL: getInviteURL(state),
  451. _locked: locked,
  452. _password: password
  453. };
  454. }
  455. export default translate(connect(_mapStateToProps)(InfoDialog));