index.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use client'
  2. import { useState } from 'react'
  3. import cn from '@/utils/classnames'
  4. type AvatarProps = {
  5. name: string
  6. avatar?: string
  7. size?: number
  8. className?: string
  9. textClassName?: string
  10. }
  11. const Avatar = ({
  12. name,
  13. avatar,
  14. size = 30,
  15. className,
  16. textClassName,
  17. }: AvatarProps) => {
  18. const avatarClassName = 'shrink-0 flex items-center rounded-full bg-primary-600'
  19. const style = { width: `${size}px`, height: `${size}px`, fontSize: `${size}px`, lineHeight: `${size}px` }
  20. const [imgError, setImgError] = useState(false)
  21. const handleError = () => {
  22. setImgError(true)
  23. }
  24. if (avatar && !imgError) {
  25. return (
  26. <img
  27. className={cn(avatarClassName, className)}
  28. style={style}
  29. alt={name}
  30. src={avatar}
  31. onError={handleError}
  32. />
  33. )
  34. }
  35. return (
  36. <div
  37. className={cn(avatarClassName, className)}
  38. style={style}
  39. >
  40. <div
  41. className={cn(textClassName, 'text-center text-white scale-[0.4]')}
  42. style={style}
  43. >
  44. {name[0].toLocaleUpperCase()}
  45. </div>
  46. </div>
  47. )
  48. }
  49. export default Avatar