您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InfoDialog.web.js 16KB

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