import { Person, Briefcase as BriefcaseIcon } from '@transferwise/icons'; import { useState } from 'react'; import Avatar, { AvatarProps, AvatarType } from '../Avatar'; import Badge, { BadgeProps } from '../Badge'; import { ProfileType, ProfileTypePersonal, ProfileTypeBusiness, Size, Sentiment, getInitials, Status, } from '../../common'; import StatusIcon from '../../StatusIcon/StatusIcon'; interface OptionalBadgeProps extends Omit { url?: string; ariaLabel?: string; altText?: string; statusIcon?: Sentiment | Status.PENDING; } const OptionalBadge = ({ url, altText, statusIcon, children, ariaLabel, ...rest }: OptionalBadgeProps) => { if (url) { return ( } {...rest}> {children} ); } if (statusIcon) { return ( } {...rest} > {children} ); } return <>{children}; }; /** * @deprecated Use AvatarView and associated typings */ export type AvatarWrapperProps = { url?: string; 'aria-label'?: string; profileType?: ProfileTypeBusiness | ProfileTypePersonal; profileId?: string; name?: string; avatarProps?: AvatarProps; badgeProps?: BadgeProps; } & ( | { badgeUrl: string; badgeAltText: string; badgeStatusIcon?: never; } | { badgeUrl?: never; badgeAltText?: never; badgeStatusIcon: Sentiment | Status.PENDING; } | { badgeUrl?: never; badgeAltText?: never; badgeStatusIcon?: never; } ); /** * @deprecated Use `AvatarView` component instead */ const AvatarWrapper = ({ url, 'aria-label': ariaLabel, profileType, profileId, badgeUrl, badgeAltText, badgeStatusIcon, name, avatarProps, badgeProps, }: AvatarWrapperProps) => { const [errorUrl, setErrorUrl] = useState(undefined); const hasImageLoadError = errorUrl === url; const isBusinessProfile = profileType === ProfileType.BUSINESS; const getAvatarProps = () => { let updatedAvatarProps = avatarProps; if (!badgeUrl && !badgeStatusIcon && ariaLabel) { updatedAvatarProps = { ...updatedAvatarProps, 'aria-label': ariaLabel }; } if (url && !hasImageLoadError) { return { type: AvatarType.THUMBNAIL, children: setErrorUrl(url)} />, ...updatedAvatarProps, }; } if (profileType) { return { type: AvatarType.ICON, children: isBusinessProfile ? : , ...updatedAvatarProps, }; } if (name) { return { type: AvatarType.INITIALS, children: <>{getInitials(name)}, backgroundColorSeed: profileId?.toString(), ...updatedAvatarProps, }; } return { type: AvatarType.ICON, children: , ...updatedAvatarProps, }; }; return ( ); }; export default AvatarWrapper;