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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. const origin = window.location.origin;
  239. const encodedConferenceName
  240. = encodeURIComponent(this.props._conferenceName);
  241. const pathParts = window.location.pathname.split('/');
  242. pathParts.length = pathParts.length - 1;
  243. const newPath = pathParts.reduce((accumulator, currentValue) => {
  244. if (currentValue) {
  245. return `${accumulator}/${currentValue}`;
  246. }
  247. return accumulator;
  248. }, '');
  249. return `${origin}${newPath}/static/dialInInfo.html?room=${
  250. encodedConferenceName}`;
  251. }
  252. /**
  253. * Creates a message describing how to dial in to the conference.
  254. *
  255. * @private
  256. * @returns {string}
  257. */
  258. _getTextToCopy() {
  259. const { t } = this.props;
  260. let invite = t('info.inviteURL', {
  261. url: this.props._inviteURL
  262. });
  263. if (this._shouldDisplayDialIn()) {
  264. const dial = t('info.invitePhone', {
  265. number: this.state.phoneNumber,
  266. conferenceID: this.props._dialIn.conferenceID
  267. });
  268. const moreNumbers = t('info.invitePhoneAlternatives', {
  269. url: this._getDialInfoPageURL()
  270. });
  271. invite = `${invite}\n${dial}\n${moreNumbers}`;
  272. }
  273. return invite;
  274. }
  275. /**
  276. * Modifies the inviteURL for display in the modal.
  277. *
  278. * @private
  279. * @returns {string}
  280. */
  281. _getURLToDisplay() {
  282. return this.props._inviteURL.replace(/^https?:\/\//i, '');
  283. }
  284. /**
  285. * Callback invoked to copy the contents of {@code this._copyElement} to the
  286. * clipboard.
  287. *
  288. * @private
  289. * @returns {void}
  290. */
  291. _onCopyInviteURL() {
  292. try {
  293. this._copyElement.select();
  294. document.execCommand('copy');
  295. this._copyElement.blur();
  296. } catch (err) {
  297. logger.error('error when copying the text', err);
  298. }
  299. }
  300. /**
  301. * Callback invoked to unlock the current JitsiConference.
  302. *
  303. * @private
  304. * @returns {void}
  305. */
  306. _onPasswordRemove() {
  307. this._onPasswordSubmit('');
  308. }
  309. /**
  310. * Callback invoked to set a password on the current JitsiConference.
  311. *
  312. * @param {string} enteredPassword - The new password to be used to lock the
  313. * current JitsiConference.
  314. * @private
  315. * @returns {void}
  316. */
  317. _onPasswordSubmit(enteredPassword) {
  318. const { _conference } = this.props;
  319. this.props.dispatch(setPassword(
  320. _conference,
  321. _conference.lock,
  322. enteredPassword
  323. ));
  324. }
  325. /**
  326. * Toggles whether or not the password should currently be shown as being
  327. * edited locally.
  328. *
  329. * @private
  330. * @returns {void}
  331. */
  332. _onTogglePasswordEditState() {
  333. this.setState({
  334. passwordEditEnabled: !this.state.passwordEditEnabled
  335. });
  336. }
  337. /**
  338. * Returns a ReactElement for showing how to dial into the conference, if
  339. * dialing in is available.
  340. *
  341. * @private
  342. * @returns {null|ReactElement}
  343. */
  344. _renderDialInDisplay() {
  345. if (!this._shouldDisplayDialIn()) {
  346. return null;
  347. }
  348. return (
  349. <div>
  350. <DialInNumber
  351. conferenceID = { this.props._dialIn.conferenceID }
  352. phoneNumber = { this.state.phoneNumber } />
  353. <a
  354. className = 'more-numbers'
  355. href = { this._getDialInfoPageURL() }
  356. rel = 'noopener noreferrer'
  357. target = '_blank'>
  358. { this.props.t('info.moreNumbers') }
  359. </a>
  360. </div>
  361. );
  362. }
  363. /**
  364. * Returns a ReactElement for interacting with the password field.
  365. *
  366. * @private
  367. * @returns {null|ReactElement}
  368. */
  369. _renderPasswordAction() {
  370. const { t } = this.props;
  371. let className, onClick, textKey;
  372. if (!this.props._canEditPassword) {
  373. // intentionally left blank to prevent rendering anything
  374. } else if (this.state.passwordEditEnabled) {
  375. className = 'cancel-password';
  376. onClick = this._onTogglePasswordEditState;
  377. textKey = 'info.cancelPassword';
  378. } else if (this.props._locked) {
  379. className = 'remove-password';
  380. onClick = this._onPasswordRemove;
  381. textKey = 'dialog.removePassword';
  382. } else {
  383. className = 'add-password';
  384. onClick = this._onTogglePasswordEditState;
  385. textKey = 'info.addPassword';
  386. }
  387. return className && onClick && textKey
  388. ? <div className = 'info-dialog-action-link'>
  389. <a
  390. className = { className }
  391. onClick = { onClick }>
  392. { t(textKey) }
  393. </a>
  394. </div>
  395. : null;
  396. }
  397. /**
  398. * Returns whether or not dial-in related UI should be displayed.
  399. *
  400. * @private
  401. * @returns {boolean}
  402. */
  403. _shouldDisplayDialIn() {
  404. const { conferenceID, numbers, numbersEnabled } = this.props._dialIn;
  405. const { phoneNumber } = this.state;
  406. return Boolean(
  407. conferenceID
  408. && numbers
  409. && numbersEnabled
  410. && phoneNumber);
  411. }
  412. /**
  413. * Sets the internal reference to the DOM/HTML element backing the React
  414. * {@code Component} input.
  415. *
  416. * @param {HTMLInputElement} element - The DOM/HTML element for this
  417. * {@code Component}'s input.
  418. * @private
  419. * @returns {void}
  420. */
  421. _setCopyElement(element) {
  422. this._copyElement = element;
  423. }
  424. }
  425. /**
  426. * Maps (parts of) the Redux state to the associated props for the
  427. * {@code InfoDialog} component.
  428. *
  429. * @param {Object} state - The Redux state.
  430. * @private
  431. * @returns {{
  432. * _canEditPassword: boolean,
  433. * _conference: Object,
  434. * _conferenceName: string,
  435. * _dialIn: Object,
  436. * _inviteURL: string,
  437. * _locked: string,
  438. * _password: string
  439. * }}
  440. */
  441. function _mapStateToProps(state) {
  442. const {
  443. conference,
  444. locked,
  445. password,
  446. room
  447. } = state['features/base/conference'];
  448. const isModerator
  449. = getLocalParticipant(state).role === PARTICIPANT_ROLE.MODERATOR;
  450. let canEditPassword;
  451. if (state['features/base/config'].enableUserRolesBasedOnToken) {
  452. canEditPassword = isModerator && !state['features/base/jwt'].isGuest;
  453. } else {
  454. canEditPassword = isModerator;
  455. }
  456. return {
  457. _canEditPassword: canEditPassword,
  458. _conference: conference,
  459. _conferenceName: room,
  460. _dialIn: state['features/invite'],
  461. _inviteURL: getInviteURL(state),
  462. _locked: locked,
  463. _password: password
  464. };
  465. }
  466. export default translate(connect(_mapStateToProps)(InfoDialog));