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.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // @flow
  2. import Avatar from '@atlaskit/avatar';
  3. import InlineMessage from '@atlaskit/inline-message';
  4. import React from 'react';
  5. import type { Dispatch } from 'redux';
  6. import { createInviteDialogEvent, sendAnalytics } from '../../../../analytics';
  7. import { Dialog, hideDialog } from '../../../../base/dialog';
  8. import { translate, translateToHTML } from '../../../../base/i18n';
  9. import { getLocalParticipant } from '../../../../base/participants';
  10. import { MultiSelectAutocomplete } from '../../../../base/react';
  11. import { connect } from '../../../../base/redux';
  12. import AbstractAddPeopleDialog, {
  13. type Props as AbstractProps,
  14. type State,
  15. _mapStateToProps as _abstractMapStateToProps
  16. } from '../AbstractAddPeopleDialog';
  17. declare var interfaceConfig: Object;
  18. /**
  19. * The type of the React {@code Component} props of {@link AddPeopleDialog}.
  20. */
  21. type Props = AbstractProps & {
  22. /**
  23. * The {@link JitsiMeetConference} which will be used to invite "room"
  24. * participants through the SIP Jibri (Video SIP gateway).
  25. */
  26. _conference: Object,
  27. /**
  28. * Whether to show a footer text after the search results as a last element.
  29. */
  30. _footerTextEnabled: boolean,
  31. /**
  32. * The redux {@code dispatch} function.
  33. */
  34. dispatch: Dispatch<any>,
  35. /**
  36. * Invoked to obtain translated strings.
  37. */
  38. t: Function,
  39. };
  40. /**
  41. * The dialog that allows to invite people to the call.
  42. */
  43. class AddPeopleDialog extends AbstractAddPeopleDialog<Props, State> {
  44. _multiselect = null;
  45. _resourceClient: Object;
  46. state = {
  47. addToCallError: false,
  48. addToCallInProgress: false,
  49. inviteItems: []
  50. };
  51. /**
  52. * Initializes a new {@code AddPeopleDialog} instance.
  53. *
  54. * @param {Object} props - The read-only properties with which the new
  55. * instance is to be initialized.
  56. */
  57. constructor(props: Props) {
  58. super(props);
  59. // Bind event handlers so they are only bound once per instance.
  60. this._onItemSelected = this._onItemSelected.bind(this);
  61. this._onSelectionChange = this._onSelectionChange.bind(this);
  62. this._onSubmit = this._onSubmit.bind(this);
  63. this._parseQueryResults = this._parseQueryResults.bind(this);
  64. this._setMultiSelectElement = this._setMultiSelectElement.bind(this);
  65. this._resourceClient = {
  66. makeQuery: this._query,
  67. parseResults: this._parseQueryResults
  68. };
  69. }
  70. /**
  71. * Sends an analytics event to record the dialog has been shown.
  72. *
  73. * @inheritdoc
  74. * @returns {void}
  75. */
  76. componentDidMount() {
  77. sendAnalytics(createInviteDialogEvent(
  78. 'invite.dialog.opened', 'dialog'));
  79. }
  80. /**
  81. * React Component method that executes once component is updated.
  82. *
  83. * @param {Object} prevProps - The state object before the update.
  84. * @param {Object} prevState - The state object before the update.
  85. * @returns {void}
  86. */
  87. componentDidUpdate(prevProps, prevState) {
  88. /**
  89. * Clears selected items from the multi select component on successful
  90. * invite.
  91. */
  92. if (prevState.addToCallError
  93. && !this.state.addToCallInProgress
  94. && !this.state.addToCallError
  95. && this._multiselect) {
  96. this._multiselect.setSelectedItems([]);
  97. }
  98. }
  99. /**
  100. * Sends an analytics event to record the dialog has been closed.
  101. *
  102. * @inheritdoc
  103. * @returns {void}
  104. */
  105. componentWillUnmount() {
  106. sendAnalytics(createInviteDialogEvent(
  107. 'invite.dialog.closed', 'dialog'));
  108. }
  109. /**
  110. * Renders the content of this component.
  111. *
  112. * @returns {ReactElement}
  113. */
  114. render() {
  115. const {
  116. _addPeopleEnabled,
  117. _dialOutEnabled,
  118. _footerTextEnabled,
  119. t
  120. } = this.props;
  121. let isMultiSelectDisabled = this.state.addToCallInProgress || false;
  122. let placeholder;
  123. let loadingMessage;
  124. let noMatches;
  125. let footerText;
  126. if (_addPeopleEnabled && _dialOutEnabled) {
  127. loadingMessage = 'addPeople.loading';
  128. noMatches = 'addPeople.noResults';
  129. placeholder = 'addPeople.searchPeopleAndNumbers';
  130. } else if (_addPeopleEnabled) {
  131. loadingMessage = 'addPeople.loadingPeople';
  132. noMatches = 'addPeople.noResults';
  133. placeholder = 'addPeople.searchPeople';
  134. } else if (_dialOutEnabled) {
  135. loadingMessage = 'addPeople.loadingNumber';
  136. noMatches = 'addPeople.noValidNumbers';
  137. placeholder = 'addPeople.searchNumbers';
  138. } else {
  139. isMultiSelectDisabled = true;
  140. noMatches = 'addPeople.noResults';
  141. placeholder = 'addPeople.disabled';
  142. }
  143. if (_footerTextEnabled) {
  144. footerText = {
  145. content: <div className = 'footer-text-wrap'>
  146. <div>
  147. <span className = 'footer-telephone-icon'>
  148. <i className = 'icon-phone' />
  149. </span>
  150. </div>
  151. { translateToHTML(t, 'addPeople.footerText') }
  152. </div>
  153. };
  154. }
  155. return (
  156. <Dialog
  157. okDisabled = { this._isAddDisabled() }
  158. okKey = 'addPeople.add'
  159. onSubmit = { this._onSubmit }
  160. titleKey = 'addPeople.title'
  161. width = 'medium'>
  162. <div className = 'add-people-form-wrap'>
  163. { this._renderErrorMessage() }
  164. <MultiSelectAutocomplete
  165. footer = { footerText }
  166. isDisabled = { isMultiSelectDisabled }
  167. loadingMessage = { t(loadingMessage) }
  168. noMatchesFound = { t(noMatches) }
  169. onItemSelected = { this._onItemSelected }
  170. onSelectionChange = { this._onSelectionChange }
  171. placeholder = { t(placeholder) }
  172. ref = { this._setMultiSelectElement }
  173. resourceClient = { this._resourceClient }
  174. shouldFitContainer = { true }
  175. shouldFocus = { true } />
  176. </div>
  177. </Dialog>
  178. );
  179. }
  180. _invite: Array<Object> => Promise<*>
  181. _isAddDisabled: () => boolean;
  182. _onItemSelected: (Object) => Object;
  183. /**
  184. * Callback invoked when a selection has been made but before it has been
  185. * set as selected.
  186. *
  187. * @param {Object} item - The item that has just been selected.
  188. * @private
  189. * @returns {Object} The item to display as selected in the input.
  190. */
  191. _onItemSelected(item) {
  192. if (item.item.type === 'phone') {
  193. item.content = item.item.number;
  194. }
  195. return item;
  196. }
  197. _onSelectionChange: (Map<*, *>) => void;
  198. /**
  199. * Handles a selection change.
  200. *
  201. * @param {Map} selectedItems - The list of selected items.
  202. * @private
  203. * @returns {void}
  204. */
  205. _onSelectionChange(selectedItems) {
  206. this.setState({
  207. inviteItems: selectedItems
  208. });
  209. }
  210. _onSubmit: () => void;
  211. /**
  212. * Submits the selection for inviting.
  213. *
  214. * @private
  215. * @returns {void}
  216. */
  217. _onSubmit() {
  218. const { inviteItems } = this.state;
  219. const invitees = inviteItems.map(({ item }) => item);
  220. this._invite(invitees)
  221. .then(invitesLeftToSend => {
  222. if (invitesLeftToSend.length) {
  223. const unsentInviteIDs
  224. = invitesLeftToSend.map(invitee =>
  225. invitee.id || invitee.user_id || invitee.number);
  226. const itemsToSelect
  227. = inviteItems.filter(({ item }) =>
  228. unsentInviteIDs.includes(item.id || item.user_id || item.number));
  229. if (this._multiselect) {
  230. this._multiselect.setSelectedItems(itemsToSelect);
  231. }
  232. } else {
  233. this.props.dispatch(hideDialog());
  234. }
  235. });
  236. }
  237. _parseQueryResults: (?Array<Object>) => Array<Object>;
  238. /**
  239. * Processes results from requesting available numbers and people by munging
  240. * each result into a format {@code MultiSelectAutocomplete} can use for
  241. * display.
  242. *
  243. * @param {Array} response - The response object from the server for the
  244. * query.
  245. * @private
  246. * @returns {Object[]} Configuration objects for items to display in the
  247. * search autocomplete.
  248. */
  249. _parseQueryResults(response = []) {
  250. const { t } = this.props;
  251. const users = response.filter(item => item.type !== 'phone');
  252. const userDisplayItems = users.map(user => {
  253. return {
  254. content: user.name,
  255. elemBefore: <Avatar
  256. size = 'small'
  257. src = { user.avatar } />,
  258. item: user,
  259. tag: {
  260. elemBefore: <Avatar
  261. size = 'xsmall'
  262. src = { user.avatar } />
  263. },
  264. value: user.id || user.user_id
  265. };
  266. });
  267. const numbers = response.filter(item => item.type === 'phone');
  268. const telephoneIcon = this._renderTelephoneIcon();
  269. const numberDisplayItems = numbers.map(number => {
  270. const numberNotAllowedMessage
  271. = number.allowed ? '' : t('addPeople.countryNotSupported');
  272. const countryCodeReminder = number.showCountryCodeReminder
  273. ? t('addPeople.countryReminder') : '';
  274. const description
  275. = `${numberNotAllowedMessage} ${countryCodeReminder}`.trim();
  276. return {
  277. filterValues: [
  278. number.originalEntry,
  279. number.number
  280. ],
  281. content: t('addPeople.telephone', { number: number.number }),
  282. description,
  283. isDisabled: !number.allowed,
  284. elemBefore: telephoneIcon,
  285. item: number,
  286. tag: {
  287. elemBefore: telephoneIcon
  288. },
  289. value: number.number
  290. };
  291. });
  292. return [
  293. ...userDisplayItems,
  294. ...numberDisplayItems
  295. ];
  296. }
  297. _query: (string) => Promise<Array<Object>>;
  298. /**
  299. * Renders the error message if the add doesn't succeed.
  300. *
  301. * @private
  302. * @returns {ReactElement|null}
  303. */
  304. _renderErrorMessage() {
  305. if (!this.state.addToCallError) {
  306. return null;
  307. }
  308. const { t } = this.props;
  309. const supportString = t('inlineDialogFailure.supportMsg');
  310. const supportLink = interfaceConfig.SUPPORT_URL;
  311. const supportLinkContent
  312. = (
  313. <span>
  314. <span>
  315. { supportString.padEnd(supportString.length + 1) }
  316. </span>
  317. <span>
  318. <a
  319. href = { supportLink }
  320. rel = 'noopener noreferrer'
  321. target = '_blank'>
  322. { t('inlineDialogFailure.support') }
  323. </a>
  324. </span>
  325. <span>.</span>
  326. </span>
  327. );
  328. return (
  329. <div className = 'modal-dialog-form-error'>
  330. <InlineMessage
  331. title = { t('addPeople.failedToAdd') }
  332. type = 'error'>
  333. { supportLinkContent }
  334. </InlineMessage>
  335. </div>
  336. );
  337. }
  338. /**
  339. * Renders a telephone icon.
  340. *
  341. * @private
  342. * @returns {ReactElement}
  343. */
  344. _renderTelephoneIcon() {
  345. return (
  346. <span className = 'add-telephone-icon'>
  347. <i className = 'icon-phone' />
  348. </span>
  349. );
  350. }
  351. _setMultiSelectElement: (React$ElementRef<*> | null) => mixed;
  352. /**
  353. * Sets the instance variable for the multi select component
  354. * element so it can be accessed directly.
  355. *
  356. * @param {Object} element - The DOM element for the component's dialog.
  357. * @private
  358. * @returns {void}
  359. */
  360. _setMultiSelectElement(element) {
  361. this._multiselect = element;
  362. }
  363. }
  364. /**
  365. * Maps (parts of) the Redux state to the associated
  366. * {@code AddPeopleDialog}'s props.
  367. *
  368. * @param {Object} state - The Redux state.
  369. * @private
  370. * @returns {{
  371. * _dialOutAuthUrl: string,
  372. * _jwt: string,
  373. * _peopleSearchQueryTypes: Array<string>,
  374. * _peopleSearchUrl: string
  375. * }}
  376. */
  377. function _mapStateToProps(state) {
  378. const {
  379. enableFeaturesBasedOnToken
  380. } = state['features/base/config'];
  381. let footerTextEnabled = false;
  382. if (enableFeaturesBasedOnToken) {
  383. const { features = {} } = getLocalParticipant(state);
  384. if (String(features['outbound-call']) !== 'true') {
  385. footerTextEnabled = true;
  386. }
  387. }
  388. return {
  389. ..._abstractMapStateToProps(state),
  390. _footerTextEnabled: footerTextEnabled
  391. };
  392. }
  393. export default translate(connect(_mapStateToProps)(AddPeopleDialog));