UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

207 lines 8.34 kB
import _extends from "@babel/runtime/helpers/extends"; 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'; const GIVE_KUDOS_ACTION_ID = 'give-kudos'; const useKudos = (cloudId, userId, teamCentralBaseUrl, openKudosDrawer) => { const kudosUrl = useMemo(() => { const recipientId = userId ? `&recipientId=${userId}` : ''; const cloudIdParam = cloudId ? `&cloudId=${cloudId}` : ''; return `${teamCentralBaseUrl || ''}/kudos/give?type=individual${recipientId}${cloudIdParam}`; }, [cloudId, teamCentralBaseUrl, userId]); const kudosButtonCallback = useCallback(() => { if (openKudosDrawer) { openKudosDrawer(); } else { window.open(kudosUrl); } }, [kudosUrl, openKudosDrawer]); const kudosAction = useMemo(() => { return { label: /*#__PURE__*/React.createElement(FormattedMessage, messages.giveKudosButton), id: GIVE_KUDOS_ACTION_ID, callback: kudosButtonCallback, link: kudosUrl }; }, [kudosButtonCallback, kudosUrl]); return { kudosAction, kudosButtonCallback, kudosUrl }; }; const Wrapper = props => /*#__PURE__*/React.createElement(CardWrapper, { testId: "profilecard", role: "dialog", labelledBy: props.labelledBy, ariaLabel: props.ariaLabel }, props.children); export const ProfilecardInternal = props => { const [openTime] = useState(getPageTime()); const intl = useIntl(); const { fireEvent } = useAnalyticsEvents(); const fireAnalyticsWithDuration = useCallback((eventKey, generator) => { const duration = getPageTime() - openTime; const attributes = generator(duration); fireEvent(eventKey, attributes); }, [openTime, fireEvent]); const { kudosAction } = useKudos(props.cloudId, props.userId, props.teamCentralBaseUrl, props.openKudosDrawer); const { actions = [], isCurrentUser, isKudosEnabled, status = 'active' } = props; const realActions = useMemo(() => { if (isCurrentUser || !isKudosEnabled || status !== 'active') { return actions; } return actions.concat([kudosAction]); }, [actions, isCurrentUser, isKudosEnabled, kudosAction, status]); const { isLoading, fullName, hasError } = props; const canRender = !hasError && !isLoading && !!(fullName || status === 'closed'); useEffect(() => { if (canRender) { fireAnalyticsWithDuration('ui.profilecard.rendered.content', 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; } const 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, ${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, actions: realActions, fireAnalyticsWithDuration: fireAnalyticsWithDuration, isTriggeredUsingKeyboard: props.isTriggeredUsingKeyboard, isRenderedInPortal: props.isRenderedInPortal })))))); }; const Actions = ({ actions, fireAnalyticsWithDuration, isTriggeredUsingKeyboard, isRenderedInPortal, fullName }) => { const onActionClick = useCallback((action, args, event, index) => { fireAnalyticsWithDuration('ui.profilecard.clicked.action', duration => ({ method: 'click', firedAt: Math.round(getPageTime()), duration, hasHref: !!action.link, hasOnClick: !!action.callback, index, actionId: action.id || 'no-id-specified', ...PACKAGE_META_DATA })); if (action.callback && isBasicClick(event)) { event.preventDefault(); action.callback(event, ...args); } }, [fireAnalyticsWithDuration]); if (!actions || actions.length === 0) { return null; } const regularActions = actions.slice(0, ACTION_OVERFLOW_THRESHOLD); const overflowActions = actions.length > ACTION_OVERFLOW_THRESHOLD ? actions.slice(ACTION_OVERFLOW_THRESHOLD) : undefined; return /*#__PURE__*/React.createElement(ActionButtonGroup, { testId: "profilecard-actions" }, regularActions.map((action, index) => { const isKudos = action.id === GIVE_KUDOS_ACTION_ID; const button = /*#__PURE__*/React.createElement(LinkButton, { appearance: "default", key: action.id || index, onClick: (event, ...args) => onActionClick(action, args, event, index), href: action.link || '', target: action.target, autoFocus: index === 0 && isTriggeredUsingKeyboard && !isRenderedInPortal, id: `action-button-${action.id}`, "aria-labelledby": `action-button-${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_${action.id || index}` }, button); } return button; }), overflowActions && /*#__PURE__*/React.createElement(OverflowProfileCardButtons, _extends({ actions: overflowActions, fireAnalyticsWithDuration: fireAnalyticsWithDuration, onItemClick: (action, args, event, index) => onActionClick(action, args, event, index + ACTION_OVERFLOW_THRESHOLD) }, fg('jfp_a11y_team_profile_card_actions_label') && { fullName }))); }; export const LoadingView = ({ fireAnalyticsWithDuration }) => { useEffect(() => { fireAnalyticsWithDuration('ui.profilecard.rendered.spinner', duration => ({ firedAt: Math.round(getPageTime()), duration, ...PACKAGE_META_DATA })); }, [fireAnalyticsWithDuration]); return /*#__PURE__*/React.createElement(SpinnerContainer, { testId: "profilecard-spinner-container" }, /*#__PURE__*/React.createElement(Spinner, null)); }; export default ProfilecardInternal;