Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InfoDialog.web.js 16KB

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