@atlaskit/global-search
Version:
A cross-product search component (batteries included)
115 lines (100 loc) • 3.67 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import { ResultType, AnalyticsType, ContentType } from '../model/Result';
import { utils } from '@atlaskit/util-service-support';
var PeopleSearchClientImpl = /*#__PURE__*/function () {
function PeopleSearchClientImpl(url, cloudId) {
_classCallCheck(this, PeopleSearchClientImpl);
this.serviceConfig = {
url: url
};
this.cloudId = cloudId;
}
_createClass(PeopleSearchClientImpl, [{
key: "buildRecentQuery",
value: function buildRecentQuery() {
return {
query: "query Collaborators(\n $cloudId: String!,\n $limit: Int) {\n Collaborators(cloudId: $cloudId, limit: $limit) {\n id,\n fullName,\n avatarUrl,\n title,\n nickname,\n department\n }\n }",
variables: {
cloudId: this.cloudId,
limit: 3,
excludeBots: true,
excludeInactive: true
}
};
}
}, {
key: "buildRequestOptions",
value: function buildRequestOptions(body) {
return {
path: 'graphql',
requestInit: {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(body)
}
};
}
}, {
key: "getRecentPeople",
value: function () {
var _getRecentPeople = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var options, response;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = this.buildRequestOptions(this.buildRecentQuery());
_context.next = 3;
return utils.requestService(this.serviceConfig, options);
case 3:
response = _context.sent;
if (!response.errors) {
_context.next = 6;
break;
}
return _context.abrupt("return", []);
case 6:
if (!(!response.data || !response.data.Collaborators)) {
_context.next = 8;
break;
}
return _context.abrupt("return", []);
case 8:
return _context.abrupt("return", response.data.Collaborators.map(function (record) {
return userSearchResultToResult(record, AnalyticsType.RecentPerson);
}));
case 9:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function getRecentPeople() {
return _getRecentPeople.apply(this, arguments);
}
return getRecentPeople;
}()
}]);
return PeopleSearchClientImpl;
}();
export { PeopleSearchClientImpl as default };
function userSearchResultToResult(searchResult, analyticsType) {
var mention = searchResult.nickname || searchResult.fullName;
return {
resultType: ResultType.PersonResult,
resultId: 'people-' + searchResult.id,
name: searchResult.fullName,
href: '/people/' + searchResult.id,
avatarUrl: searchResult.avatarUrl,
contentType: ContentType.Person,
analyticsType: analyticsType,
mentionName: mention,
presenceMessage: searchResult.title
};
}