Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractAddPeopleDialog.tsx 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { Component } from 'react';
  2. import { createInviteDialogEvent } from '../../../analytics/AnalyticsEvents';
  3. import { sendAnalytics } from '../../../analytics/functions';
  4. import { IReduxState } from '../../../app/types';
  5. import { showErrorNotification, showNotification } from '../../../notifications/actions';
  6. import { NOTIFICATION_TIMEOUT_TYPE } from '../../../notifications/constants';
  7. import { INotificationProps } from '../../../notifications/types';
  8. import { invite } from '../../actions';
  9. import { INVITE_TYPES } from '../../constants';
  10. import {
  11. getInviteResultsForQuery,
  12. getInviteTypeCounts,
  13. isAddPeopleEnabled,
  14. isDialOutEnabled,
  15. isSipInviteEnabled
  16. } from '../../functions';
  17. import logger from '../../logger';
  18. import { IInviteSelectItem, IInvitee } from '../../types';
  19. export interface IProps {
  20. /**
  21. * Whether or not to show Add People functionality.
  22. */
  23. _addPeopleEnabled: boolean;
  24. /**
  25. * The app id of the user.
  26. */
  27. _appId: string;
  28. /**
  29. * Whether or not call flows are enabled.
  30. */
  31. _callFlowsEnabled: boolean;
  32. /**
  33. * The URL for validating if a phone number can be called.
  34. */
  35. _dialOutAuthUrl: string;
  36. /**
  37. * Whether or not to show Dial Out functionality.
  38. */
  39. _dialOutEnabled: boolean;
  40. /**
  41. * The URL for validating if an outbound destination is allowed.
  42. */
  43. _dialOutRegionUrl: string;
  44. /**
  45. * The JWT token.
  46. */
  47. _jwt: string;
  48. /**
  49. * The query types used when searching people.
  50. */
  51. _peopleSearchQueryTypes: Array<string>;
  52. /**
  53. * The URL pointing to the service allowing for people search.
  54. */
  55. _peopleSearchUrl: string;
  56. /**
  57. * Whether or not to allow sip invites.
  58. */
  59. _sipInviteEnabled: boolean;
  60. /**
  61. * The Redux dispatch function.
  62. */
  63. dispatch: Function;
  64. }
  65. export interface IState {
  66. /**
  67. * Indicating that an error occurred when adding people to the call.
  68. */
  69. addToCallError: boolean;
  70. /**
  71. * Indicating that we're currently adding the new people to the
  72. * call.
  73. */
  74. addToCallInProgress: boolean;
  75. /**
  76. * The list of invite items.
  77. */
  78. inviteItems: Array<IInviteSelectItem>;
  79. }
  80. /**
  81. * Implements an abstract dialog to invite people to the conference.
  82. */
  83. export default class AbstractAddPeopleDialog<P extends IProps, S extends IState> extends Component<P, S> {
  84. /**
  85. * Constructor of the component.
  86. *
  87. * @inheritdoc
  88. */
  89. constructor(props: P) {
  90. super(props);
  91. this._query = this._query.bind(this);
  92. }
  93. /**
  94. * Retrieves the notification display name for the invitee.
  95. *
  96. * @param {IInvitee} invitee - The invitee object.
  97. * @returns {string}
  98. */
  99. _getDisplayName(invitee: IInvitee) {
  100. if (invitee.type === INVITE_TYPES.PHONE) {
  101. return invitee.number;
  102. }
  103. if (invitee.type === INVITE_TYPES.SIP) {
  104. return invitee.address;
  105. }
  106. return invitee.name ?? '';
  107. }
  108. /**
  109. * Invite people and numbers to the conference. The logic works by inviting
  110. * numbers, people/rooms, sip endpoints and videosipgw in parallel. All invitees are
  111. * stored in an array. As each invite succeeds, the invitee is removed
  112. * from the array. After all invites finish, close the modal if there are
  113. * no invites left to send. If any are left, that means an invite failed
  114. * and an error state should display.
  115. *
  116. * @param {Array<IInvitee>} invitees - The items to be invited.
  117. * @returns {Promise<Array<any>>}
  118. */
  119. _invite(invitees: IInvitee[]) {
  120. const inviteTypeCounts = getInviteTypeCounts(invitees);
  121. sendAnalytics(createInviteDialogEvent(
  122. 'clicked', 'inviteButton', {
  123. ...inviteTypeCounts,
  124. inviteAllowed: this._isAddDisabled()
  125. }));
  126. if (this._isAddDisabled()) {
  127. return Promise.resolve([]);
  128. }
  129. this.setState({
  130. addToCallInProgress: true
  131. });
  132. const { _callFlowsEnabled, dispatch } = this.props;
  133. return dispatch(invite(invitees))
  134. .then((invitesLeftToSend: IInvitee[]) => {
  135. this.setState({
  136. addToCallInProgress: false
  137. });
  138. // If any invites are left that means something failed to send
  139. // so treat it as an error.
  140. if (invitesLeftToSend.length) {
  141. const erroredInviteTypeCounts
  142. = getInviteTypeCounts(invitesLeftToSend);
  143. logger.error(`${invitesLeftToSend.length} invites failed`,
  144. erroredInviteTypeCounts);
  145. sendAnalytics(createInviteDialogEvent(
  146. 'error', 'invite', {
  147. ...erroredInviteTypeCounts
  148. }));
  149. dispatch(showErrorNotification({
  150. titleKey: 'addPeople.failedToAdd'
  151. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  152. } else if (!_callFlowsEnabled) {
  153. const invitedCount = invitees.length;
  154. let notificationProps: INotificationProps | undefined;
  155. if (invitedCount >= 3) {
  156. notificationProps = {
  157. titleArguments: {
  158. name: this._getDisplayName(invitees[0]),
  159. count: `${invitedCount - 1}`
  160. },
  161. titleKey: 'notify.invitedThreePlusMembers'
  162. };
  163. } else if (invitedCount === 2) {
  164. notificationProps = {
  165. titleArguments: {
  166. first: this._getDisplayName(invitees[0]),
  167. second: this._getDisplayName(invitees[1])
  168. },
  169. titleKey: 'notify.invitedTwoMembers'
  170. };
  171. } else if (invitedCount) {
  172. notificationProps = {
  173. titleArguments: {
  174. name: this._getDisplayName(invitees[0])
  175. },
  176. titleKey: 'notify.invitedOneMember'
  177. };
  178. }
  179. if (notificationProps) {
  180. dispatch(
  181. showNotification(notificationProps, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  182. }
  183. }
  184. return invitesLeftToSend;
  185. });
  186. }
  187. /**
  188. * Indicates if the Add button should be disabled.
  189. *
  190. * @private
  191. * @returns {boolean} - True to indicate that the Add button should
  192. * be disabled, false otherwise.
  193. */
  194. _isAddDisabled() {
  195. return !this.state.inviteItems.length
  196. || this.state.addToCallInProgress;
  197. }
  198. /**
  199. * Performs a people and phone number search request.
  200. *
  201. * @param {string} query - The search text.
  202. * @private
  203. * @returns {Promise}
  204. */
  205. _query(query = '') {
  206. const {
  207. _addPeopleEnabled: addPeopleEnabled,
  208. _appId: appId,
  209. _dialOutAuthUrl: dialOutAuthUrl,
  210. _dialOutRegionUrl: dialOutRegionUrl,
  211. _dialOutEnabled: dialOutEnabled,
  212. _jwt: jwt,
  213. _peopleSearchQueryTypes: peopleSearchQueryTypes,
  214. _peopleSearchUrl: peopleSearchUrl,
  215. _sipInviteEnabled: sipInviteEnabled
  216. } = this.props;
  217. const options = {
  218. addPeopleEnabled,
  219. appId,
  220. dialOutAuthUrl,
  221. dialOutEnabled,
  222. dialOutRegionUrl,
  223. jwt,
  224. peopleSearchQueryTypes,
  225. peopleSearchUrl,
  226. sipInviteEnabled
  227. };
  228. return getInviteResultsForQuery(query, options);
  229. }
  230. }
  231. /**
  232. * Maps (parts of) the Redux state to the props of this component.
  233. *
  234. * @param {Object} state - The Redux state.
  235. * @private
  236. * @returns {{
  237. * _addPeopleEnabled: boolean,
  238. * _dialOutAuthUrl: string,
  239. * _dialOutEnabled: boolean,
  240. * _jwt: string,
  241. * _peopleSearchQueryTypes: Array<string>,
  242. * _peopleSearchUrl: string
  243. * }}
  244. */
  245. export function _mapStateToProps(state: IReduxState) {
  246. const {
  247. callFlowsEnabled,
  248. dialOutAuthUrl,
  249. dialOutRegionUrl,
  250. peopleSearchQueryTypes,
  251. peopleSearchUrl
  252. } = state['features/base/config'];
  253. return {
  254. _addPeopleEnabled: isAddPeopleEnabled(state),
  255. _appId: state['features/base/jwt']?.tenant ?? '',
  256. _callFlowsEnabled: callFlowsEnabled ?? false,
  257. _dialOutAuthUrl: dialOutAuthUrl ?? '',
  258. _dialOutRegionUrl: dialOutRegionUrl ?? '',
  259. _dialOutEnabled: isDialOutEnabled(state),
  260. _jwt: state['features/base/jwt'].jwt ?? '',
  261. _peopleSearchQueryTypes: peopleSearchQueryTypes ?? [],
  262. _peopleSearchUrl: peopleSearchUrl ?? '',
  263. _sipInviteEnabled: isSipInviteEnabled(state)
  264. };
  265. }