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 17KB

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