import { Person as ProfileIcon, Briefcase as BriefcaseIcon } from '@transferwise/icons'; import { useState, useEffect } 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}; }; 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 [hasImageLoadError, setImageLoadError] = useState(false); const isBusinessProfile = profileType === ProfileType.BUSINESS; // Reset the errored state when url changes useEffect(() => setImageLoadError(false), [url]); const getAvatarProps = () => { let updatedAvatarProps = avatarProps; if (!badgeUrl && !badgeStatusIcon && ariaLabel) { updatedAvatarProps = { ...updatedAvatarProps, 'aria-label': ariaLabel }; } if (url && !hasImageLoadError) { return { type: AvatarType.THUMBNAIL, children: setImageLoadError(true)} />, ...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;