UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

215 lines 9.88 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _extends from "@babel/runtime/helpers/extends"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { FormattedMessage } from 'react-intl-next'; import { withAnalyticsEvents } from '@atlaskit/analytics-next'; import Avatar from '@atlaskit/avatar'; import ButtonLegacy from '@atlaskit/button'; import { LinkButton } from '@atlaskit/button/new'; import FocusRing from '@atlaskit/focus-ring'; import { fg } from '@atlaskit/platform-feature-flags'; import Spinner from '@atlaskit/spinner'; import { N0 } from '@atlaskit/theme/colors'; import messages from '../../messages'; import { ActionButtonGroup, ActionsFlexSpacer, AnimatedKudosButton, AnimationWrapper, CardContainer, CardContent, CardWrapper, KudosBlobAnimation, ProfileImage, SpinnerContainer } from '../../styled/Card'; import { actionClicked, fireEvent, profileCardRendered } 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: "profilecard-name-label" }, props.children); }; export var ProfilecardInternal = function ProfilecardInternal(props) { var _useState = useState(getPageTime()), _useState2 = _slicedToArray(_useState, 1), openTime = _useState2[0]; var createAnalyticsEvent = props.createAnalyticsEvent; var fireAnalytics = useCallback(function (payload) { if (createAnalyticsEvent) { fireEvent(createAnalyticsEvent, payload); } }, [createAnalyticsEvent]); var fireAnalyticsWithDuration = useCallback(function (generator) { var elapsed = getPageTime() - openTime; var event = generator(elapsed); fireAnalytics(event); }, [fireAnalytics, openTime]); 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(function (duration) { return profileCardRendered('user', 'content', { duration: duration, numActions: realActions.length }); }); } }, [canRender, fireAnalyticsWithDuration, realActions]); if (hasError) { return /*#__PURE__*/React.createElement(Wrapper, null, /*#__PURE__*/React.createElement(ErrorMessage, { reload: props.clientFetchProfile, errorType: props.errorType || null, fireAnalytics: fireAnalytics })); } if (isLoading) { return /*#__PURE__*/React.createElement(Wrapper, null, /*#__PURE__*/React.createElement(LoadingView, { fireAnalyticsWithDuration: fireAnalyticsWithDuration })); } if (!canRender) { return null; } var isDisabledUser = status === 'inactive' || status === 'closed'; return /*#__PURE__*/React.createElement(Wrapper, null, /*#__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, { actions: realActions, fireAnalyticsWithDuration: fireAnalyticsWithDuration, isTriggeredUsingKeyboard: props.isTriggeredUsingKeyboard }))))); }; var Actions = function Actions(_ref) { var actions = _ref.actions, fireAnalyticsWithDuration = _ref.fireAnalyticsWithDuration, isTriggeredUsingKeyboard = _ref.isTriggeredUsingKeyboard; var onActionClick = useCallback(function (action, args, event, index) { fireAnalyticsWithDuration(function (duration) { return actionClicked('user', { duration: duration, hasHref: !!action.link, hasOnClick: !!action.callback, index: index, actionId: action.id || 'no-id-specified' }); }); 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(FocusRing, { isInset: true, key: "profile-card-action-focus-ring_".concat(action.id || index) }, fg('ptc_migrate_buttons') ? /*#__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 || '', autoFocus: index === 0 && isTriggeredUsingKeyboard, id: "action-button-".concat(action.id), "aria-labelledby": fg('enable_userprofilecard_arialabelfix') ? "action-button-".concat(action.id, " profilecard-name-label") : '' }, action.label, isKudos && /*#__PURE__*/React.createElement(AnimationWrapper, null, /*#__PURE__*/React.createElement(KudosBlobAnimation, null))) : /*#__PURE__*/React.createElement(ButtonLegacy, { appearance: "default", key: action.id || index, onClick: function onClick(event) { for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } return onActionClick(action, args, event, index); }, href: action.link, autoFocus: index === 0 && isTriggeredUsingKeyboard }, 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, { actions: overflowActions, fireAnalyticsWithDuration: fireAnalyticsWithDuration, onItemClick: function onItemClick(action, args, event, index) { return onActionClick(action, args, event, index + ACTION_OVERFLOW_THRESHOLD); } })); }; var LoadingView = function LoadingView(_ref2) { var fireAnalyticsWithDuration = _ref2.fireAnalyticsWithDuration; useEffect(function () { fireAnalyticsWithDuration(function (duration) { return profileCardRendered('user', 'spinner', { duration: duration }); }); }, [fireAnalyticsWithDuration]); return /*#__PURE__*/React.createElement(SpinnerContainer, { testId: "profilecard-spinner-container" }, /*#__PURE__*/React.createElement(Spinner, null)); }; export default withAnalyticsEvents()(ProfilecardInternal);