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

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