@atlaskit/profilecard
Version:
A React component to display a card with user information.
198 lines • 8.4 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
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';
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: "profilecard-name-label"
}, props.children);
export const ProfilecardInternal = props => {
const [openTime] = useState(getPageTime());
const {
createAnalyticsEvent
} = props;
const fireAnalytics = useCallback(payload => {
if (createAnalyticsEvent) {
fireEvent(createAnalyticsEvent, payload);
}
}, [createAnalyticsEvent]);
const fireAnalyticsWithDuration = useCallback(generator => {
const elapsed = getPageTime() - openTime;
const event = generator(elapsed);
fireAnalytics(event);
}, [fireAnalytics, openTime]);
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(duration => profileCardRendered('user', 'content', {
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;
}
const 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, ${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
})))));
};
const Actions = ({
actions,
fireAnalyticsWithDuration,
isTriggeredUsingKeyboard
}) => {
const onActionClick = useCallback((action, args, event, index) => {
fireAnalyticsWithDuration(duration => actionClicked('user', {
duration,
hasHref: !!action.link,
hasOnClick: !!action.callback,
index,
actionId: action.id || 'no-id-specified'
}));
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(FocusRing, {
isInset: true,
key: `profile-card-action-focus-ring_${action.id || index}`
}, fg('ptc_migrate_buttons') ? /*#__PURE__*/React.createElement(LinkButton, {
appearance: "default",
key: action.id || index,
onClick: (event, ...args) => onActionClick(action, args, event, index),
href: action.link || '',
autoFocus: index === 0 && isTriggeredUsingKeyboard,
id: `action-button-${action.id}`,
"aria-labelledby": fg('enable_userprofilecard_arialabelfix') ? `action-button-${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: (event, ...args) => 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_${action.id || index}`
}, button);
}
return button;
}), overflowActions && /*#__PURE__*/React.createElement(OverflowProfileCardButtons, {
actions: overflowActions,
fireAnalyticsWithDuration: fireAnalyticsWithDuration,
onItemClick: (action, args, event, index) => onActionClick(action, args, event, index + ACTION_OVERFLOW_THRESHOLD)
}));
};
const LoadingView = ({
fireAnalyticsWithDuration
}) => {
useEffect(() => {
fireAnalyticsWithDuration(duration => profileCardRendered('user', 'spinner', {
duration
}));
}, [fireAnalyticsWithDuration]);
return /*#__PURE__*/React.createElement(SpinnerContainer, {
testId: "profilecard-spinner-container"
}, /*#__PURE__*/React.createElement(Spinner, null));
};
export default withAnalyticsEvents()(ProfilecardInternal);