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

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