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.

AddPeopleDialog.web.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // @flow
  2. import Avatar from '@atlaskit/avatar';
  3. import InlineMessage from '@atlaskit/inline-message';
  4. import PropTypes from 'prop-types';
  5. import React, { Component } from 'react';
  6. import { connect } from 'react-redux';
  7. import { createInviteDialogEvent, sendAnalytics } from '../../analytics';
  8. import { getInviteURL } from '../../base/connection';
  9. import { Dialog, hideDialog } from '../../base/dialog';
  10. import { translate } from '../../base/i18n';
  11. import { MultiSelectAutocomplete } from '../../base/react';
  12. import { inviteVideoRooms } from '../../videosipgw';
  13. import {
  14. sendInvitesForItems,
  15. getInviteResultsForQuery
  16. } from '../functions';
  17. const logger = require('jitsi-meet-logger').getLogger(__filename);
  18. declare var interfaceConfig: Object;
  19. /**
  20. * The dialog that allows to invite people to the call.
  21. */
  22. class AddPeopleDialog extends Component<*, *> {
  23. /**
  24. * {@code AddPeopleDialog}'s property types.
  25. *
  26. * @static
  27. */
  28. static propTypes = {
  29. /**
  30. * The {@link JitsiMeetConference} which will be used to invite "room"
  31. * participants through the SIP Jibri (Video SIP gateway).
  32. */
  33. _conference: PropTypes.object,
  34. /**
  35. * The URL for validating if a phone number can be called.
  36. */
  37. _dialOutAuthUrl: PropTypes.string,
  38. /**
  39. * The URL pointing to the service allowing for people invite.
  40. */
  41. _inviteServiceUrl: PropTypes.string,
  42. /**
  43. * The url of the conference to invite people to.
  44. */
  45. _inviteUrl: PropTypes.string,
  46. /**
  47. * The JWT token.
  48. */
  49. _jwt: PropTypes.string,
  50. /**
  51. * The query types used when searching people.
  52. */
  53. _peopleSearchQueryTypes: PropTypes.arrayOf(PropTypes.string),
  54. /**
  55. * The URL pointing to the service allowing for people search.
  56. */
  57. _peopleSearchUrl: PropTypes.string,
  58. /**
  59. * Whether or not to show Add People functionality.
  60. */
  61. addPeopleEnabled: PropTypes.bool,
  62. /**
  63. * Whether or not to show Dial Out functionality.
  64. */
  65. dialOutEnabled: PropTypes.bool,
  66. /**
  67. * The function closing the dialog.
  68. */
  69. hideDialog: PropTypes.func,
  70. /**
  71. * Used to invite video rooms.
  72. */
  73. inviteVideoRooms: PropTypes.func,
  74. /**
  75. * Invoked to obtain translated strings.
  76. */
  77. t: PropTypes.func
  78. };
  79. _multiselect = null;
  80. _resourceClient: Object;
  81. state = {
  82. /**
  83. * Indicating that an error occurred when adding people to the call.
  84. */
  85. addToCallError: false,
  86. /**
  87. * Indicating that we're currently adding the new people to the
  88. * call.
  89. */
  90. addToCallInProgress: false,
  91. /**
  92. * The list of invite items.
  93. */
  94. inviteItems: []
  95. };
  96. /**
  97. * Initializes a new {@code AddPeopleDialog} instance.
  98. *
  99. * @param {Object} props - The read-only properties with which the new
  100. * instance is to be initialized.
  101. */
  102. constructor(props) {
  103. super(props);
  104. // Bind event handlers so they are only bound once per instance.
  105. this._isAddDisabled = this._isAddDisabled.bind(this);
  106. this._onItemSelected = this._onItemSelected.bind(this);
  107. this._onSelectionChange = this._onSelectionChange.bind(this);
  108. this._onSubmit = this._onSubmit.bind(this);
  109. this._parseQueryResults = this._parseQueryResults.bind(this);
  110. this._query = this._query.bind(this);
  111. this._setMultiSelectElement = this._setMultiSelectElement.bind(this);
  112. this._resourceClient = {
  113. makeQuery: this._query,
  114. parseResults: this._parseQueryResults
  115. };
  116. }
  117. /**
  118. * Sends an analytics event to record the dialog has been shown.
  119. *
  120. * @inheritdoc
  121. * @returns {void}
  122. */
  123. componentDidMount() {
  124. sendAnalytics(createInviteDialogEvent(
  125. 'invite.dialog.opened', 'dialog'));
  126. }
  127. /**
  128. * React Component method that executes once component is updated.
  129. *
  130. * @param {Object} prevState - The state object before the update.
  131. * @returns {void}
  132. */
  133. componentDidUpdate(prevState) {
  134. /**
  135. * Clears selected items from the multi select component on successful
  136. * invite.
  137. */
  138. if (prevState.addToCallError
  139. && !this.state.addToCallInProgress
  140. && !this.state.addToCallError
  141. && this._multiselect) {
  142. this._multiselect.setSelectedItems([]);
  143. }
  144. }
  145. /**
  146. * Sends an analytics event to record the dialog has been closed.
  147. *
  148. * @inheritdoc
  149. * @returns {void}
  150. */
  151. componentWillUnmount() {
  152. sendAnalytics(createInviteDialogEvent(
  153. 'invite.dialog.closed', 'dialog'));
  154. }
  155. /**
  156. * Renders the content of this component.
  157. *
  158. * @returns {ReactElement}
  159. */
  160. render() {
  161. const { addPeopleEnabled, dialOutEnabled, t } = this.props;
  162. let isMultiSelectDisabled = this.state.addToCallInProgress || false;
  163. let placeholder;
  164. let loadingMessage;
  165. let noMatches;
  166. if (addPeopleEnabled && dialOutEnabled) {
  167. loadingMessage = 'addPeople.loading';
  168. noMatches = 'addPeople.noResults';
  169. placeholder = 'addPeople.searchPeopleAndNumbers';
  170. } else if (addPeopleEnabled) {
  171. loadingMessage = 'addPeople.loadingPeople';
  172. noMatches = 'addPeople.noResults';
  173. placeholder = 'addPeople.searchPeople';
  174. } else if (dialOutEnabled) {
  175. loadingMessage = 'addPeople.loadingNumber';
  176. noMatches = 'addPeople.noValidNumbers';
  177. placeholder = 'addPeople.searchNumbers';
  178. } else {
  179. isMultiSelectDisabled = true;
  180. noMatches = 'addPeople.noResults';
  181. placeholder = 'addPeople.disabled';
  182. }
  183. return (
  184. <Dialog
  185. okDisabled = { this._isAddDisabled() }
  186. okTitleKey = 'addPeople.add'
  187. onSubmit = { this._onSubmit }
  188. titleKey = 'addPeople.title'
  189. width = 'medium'>
  190. <div className = 'add-people-form-wrap'>
  191. { this._renderErrorMessage() }
  192. <MultiSelectAutocomplete
  193. isDisabled = { isMultiSelectDisabled }
  194. loadingMessage = { t(loadingMessage) }
  195. noMatchesFound = { t(noMatches) }
  196. onItemSelected = { this._onItemSelected }
  197. onSelectionChange = { this._onSelectionChange }
  198. placeholder = { t(placeholder) }
  199. ref = { this._setMultiSelectElement }
  200. resourceClient = { this._resourceClient }
  201. shouldFitContainer = { true }
  202. shouldFocus = { true } />
  203. </div>
  204. </Dialog>
  205. );
  206. }
  207. /**
  208. * Helper for determining how many of each type of user is being invited.
  209. * Used for logging and sending analytics related to invites.
  210. *
  211. * @param {Array} inviteItems - An array with the invite items, as created
  212. * in {@link _parseQueryResults}.
  213. * @private
  214. * @returns {Object} An object with keys as user types and values as the
  215. * number of invites for that type.
  216. */
  217. _getInviteTypeCounts(inviteItems = []) {
  218. const inviteTypeCounts = {};
  219. inviteItems.forEach(i => {
  220. const type = i.item.type;
  221. if (!inviteTypeCounts[type]) {
  222. inviteTypeCounts[type] = 0;
  223. }
  224. inviteTypeCounts[type]++;
  225. });
  226. return inviteTypeCounts;
  227. }
  228. _isAddDisabled: () => boolean;
  229. /**
  230. * Indicates if the Add button should be disabled.
  231. *
  232. * @private
  233. * @returns {boolean} - True to indicate that the Add button should
  234. * be disabled, false otherwise.
  235. */
  236. _isAddDisabled() {
  237. return !this.state.inviteItems.length
  238. || this.state.addToCallInProgress;
  239. }
  240. _onItemSelected: (Object) => Object;
  241. /**
  242. * Callback invoked when a selection has been made but before it has been
  243. * set as selected.
  244. *
  245. * @param {Object} item - The item that has just been selected.
  246. * @private
  247. * @returns {Object} The item to display as selected in the input.
  248. */
  249. _onItemSelected(item) {
  250. if (item.item.type === 'phone') {
  251. item.content = item.item.number;
  252. }
  253. return item;
  254. }
  255. _onSelectionChange: (Map<*, *>) => void;
  256. /**
  257. * Handles a selection change.
  258. *
  259. * @param {Map} selectedItems - The list of selected items.
  260. * @private
  261. * @returns {void}
  262. */
  263. _onSelectionChange(selectedItems) {
  264. this.setState({
  265. inviteItems: selectedItems
  266. });
  267. }
  268. _onSubmit: () => void;
  269. /**
  270. * Invite people and numbers to the conference. The logic works by inviting
  271. * numbers, people/rooms, and videosipgw in parallel. All invitees are
  272. * stored in an array. As each invite succeeds, the invitee is removed
  273. * from the array. After all invites finish, close the modal if there are
  274. * no invites left to send. If any are left, that means an invite failed
  275. * and an error state should display.
  276. *
  277. * @private
  278. * @returns {void}
  279. */
  280. _onSubmit() {
  281. const inviteTypeCounts
  282. = this._getInviteTypeCounts(this.state.inviteItems);
  283. sendAnalytics(createInviteDialogEvent(
  284. 'clicked', 'inviteButton', {
  285. ...inviteTypeCounts,
  286. inviteAllowed: this._isAddDisabled()
  287. }));
  288. if (this._isAddDisabled()) {
  289. return;
  290. }
  291. this.setState({
  292. addToCallInProgress: true
  293. });
  294. const {
  295. _conference,
  296. _inviteServiceUrl,
  297. _inviteUrl,
  298. _jwt
  299. } = this.props;
  300. const inviteItems = this.state.inviteItems;
  301. const items = inviteItems.map(item => item.item);
  302. const options = {
  303. conference: _conference,
  304. inviteServiceUrl: _inviteServiceUrl,
  305. inviteUrl: _inviteUrl,
  306. inviteVideoRooms: this.props.inviteVideoRooms,
  307. jwt: _jwt
  308. };
  309. sendInvitesForItems(items, options)
  310. .then(invitesLeftToSend => {
  311. // If any invites are left that means something failed to send
  312. // so treat it as an error.
  313. if (invitesLeftToSend.length) {
  314. const erroredInviteTypeCounts
  315. = this._getInviteTypeCounts(invitesLeftToSend);
  316. logger.error(`${invitesLeftToSend.length} invites failed`,
  317. erroredInviteTypeCounts);
  318. sendAnalytics(createInviteDialogEvent(
  319. 'error', 'invite', {
  320. ...erroredInviteTypeCounts
  321. }));
  322. this.setState({
  323. addToCallInProgress: false,
  324. addToCallError: true
  325. });
  326. const unsentInviteIDs = invitesLeftToSend.map(invite =>
  327. invite.id || invite.number
  328. );
  329. const itemsToSelect = inviteItems.filter(invite =>
  330. unsentInviteIDs.includes(
  331. invite.item.id || invite.item.number
  332. )
  333. );
  334. if (this._multiselect) {
  335. this._multiselect.setSelectedItems(itemsToSelect);
  336. }
  337. return;
  338. }
  339. this.setState({
  340. addToCallInProgress: false
  341. });
  342. this.props.hideDialog();
  343. });
  344. }
  345. _parseQueryResults: (Array<Object>, string) => Array<Object>;
  346. /**
  347. * Processes results from requesting available numbers and people by munging
  348. * each result into a format {@code MultiSelectAutocomplete} can use for
  349. * display.
  350. *
  351. * @param {Array} response - The response object from the server for the
  352. * query.
  353. * @private
  354. * @returns {Object[]} Configuration objects for items to display in the
  355. * search autocomplete.
  356. */
  357. _parseQueryResults(response = []) {
  358. const { t } = this.props;
  359. const users = response.filter(item => item.type !== 'phone');
  360. const userDisplayItems = users.map(user => {
  361. return {
  362. content: user.name,
  363. elemBefore: <Avatar
  364. size = 'small'
  365. src = { user.avatar } />,
  366. item: user,
  367. tag: {
  368. elemBefore: <Avatar
  369. size = 'xsmall'
  370. src = { user.avatar } />
  371. },
  372. value: user.id
  373. };
  374. });
  375. const numbers = response.filter(item => item.type === 'phone');
  376. const telephoneIcon = this._renderTelephoneIcon();
  377. const numberDisplayItems = numbers.map(number => {
  378. const numberNotAllowedMessage
  379. = number.allowed ? '' : t('addPeople.countryNotSupported');
  380. const countryCodeReminder = number.showCountryCodeReminder
  381. ? t('addPeople.countryReminder') : '';
  382. const description
  383. = `${numberNotAllowedMessage} ${countryCodeReminder}`.trim();
  384. return {
  385. filterValues: [
  386. number.originalEntry,
  387. number.number
  388. ],
  389. content: t('addPeople.telephone', { number: number.number }),
  390. description,
  391. isDisabled: !number.allowed,
  392. elemBefore: telephoneIcon,
  393. item: number,
  394. tag: {
  395. elemBefore: telephoneIcon
  396. },
  397. value: number.number
  398. };
  399. });
  400. return [
  401. ...userDisplayItems,
  402. ...numberDisplayItems
  403. ];
  404. }
  405. _query: (string) => Promise<Array<Object>>;
  406. /**
  407. * Performs a people and phone number search request.
  408. *
  409. * @param {string} query - The search text.
  410. * @private
  411. * @returns {Promise}
  412. */
  413. _query(query = '') {
  414. const {
  415. addPeopleEnabled,
  416. dialOutEnabled,
  417. _dialOutAuthUrl,
  418. _jwt,
  419. _peopleSearchQueryTypes,
  420. _peopleSearchUrl
  421. } = this.props;
  422. const options = {
  423. dialOutAuthUrl: _dialOutAuthUrl,
  424. addPeopleEnabled,
  425. dialOutEnabled,
  426. jwt: _jwt,
  427. peopleSearchQueryTypes: _peopleSearchQueryTypes,
  428. peopleSearchUrl: _peopleSearchUrl
  429. };
  430. return getInviteResultsForQuery(query, options);
  431. }
  432. /**
  433. * Renders the error message if the add doesn't succeed.
  434. *
  435. * @private
  436. * @returns {ReactElement|null}
  437. */
  438. _renderErrorMessage() {
  439. if (!this.state.addToCallError) {
  440. return null;
  441. }
  442. const { t } = this.props;
  443. const supportString = t('inlineDialogFailure.supportMsg');
  444. const supportLink = interfaceConfig.SUPPORT_URL;
  445. const supportLinkContent
  446. = ( // eslint-disable-line no-extra-parens
  447. <span>
  448. <span>
  449. { supportString.padEnd(supportString.length + 1) }
  450. </span>
  451. <span>
  452. <a
  453. href = { supportLink }
  454. rel = 'noopener noreferrer'
  455. target = '_blank'>
  456. { t('inlineDialogFailure.support') }
  457. </a>
  458. </span>
  459. <span>.</span>
  460. </span>
  461. );
  462. return (
  463. <div className = 'modal-dialog-form-error'>
  464. <InlineMessage
  465. title = { t('addPeople.failedToAdd') }
  466. type = 'error'>
  467. { supportLinkContent }
  468. </InlineMessage>
  469. </div>
  470. );
  471. }
  472. /**
  473. * Renders a telephone icon.
  474. *
  475. * @private
  476. * @returns {ReactElement}
  477. */
  478. _renderTelephoneIcon() {
  479. return (
  480. <span className = 'add-telephone-icon'>
  481. <i className = 'icon-telephone' />
  482. </span>
  483. );
  484. }
  485. _setMultiSelectElement: (React$ElementRef<*> | null) => mixed;
  486. /**
  487. * Sets the instance variable for the multi select component
  488. * element so it can be accessed directly.
  489. *
  490. * @param {Object} element - The DOM element for the component's dialog.
  491. * @private
  492. * @returns {void}
  493. */
  494. _setMultiSelectElement(element) {
  495. this._multiselect = element;
  496. }
  497. }
  498. /**
  499. * Maps (parts of) the Redux state to the associated
  500. * {@code AddPeopleDialog}'s props.
  501. *
  502. * @param {Object} state - The Redux state.
  503. * @private
  504. * @returns {{
  505. * _conference: Object,
  506. * _dialOutAuthUrl: string,
  507. * _inviteServiceUrl: string,
  508. * _inviteUrl: string,
  509. * _jwt: string,
  510. * _peopleSearchQueryTypes: Array<string>,
  511. * _peopleSearchUrl: string
  512. * }}
  513. */
  514. function _mapStateToProps(state) {
  515. const { conference } = state['features/base/conference'];
  516. const {
  517. dialOutAuthUrl,
  518. inviteServiceUrl,
  519. peopleSearchQueryTypes,
  520. peopleSearchUrl
  521. } = state['features/base/config'];
  522. return {
  523. _conference: conference,
  524. _dialOutAuthUrl: dialOutAuthUrl,
  525. _inviteServiceUrl: inviteServiceUrl,
  526. _inviteUrl: getInviteURL(state),
  527. _jwt: state['features/base/jwt'].jwt,
  528. _peopleSearchQueryTypes: peopleSearchQueryTypes,
  529. _peopleSearchUrl: peopleSearchUrl
  530. };
  531. }
  532. export default translate(
  533. connect(
  534. _mapStateToProps,
  535. /* mapDispatchToProps */ {
  536. hideDialog,
  537. inviteVideoRooms
  538. })(
  539. AddPeopleDialog));