UNPKG

@atlaskit/mention

Version:

A React component used to display user profiles in a list for 'Mention' functionality

285 lines (280 loc) 13.3 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _get from "@babel/runtime/helpers/get"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import _regeneratorRuntime from "@babel/runtime/regenerator"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; } import { SLI_EVENT_TYPE } from './../util/analytics'; import { utils as serviceUtils } from '@atlaskit/util-service-support'; import { UserType, UserAccessLevel, SliNames, Actions } from '../types'; import MentionResource from './MentionResource'; import debug from '../util/logger'; var MAX_QUERY_TEAMS = 20; /** * Provides a Javascript API to fetch users and teams * In future we will have a new endpoint to return both users and teams, we can * remove this class at this point */ var TeamMentionResource = /*#__PURE__*/function (_MentionResource) { function TeamMentionResource(userMentionConfig, teamMentionConfig) { var _this; _classCallCheck(this, TeamMentionResource); _this = _callSuper(this, TeamMentionResource, [userMentionConfig]); _defineProperty(_this, "lastSearchQuery", ''); _this.verifyMentionConfig(teamMentionConfig); _this.teamMentionConfig = teamMentionConfig; _this.lastReturnedSearchTeam = 0; return _this; } _inherits(TeamMentionResource, _MentionResource); return _createClass(TeamMentionResource, [{ key: "filter", value: function filter(query, contextIdentifier) { this.lastSearchQuery = query; if (!query) { return this.remoteInitialStateTeamAndUsers(contextIdentifier); } else { this.updateActiveSearches(query); // both user and team requests start at the same time var getUserPromise = this.remoteUserSearch(query, contextIdentifier); var getTeamsPromise = this.remoteTeamSearch(query, contextIdentifier); return this.handleBothRequests(query, getUserPromise, getTeamsPromise); } } /** * Returns the initial mention display list before a search is performed for the specified * container. */ }, { key: "remoteInitialStateTeamAndUsers", value: (function () { var _remoteInitialStateTeamAndUsers = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(contextIdentifier) { var emptyQuery, getUserPromise, queryParams, options, getTeamsPromise; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: emptyQuery = ''; getUserPromise = _superPropGet(TeamMentionResource, "remoteInitialState", this, 3)([contextIdentifier]); queryParams = this.getQueryParamsOfTeamMentionConfig(contextIdentifier); options = { path: 'bootstrap', queryParams: queryParams }; getTeamsPromise = serviceUtils.requestService(this.teamMentionConfig, options); this.handleBothRequests(emptyQuery, getUserPromise, getTeamsPromise); case 6: case "end": return _context.stop(); } }, _callee, this); })); function remoteInitialStateTeamAndUsers(_x) { return _remoteInitialStateTeamAndUsers.apply(this, arguments); } return remoteInitialStateTeamAndUsers; }() /** * Both user and team requests are not blocked together * If users request arrives first, show users. Show teams when team request arrives. * If team request arrives first, block waiting for user request, then show both * If one errors, show the non-erroring one * If both error, show error */ ) }, { key: "handleBothRequests", value: (function () { var _handleBothRequests = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(query, userRequest, teamRequest) { var _this2 = this; var searchTime, accumulatedResults, notifyWhenOneRequestDone, userResults, userRequestError, teamRequestError, teamsResult; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: searchTime = Date.now(); accumulatedResults = { mentions: [], query: query }; notifyWhenOneRequestDone = function notifyWhenOneRequestDone(results, hasTeamResults) { // just update UI for the last query string if (query !== _this2.lastSearchQuery) { return; } accumulatedResults = { mentions: [].concat(_toConsumableArray(accumulatedResults.mentions), _toConsumableArray(results.mentions)), query: query }; // we need to calculate different `duration` for user and team request. if (hasTeamResults) { _this2.notify(searchTime, accumulatedResults, query); } else { _superPropGet(TeamMentionResource, "notify", _this2, 3)([searchTime, accumulatedResults, query]); } }; userRequestError = null; teamRequestError = null; _context2.prev = 5; _context2.next = 8; return userRequest; case 8: userResults = _context2.sent; notifyWhenOneRequestDone(userResults, false); _context2.next = 15; break; case 12: _context2.prev = 12; _context2.t0 = _context2["catch"](5); userRequestError = _context2.t0; case 15: _context2.prev = 15; _context2.next = 18; return teamRequest; case 18: teamsResult = _context2.sent; // update search time after team results returns notifyWhenOneRequestDone(Array.isArray(teamsResult) ? this.convertTeamResultToMentionResult(teamsResult, query) : teamsResult, true); _context2.next = 25; break; case 22: _context2.prev = 22; _context2.t1 = _context2["catch"](15); teamRequestError = _context2.t1; case 25: // both requests fail, show one of errors in UI if (userRequestError && teamRequestError) { this.notifyError(userRequestError, query); debug('User mention request fails. ', userRequestError); debug('Team mention request fails. ', teamRequestError); } case 26: case "end": return _context2.stop(); } }, _callee2, this, [[5, 12], [15, 22]]); })); function handleBothRequests(_x2, _x3, _x4) { return _handleBothRequests.apply(this, arguments); } return handleBothRequests; }()) }, { key: "notify", value: function notify(searchTime, mentionResult, query) { if (searchTime > this.lastReturnedSearchTeam) { this.lastReturnedSearchTeam = searchTime; this._notifyListeners(mentionResult, { teamMentionDuration: Date.now() - searchTime }); } else { var date = new Date(searchTime).toISOString().substr(17, 6); debug('Stale search result, skipping', date, query); // eslint-disable-line no-console, max-len } this._notifyAllResultsListeners(mentionResult); } }, { key: "getQueryParamsOfTeamMentionConfig", value: function getQueryParamsOfTeamMentionConfig(contextIdentifier) { var configParams = {}; if (this.teamMentionConfig.containerId) { configParams['containerId'] = this.teamMentionConfig.containerId; } if (this.teamMentionConfig.productId) { configParams['productIdentifier'] = this.teamMentionConfig.productId; } // if contextParams exist then it will override configParams for containerId return _objectSpread(_objectSpread({}, configParams), contextIdentifier); } }, { key: "remoteUserSearch", value: function remoteUserSearch(query, contextIdentifier) { return _superPropGet(TeamMentionResource, "remoteSearch", this, 3)([query, contextIdentifier]); } }, { key: "remoteTeamSearch", value: function () { var _remoteTeamSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(query, contextIdentifier) { var options, teamResult; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: options = { path: 'search', queryParams: _objectSpread({ query: query, limit: MAX_QUERY_TEAMS }, this.getQueryParamsOfTeamMentionConfig(contextIdentifier)) }; _context3.prev = 1; _context3.next = 4; return serviceUtils.requestService(this.teamMentionConfig, options); case 4: teamResult = _context3.sent; this._notifyAnalyticsListeners(SLI_EVENT_TYPE, SliNames.SEARCH_TEAM, Actions.SUCCEEDED); return _context3.abrupt("return", this.convertTeamResultToMentionResult(teamResult, query)); case 9: _context3.prev = 9; _context3.t0 = _context3["catch"](1); this._notifyAnalyticsListeners(SLI_EVENT_TYPE, SliNames.SEARCH_TEAM, Actions.FAILED); throw _context3.t0; case 13: case "end": return _context3.stop(); } }, _callee3, this, [[1, 9]]); })); function remoteTeamSearch(_x5, _x6) { return _remoteTeamSearch.apply(this, arguments); } return remoteTeamSearch; }() }, { key: "convertTeamResultToMentionResult", value: function convertTeamResultToMentionResult(result, query) { var _this3 = this; var teamLinkResolver = this.teamMentionConfig.teamLinkResolver; var mentions = result.map(function (team) { var teamLink = ''; var defaultTeamLink = "".concat(window.location.origin, "/people/team/").concat(team.id); if (typeof teamLinkResolver === 'function') { teamLink = teamLinkResolver(team.id); } return { id: _this3.trimTeamARI(team.id), avatarUrl: team.smallAvatarImageUrl, name: team.displayName, accessLevel: UserAccessLevel[UserAccessLevel.CONTAINER], userType: UserType[UserType.TEAM], highlight: team.highlight, context: { members: team.members, includesYou: team.includesYou, memberCount: team.memberCount, teamLink: teamLink || defaultTeamLink } }; }); return { mentions: mentions, query: query }; } }, { key: "trimTeamARI", value: function trimTeamARI() { var teamId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var TEAM_ARI_PREFIX = 'ari:cloud:teams::team/'; var IDENTITY_TEAM_ARI_PREFIX = 'ari:cloud:identity::team/'; return teamId.replace(TEAM_ARI_PREFIX, '').replace(IDENTITY_TEAM_ARI_PREFIX, ''); } }]); }(MentionResource); export { TeamMentionResource as default };