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

InfoDialog.web.js 16KB

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