import { Component, Suspense, forwardRef, type HTMLAttributes, type PropsWithChildren, type ForwardedRef, } from 'react' import { PersonIcon } from '@pluralsight/icons' import { getAvatarProps, getAvatarImageProps, getAvatarLabelProps, getAvatarIconOptions, getIconProps, splitClassNameProp, } from '@pluralsight/headless-styles' import type { AvatarOptions, AvatarImageOptions, AvatarLabelOptions, } from '@pluralsight/headless-styles/types' import { usePreloadedImg } from '@pluralsight/react-utils' interface AvatarContainerProps extends AvatarOptions, HTMLAttributes {} function AvatarContainer(props: AvatarContainerProps) { const { size, sentiment, ...nativeProps } = props const container = getAvatarProps({ classNames: splitClassNameProp(nativeProps.className), sentiment, size, }) return } interface AvatarLabelProps extends AvatarLabelOptions, HTMLAttributes {} function AvatarLabel(props: AvatarLabelProps) { const { name, size, ...nativeProps } = props const { value, ...label } = getAvatarLabelProps({ classNames: splitClassNameProp(props.className), name, size, }) return ( {value} ) } interface FallbackAvatarProps extends AvatarOptions { name?: AvatarLabelProps['name'] } function FallbackAvatar(props: FallbackAvatarProps) { const { size = 'm', sentiment = 'default', name, ...nativeProps } = props return ( {name ? ( ) : ( )} ) } interface AvatarErrorBoundaryState { hasError: boolean } interface AvatarErrorBoundaryProps extends AvatarOptions { name?: AvatarLabelProps['name'] } class AvatarErrorBoundary extends Component< PropsWithChildren, AvatarErrorBoundaryState > { constructor(props: AvatarOptions) { super(props) this.state = { hasError: false } } static getDerivedStateFromError() { // Update state so the next render will show the fallback UI. return { hasError: true } } render() { if (this.state.hasError) { return } return this.props.children } } interface ImageProps extends HTMLAttributes, AvatarImageOptions { imgData: { read: () => HTMLAttributes } name: AvatarLabelOptions['name'] ref?: ForwardedRef } function ImageEl(props: ImageProps, ref: ForwardedRef) { const { imgData, name, src, ...nativeProps } = props const avatarImg = getAvatarImageProps({ alt: name, classNames: splitClassNameProp(props.className), src, }) const img = imgData.read() return ( {avatarImg.alt} ) } const Image = forwardRef(ImageEl) // interface AvatarProps extends AvatarOptions, HTMLAttributes { name?: AvatarLabelOptions['name'] src?: AvatarImageOptions['src'] } function AvatarEl(props: AvatarProps, ref: ForwardedRef) { const { name, src, ...altProps } = props const resource = usePreloadedImg({ alt: name, src, }) if (!resource) { return } return ( }> {name ) } // public exports export const Avatar = forwardRef(AvatarEl)