UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

192 lines (191 loc) 10.6 kB
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 _inherits from "@babel/runtime/helpers/inherits"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _templateObject; 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; })(); } import _regeneratorRuntime from "@babel/runtime/regenerator"; 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 { print } from 'graphql'; import gql from 'graphql-tag'; import { fg } from '@atlaskit/platform-feature-flags'; import { PACKAGE_META_DATA } 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 var modifyResponse = function modifyResponse(response) { var data = _objectSpread({}, response.User); var localWeekdayIndex = new Date().getDay().toString(); if (data.remoteWeekdayIndex && data.remoteWeekdayIndex !== localWeekdayIndex) { data.remoteTimeString = "".concat(data.remoteWeekdayString, " ").concat(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 }; }; var aggUserQuery = gql(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n\tquery user($userId: ID!) {\n\t\tuser(accountId: $userId) {\n\t\t\tid\n\t\t\tname\n\t\t\tpicture\n\t\t\taccountStatus\n\t\t\t__typename\n\t\t\t... on AtlassianAccountUser {\n\t\t\t\temail\n\t\t\t\tnickname\n\t\t\t\tzoneinfo\n\t\t\t\textendedProfile {\n\t\t\t\t\tjobTitle\n\t\t\t\t\torganization\n\t\t\t\t\tlocation\n\t\t\t\t\tclosedDate\n\t\t\t\t\tinactiveDate\n\t\t\t\t}\n\t\t\t}\n\t\t\t... on CustomerUser {\n\t\t\t\temail\n\t\t\t\tzoneinfo\n\t\t\t}\n\t\t\t... on AppUser {\n\t\t\t\tappType\n\t\t\t}\n\t\t}\n\t}\n"]))); var aggUserQueryString = "query user($userId: ID!) {\n\t\tuser(accountId: $userId) {\n\t\t\tid\n\t\t\tname\n\t\t\tpicture\n\t\t\taccountStatus\n\t\t\t__typename\n\t\t\t... on AtlassianAccountUser {\n\t\t\t\temail\n\t\t\t\tnickname\n\t\t\t\tzoneinfo\n\t\t\t\textendedProfile {\n\t\t\t\t\tjobTitle\n\t\t\t\t\torganization\n\t\t\t\t\tlocation\n\t\t\t\t\tclosedDate\n\t\t\t\t\tinactiveDate\n\t\t\t\t}\n\t\t\t}\n\t\t\t... on CustomerUser {\n\t\t\t\temail\n\t\t\t\tzoneinfo\n\t\t\t}\n\t\t\t... on AppUser {\n \t\t\tappType\n \t\t}\n\t\t}\n\t}"; export var buildAggUserQuery = function buildAggUserQuery(userId) { return { query: fg('platform_agg_user_query_doc_change') ? print(aggUserQuery) : aggUserQueryString, variables: { userId: userId } }; }; var queryAGGUser = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url, userId) { var query, _yield$AGGQuery, user; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: query = buildAggUserQuery(userId); _context.next = 3; return AGGQuery(url, query); case 3: _yield$AGGQuery = _context.sent; user = _yield$AGGQuery.user; return _context.abrupt("return", user); case 6: case "end": return _context.stop(); } }, _callee); })); return function queryAGGUser(_x, _x2) { return _ref.apply(this, arguments); }; }(); var UserProfileCardClient = /*#__PURE__*/function (_CachingClient) { function UserProfileCardClient(options) { var _this; _classCallCheck(this, UserProfileCardClient); _this = _callSuper(this, UserProfileCardClient, [options]); _this.options = options; return _this; } _inherits(UserProfileCardClient, _CachingClient); return _createClass(UserProfileCardClient, [{ key: "makeRequest", value: function () { var _makeRequest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_cloudId, userId) { var _user$extendedProfile, _user$extendedProfile2, _user$extendedProfile3, _user$extendedProfile4, _user$extendedProfile5; var gatewayGraphqlUrl, urlWithOperationName, userQueryPromise, user, timestring, localWeekdayIndex, timeFormat; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: gatewayGraphqlUrl = this.options.gatewayGraphqlUrl || '/gateway/api/graphql'; urlWithOperationName = "".concat(gatewayGraphqlUrl, "?operationName=aggUserQuery"); userQueryPromise = queryAGGUser(urlWithOperationName, userId); _context2.next = 5; return userQueryPromise; case 5: user = _context2.sent; localWeekdayIndex = new Date().getDay().toString(); timeFormat = 'h:mmaaa'; if (user.zoneinfo) { if (localTime(user.zoneinfo, 'i') === localWeekdayIndex) { timestring = localTime(user.zoneinfo, timeFormat) || undefined; } else { timestring = localTime(user.zoneinfo, "eee ".concat(timeFormat)) || undefined; } } return _context2.abrupt("return", _objectSpread(_objectSpread({}, user), {}, { isBot: user.__typename === 'AppUser', isAgent: user.appType === 'agent', isServiceAccount: user.__typename === 'AppUser' && user.appType === 'service', 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 })); case 10: case "end": return _context2.stop(); } }, _callee2, this); })); function makeRequest(_x3, _x4) { return _makeRequest.apply(this, arguments); } return makeRequest; }() }, { key: "getProfile", value: function getProfile(cloudId, userId, analytics) { var _this2 = this; if (!userId) { return Promise.reject(new Error('userId missing')); } var cacheIdentifier = "".concat(cloudId, "/").concat(userId); var cache = this.getCachedProfile(cacheIdentifier); if (cache) { return Promise.resolve(cache); } return new Promise(function (resolve, reject) { var startTime = getPageTime(); if (analytics) { analytics('operational.profilecard.triggered.request', _objectSpread({ firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } _this2.makeRequest(cloudId, userId).then(function (data) { if (_this2.cache) { _this2.setCachedProfile(cacheIdentifier, data); } if (analytics) { analytics('operational.profilecard.succeeded.request', _objectSpread({ duration: getPageTime() - startTime, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } resolve(data); }).catch(function (error) { if (analytics) { analytics('operational.profilecard.failed.request', _objectSpread(_objectSpread({ duration: getPageTime() - startTime }, getErrorAttributes(error)), {}, { firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } reject(error); }); }); } }]); }(CachingClient); export { UserProfileCardClient as default };