import { Edit, PersonOutline } from '@mui/icons-material' import clsx from 'clsx' import { forwardRef, useEffect, useState } from 'react' import { toAccessibleImageUrl } from '@dao-dao/utils' export type ProfileImageProps = { imageUrl?: string loading?: boolean size: 'xs' | 'sm' | 'md' | 'lg' | 'header' className?: string fallbackIconClassName?: string onClick?: () => void rounded?: boolean disabled?: boolean } export const ProfileImage = forwardRef( function ProfileImage( { imageUrl, loading, size, className, fallbackIconClassName, onClick, rounded, }, ref ) { const [loadedImage, setLoadedImage] = useState() useEffect(() => { if (!imageUrl) { setLoadedImage(undefined) return } const onLoad = () => setLoadedImage(imageUrl) const image = new Image() image.addEventListener('load', onLoad) image.src = toAccessibleImageUrl(imageUrl) // Clean up. return () => image.removeEventListener('load', onLoad) }, [imageUrl]) const loadingImage = loading || loadedImage !== imageUrl // Size and rounding of container and children. const sizingRoundingClassNames = clsx( { 'h-6 w-6 rounded-full': size === 'xs', 'h-8 w-8 rounded-lg': size === 'sm', 'h-10 w-10 rounded-xl': size === 'md', 'h-16 w-16 rounded-2xl': size === 'lg', 'h-24 w-24 rounded-full': size === 'header', }, rounded && '!rounded-full' ) const showingPlaceholder = !!imageUrl?.startsWith('/placeholders/') return (
{/* Image */}
{/* No image (hides underneath image always) */} {/* Click icon */} {onClick && (
{!loadingImage && ( )}
)}
) } )