UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

185 lines (184 loc) 5.78 kB
import { print } from 'graphql'; import gql from 'graphql-tag'; import { fg } from '@atlaskit/platform-feature-flags'; import { userRequestAnalytics } from '../util/analytics'; import { localTime } from '../util/date'; import { getPageTime } from '../util/performance'; import CachingClient from './CachingClient'; import { getErrorAttributes } from './errorUtils'; import { AGGQuery } from './graphqlUtils'; /** * Transform response from GraphQL * - Prefix `timestring` with `remoteWeekdayString` depending on `remoteWeekdayIndex` * - Remove properties which will be not used later * @ignore * @param {object} response * @return {object} */ export const modifyResponse = response => { const data = { ...response.User }; const localWeekdayIndex = new Date().getDay().toString(); if (data.remoteWeekdayIndex && data.remoteWeekdayIndex !== localWeekdayIndex) { data.remoteTimeString = `${data.remoteWeekdayString} ${data.remoteTimeString}`; } return { isBot: data.isBot, isCurrentUser: data.isCurrentUser, status: data.status, statusModifiedDate: data.statusModifiedDate || undefined, avatarUrl: data.avatarUrl || undefined, email: data.email || undefined, fullName: data.fullName || undefined, location: data.location || undefined, meta: data.meta || undefined, nickname: data.nickname || undefined, companyName: data.companyName || undefined, timestring: data.remoteTimeString || undefined, accountType: data.accountType || undefined }; }; const aggUserQuery = gql` query user($userId: ID!) { user(accountId: $userId) { id name picture accountStatus __typename ... on AtlassianAccountUser { email nickname zoneinfo extendedProfile { jobTitle organization location closedDate inactiveDate } } ... on CustomerUser { email zoneinfo } ... on AppUser { appType } } } `; const aggUserQueryString = `query user($userId: ID!) { user(accountId: $userId) { id name picture accountStatus __typename ... on AtlassianAccountUser { email nickname zoneinfo extendedProfile { jobTitle organization location closedDate inactiveDate } } ... on CustomerUser { email zoneinfo } ... on AppUser { appType } } }`; export const buildAggUserQuery = userId => ({ query: fg('platform_agg_user_query_doc_change') ? print(aggUserQuery) : aggUserQueryString, variables: { userId } }); const queryAGGUser = async (url, userId) => { const query = buildAggUserQuery(userId); const { user } = await AGGQuery(url, query); return user; }; export default class UserProfileCardClient extends CachingClient { constructor(options) { super(options); this.options = options; } async makeRequest(cloudId, userId) { var _user$extendedProfile, _user$extendedProfile2, _user$extendedProfile3, _user$extendedProfile4, _user$extendedProfile5; const gatewayGraphqlUrl = this.options.gatewayGraphqlUrl || '/gateway/api/graphql'; const urlWithOperationName = `${gatewayGraphqlUrl}?operationName=aggUserQuery`; const userQueryPromise = queryAGGUser(urlWithOperationName, userId); const user = await userQueryPromise; let timestring; const localWeekdayIndex = new Date().getDay().toString(); if (user.zoneinfo) { if (localTime(user.zoneinfo, 'i') === localWeekdayIndex) { timestring = localTime(user.zoneinfo, 'h:mmbbb') || undefined; } else { timestring = localTime(user.zoneinfo, 'eee h:mmbbb') || undefined; } } return { ...user, isBot: user.__typename === 'AppUser', isAgent: user.appType === 'agent', status: user.accountStatus, statusModifiedDate: ((_user$extendedProfile = user.extendedProfile) === null || _user$extendedProfile === void 0 ? void 0 : _user$extendedProfile.closedDate) || ((_user$extendedProfile2 = user.extendedProfile) === null || _user$extendedProfile2 === void 0 ? void 0 : _user$extendedProfile2.inactiveDate), avatarUrl: user.picture, email: user.email, fullName: user.name, location: (_user$extendedProfile3 = user.extendedProfile) === null || _user$extendedProfile3 === void 0 ? void 0 : _user$extendedProfile3.location, meta: (_user$extendedProfile4 = user.extendedProfile) === null || _user$extendedProfile4 === void 0 ? void 0 : _user$extendedProfile4.jobTitle, nickname: user.nickname, companyName: (_user$extendedProfile5 = user.extendedProfile) === null || _user$extendedProfile5 === void 0 ? void 0 : _user$extendedProfile5.organization, timestring: timestring }; } getProfile(cloudId, userId, analytics) { if (!userId) { return Promise.reject(new Error('userId missing')); } const cacheIdentifier = `${cloudId}/${userId}`; const cache = this.getCachedProfile(cacheIdentifier); if (cache) { return Promise.resolve(cache); } return new Promise((resolve, reject) => { const startTime = getPageTime(); if (analytics) { analytics(userRequestAnalytics('triggered')); } this.makeRequest(cloudId, userId).then(data => { if (this.cache) { this.setCachedProfile(cacheIdentifier, data); } if (analytics) { analytics(userRequestAnalytics('succeeded', { duration: getPageTime() - startTime })); } resolve(data); }).catch(error => { if (analytics) { analytics(userRequestAnalytics('failed', { duration: getPageTime() - startTime, ...getErrorAttributes(error) })); } reject(error); }); }); } }