@atlaskit/profilecard
Version:
A React component to display a card with user information.
108 lines • 5 kB
JavaScript
import React, { Suspense, useCallback, useEffect, useMemo, useState } from 'react';
import { fg } from '@atlaskit/platform-feature-flags';
import { getAgentCreator } from '@atlaskit/rovo-agent-components/ui/AgentProfileInfo';
import { navigateToTeamsApp } from '@atlaskit/teams-app-config/navigation';
import { useAnalyticsEvents as useAnalyticsEventsNext } from '@atlaskit/teams-app-internal-analytics';
import { getAAIDFromARI } from '../../util/rovoAgentUtils';
import ErrorMessage from '../Error/ErrorMessage';
import { AgentProfileCardWrapper } from './AgentProfileCardWrapper';
import { AgentProfileCardLazy } from './lazyAgentProfileCard';
export const AgentProfileCardResourced = props => {
const [agentData, setAgentData] = useState();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState();
const {
fireEvent
} = useAnalyticsEventsNext();
const creatorUserId = useMemo(() => (agentData === null || agentData === void 0 ? void 0 : agentData.creator_type) === 'CUSTOMER' && agentData.creator ? getAAIDFromARI(agentData.creator) : '', [agentData === null || agentData === void 0 ? void 0 : agentData.creator_type, agentData === null || agentData === void 0 ? void 0 : agentData.creator]);
const {
href: profileHref
} = navigateToTeamsApp({
type: 'USER',
payload: {
userId: creatorUserId || ''
},
cloudId: props.cloudId
});
const getCreator = useCallback(async ({
creator_type,
creator,
authoringTeam
}) => {
try {
var _authoringTeam$displa, _authoringTeam$profil, _userCreatorInfo$full;
let userCreatorInfo;
if (creatorUserId && props.cloudId) {
userCreatorInfo = await props.resourceClient.getProfile(props.cloudId, creatorUserId, fireEvent);
}
const creatorInfo = getAgentCreator({
creatorType: creator_type !== null && creator_type !== void 0 ? creator_type : '',
authoringTeam: authoringTeam ? {
displayName: (_authoringTeam$displa = authoringTeam.displayName) !== null && _authoringTeam$displa !== void 0 ? _authoringTeam$displa : '',
profileLink: (_authoringTeam$profil = authoringTeam.profileUrl) !== null && _authoringTeam$profil !== void 0 ? _authoringTeam$profil : undefined
} : undefined,
userCreator: userCreatorInfo ? {
name: (_userCreatorInfo$full = userCreatorInfo.fullName) !== null && _userCreatorInfo$full !== void 0 ? _userCreatorInfo$full : '',
profileLink: fg('platform-adopt-teams-nav-config') ? profileHref : `/people/${creatorUserId}`
} : undefined,
forgeCreator: creator !== null && creator !== void 0 ? creator : undefined
});
return creatorInfo;
} catch {
return undefined;
}
}, [creatorUserId, fireEvent, props.cloudId, props.resourceClient, profileHref]);
const fetchData = useCallback(async () => {
setIsLoading(true);
try {
var _profileResult$aggDat, _profileResult$aggDat2;
const profileResult = await props.resourceClient.getRovoAgentProfile({
type: 'identity',
value: props.accountId
}, fireEvent);
const profileData = profileResult.restData;
const creatorInfoProps = {
creator_type: profileData === null || profileData === void 0 ? void 0 : profileData.creator_type,
creator: (profileData === null || profileData === void 0 ? void 0 : profileData.creator) || undefined,
authoringTeam: (_profileResult$aggDat = (_profileResult$aggDat2 = profileResult.aggData) === null || _profileResult$aggDat2 === void 0 ? void 0 : _profileResult$aggDat2.authoringTeam) !== null && _profileResult$aggDat !== void 0 ? _profileResult$aggDat : undefined
};
const agentCreatorInfo = await getCreator(creatorInfoProps);
setAgentData({
...profileData,
creatorInfo: agentCreatorInfo
});
} catch (err) {
setError(err);
} finally {
setIsLoading(false);
}
}, [fireEvent, getCreator, props.accountId, props.resourceClient]);
useEffect(() => {
fetchData();
}, [fetchData]);
if (error || !isLoading && !agentData) {
return /*#__PURE__*/React.createElement(AgentProfileCardWrapper, null, /*#__PURE__*/React.createElement(ErrorMessage, {
reload: () => {
fetchData();
},
errorType: error || null,
fireAnalytics: fireEvent
}));
}
return /*#__PURE__*/React.createElement(Suspense, {
fallback: null
}, /*#__PURE__*/React.createElement(AgentProfileCardLazy, {
agent: agentData,
isLoading: isLoading,
hasError: !!error,
onConversationStartersClick: props.onConversationStartersClick,
onChatClick: props.onChatClick,
addFlag: props.addFlag,
resourceClient: props.resourceClient,
cloudId: props.cloudId,
onDeleteAgent: props.onDeleteAgent,
hideMoreActions: props.hideMoreActions,
hideAiDisclaimer: props.hideAiDisclaimer,
hideConversationStarters: props.hideConversationStarters
}));
};