UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

220 lines 10.5 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; 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 React, { useCallback, useEffect, useMemo, useState } from 'react'; import { FormattedMessage, useIntl } from 'react-intl-next'; import Avatar from '@atlaskit/avatar'; import { LinkButton } from '@atlaskit/button/new'; import { fg } from '@atlaskit/platform-feature-flags'; import Spinner from '@atlaskit/spinner'; import { useAnalyticsEvents } from '@atlaskit/teams-app-internal-analytics'; import { N0 } from '@atlaskit/theme/colors'; import messages from '../../messages'; import { ActionButtonGroup, ActionsFlexSpacer, AnimatedKudosButton, AnimationWrapper, CardContainer, CardContent, KudosBlobAnimation, ProfileImage } from '../../styled/Card'; import { CardWrapper, SpinnerContainer } from '../../styled/UserTrigger'; import { PACKAGE_META_DATA } from '../../util/analytics'; import { isBasicClick } from '../../util/click'; import { getPageTime } from '../../util/performance'; import { ErrorMessage } from '../Error'; import { ACTION_OVERFLOW_THRESHOLD, OverflowProfileCardButtons } from './OverflowProfileCardButtons'; import { ProfileCardDetails } from './ProfileCardDetails'; var GIVE_KUDOS_ACTION_ID = 'give-kudos'; var useKudos = function useKudos(cloudId, userId, teamCentralBaseUrl, openKudosDrawer) { var kudosUrl = useMemo(function () { var recipientId = userId ? "&recipientId=".concat(userId) : ''; var cloudIdParam = cloudId ? "&cloudId=".concat(cloudId) : ''; return "".concat(teamCentralBaseUrl || '', "/kudos/give?type=individual").concat(recipientId).concat(cloudIdParam); }, [cloudId, teamCentralBaseUrl, userId]); var kudosButtonCallback = useCallback(function () { if (openKudosDrawer) { openKudosDrawer(); } else { window.open(kudosUrl); } }, [kudosUrl, openKudosDrawer]); var kudosAction = useMemo(function () { return { label: /*#__PURE__*/React.createElement(FormattedMessage, messages.giveKudosButton), id: GIVE_KUDOS_ACTION_ID, callback: kudosButtonCallback, link: kudosUrl }; }, [kudosButtonCallback, kudosUrl]); return { kudosAction: kudosAction, kudosButtonCallback: kudosButtonCallback, kudosUrl: kudosUrl }; }; var Wrapper = function Wrapper(props) { return /*#__PURE__*/React.createElement(CardWrapper, { testId: "profilecard", role: "dialog", labelledBy: props.labelledBy, ariaLabel: props.ariaLabel }, props.children); }; export var ProfilecardInternal = function ProfilecardInternal(props) { var _useState = useState(getPageTime()), _useState2 = _slicedToArray(_useState, 1), openTime = _useState2[0]; var intl = useIntl(); var _useAnalyticsEvents = useAnalyticsEvents(), fireEvent = _useAnalyticsEvents.fireEvent; var fireAnalyticsWithDuration = useCallback(function (eventKey, generator) { var duration = getPageTime() - openTime; var attributes = generator(duration); fireEvent(eventKey, attributes); }, [openTime, fireEvent]); var _useKudos = useKudos(props.cloudId, props.userId, props.teamCentralBaseUrl, props.openKudosDrawer), kudosAction = _useKudos.kudosAction; var _props$actions = props.actions, actions = _props$actions === void 0 ? [] : _props$actions, isCurrentUser = props.isCurrentUser, isKudosEnabled = props.isKudosEnabled, _props$status = props.status, status = _props$status === void 0 ? 'active' : _props$status; var realActions = useMemo(function () { if (isCurrentUser || !isKudosEnabled || status !== 'active') { return actions; } return actions.concat([kudosAction]); }, [actions, isCurrentUser, isKudosEnabled, kudosAction, status]); var isLoading = props.isLoading, fullName = props.fullName, hasError = props.hasError; var canRender = !hasError && !isLoading && !!(fullName || status === 'closed'); useEffect(function () { if (canRender) { fireAnalyticsWithDuration('ui.profilecard.rendered.content', function (duration) { return _objectSpread({ duration: duration, numActions: realActions.length, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA); }); } }, [canRender, fireAnalyticsWithDuration, realActions]); if (hasError) { return /*#__PURE__*/React.createElement(Wrapper, { ariaLabel: intl.formatMessage(messages.errorDialogLabel) }, /*#__PURE__*/React.createElement(ErrorMessage, { reload: props.clientFetchProfile, errorType: props.errorType || null, fireAnalytics: fireEvent })); } if (isLoading) { return /*#__PURE__*/React.createElement(Wrapper, { ariaLabel: intl.formatMessage(messages.loadingDialogLabel) }, /*#__PURE__*/React.createElement(LoadingView, { fireAnalyticsWithDuration: fireAnalyticsWithDuration })); } if (!canRender) { return null; } var isDisabledUser = status === 'inactive' || status === 'closed'; return /*#__PURE__*/React.createElement(Wrapper, { labelledBy: "profilecard-name-label" }, /*#__PURE__*/React.createElement(CardContainer, { isDisabledUser: isDisabledUser, withoutElevation: props.withoutElevation }, /*#__PURE__*/React.createElement(ProfileImage, null, /*#__PURE__*/React.createElement(Avatar, { size: "xlarge", src: status !== 'closed' ? props.avatarUrl : undefined, borderColor: "var(--ds-shadow-overlay, ".concat(N0, ")") })), /*#__PURE__*/React.createElement(CardContent, null, /*#__PURE__*/React.createElement(ProfileCardDetails, _extends({}, props, { status: status, fireAnalyticsWithDuration: fireAnalyticsWithDuration })), realActions && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ActionsFlexSpacer, null), /*#__PURE__*/React.createElement(Actions, _extends({}, fg('jfp_a11y_team_profile_card_actions_label') && { fullName: fullName }, { fullName: fullName, actions: realActions, fireAnalyticsWithDuration: fireAnalyticsWithDuration, isTriggeredUsingKeyboard: props.isTriggeredUsingKeyboard, isRenderedInPortal: props.isRenderedInPortal })))))); }; var Actions = function Actions(_ref) { var actions = _ref.actions, fireAnalyticsWithDuration = _ref.fireAnalyticsWithDuration, isTriggeredUsingKeyboard = _ref.isTriggeredUsingKeyboard, isRenderedInPortal = _ref.isRenderedInPortal, fullName = _ref.fullName; var onActionClick = useCallback(function (action, args, event, index) { fireAnalyticsWithDuration('ui.profilecard.clicked.action', function (duration) { return _objectSpread({ method: 'click', firedAt: Math.round(getPageTime()), duration: duration, hasHref: !!action.link, hasOnClick: !!action.callback, index: index, actionId: action.id || 'no-id-specified' }, PACKAGE_META_DATA); }); if (action.callback && isBasicClick(event)) { event.preventDefault(); action.callback.apply(action, [event].concat(_toConsumableArray(args))); } }, [fireAnalyticsWithDuration]); if (!actions || actions.length === 0) { return null; } var regularActions = actions.slice(0, ACTION_OVERFLOW_THRESHOLD); var overflowActions = actions.length > ACTION_OVERFLOW_THRESHOLD ? actions.slice(ACTION_OVERFLOW_THRESHOLD) : undefined; return /*#__PURE__*/React.createElement(ActionButtonGroup, { testId: "profilecard-actions" }, regularActions.map(function (action, index) { var isKudos = action.id === GIVE_KUDOS_ACTION_ID; var button = /*#__PURE__*/React.createElement(LinkButton, { appearance: "default", key: action.id || index, onClick: function onClick(event) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return onActionClick(action, args, event, index); }, href: action.link || '', target: action.target, autoFocus: index === 0 && isTriggeredUsingKeyboard && !isRenderedInPortal, id: "action-button-".concat(action.id), "aria-labelledby": "action-button-".concat(action.id, " profilecard-name-label") }, action.label, isKudos && /*#__PURE__*/React.createElement(AnimationWrapper, null, /*#__PURE__*/React.createElement(KudosBlobAnimation, null))); if (isKudos) { return /*#__PURE__*/React.createElement(AnimatedKudosButton, { key: "profile-card-action-kudos_".concat(action.id || index) }, button); } return button; }), overflowActions && /*#__PURE__*/React.createElement(OverflowProfileCardButtons, _extends({ actions: overflowActions, fireAnalyticsWithDuration: fireAnalyticsWithDuration, onItemClick: function onItemClick(action, args, event, index) { return onActionClick(action, args, event, index + ACTION_OVERFLOW_THRESHOLD); } }, fg('jfp_a11y_team_profile_card_actions_label') && { fullName: fullName }))); }; export var LoadingView = function LoadingView(_ref2) { var fireAnalyticsWithDuration = _ref2.fireAnalyticsWithDuration; useEffect(function () { fireAnalyticsWithDuration('ui.profilecard.rendered.spinner', function (duration) { return _objectSpread({ firedAt: Math.round(getPageTime()), duration: duration }, PACKAGE_META_DATA); }); }, [fireAnalyticsWithDuration]); return /*#__PURE__*/React.createElement(SpinnerContainer, { testId: "profilecard-spinner-container" }, /*#__PURE__*/React.createElement(Spinner, null)); }; export default ProfilecardInternal;