UNPKG

@atlaskit/global-search

Version:

A cross-product search component (batteries included)

78 lines (70 loc) 1.96 kB
import { ResultType, AnalyticsType, ContentType } from '../model/Result'; import { utils } from '@atlaskit/util-service-support'; export default class PeopleSearchClientImpl { constructor(url, cloudId) { this.serviceConfig = { url: url }; this.cloudId = cloudId; } buildRecentQuery() { return { query: `query Collaborators( $cloudId: String!, $limit: Int) { Collaborators(cloudId: $cloudId, limit: $limit) { id, fullName, avatarUrl, title, nickname, department } }`, variables: { cloudId: this.cloudId, limit: 3, excludeBots: true, excludeInactive: true } }; } buildRequestOptions(body) { return { path: 'graphql', requestInit: { headers: { 'Content-Type': 'application/json' }, method: 'POST', body: JSON.stringify(body) } }; } async getRecentPeople() { const options = this.buildRequestOptions(this.buildRecentQuery()); const response = await utils.requestService(this.serviceConfig, options); if (response.errors) { // TODO should probably catch and log this return []; } if (!response.data || !response.data.Collaborators) { return []; } return response.data.Collaborators.map(record => userSearchResultToResult(record, AnalyticsType.RecentPerson)); } } function userSearchResultToResult(searchResult, analyticsType) { const 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, mentionName: mention, presenceMessage: searchResult.title }; }