mirror of
				https://github.com/langgenius/dify.git
				synced 2025-10-31 19:03:09 +00:00 
			
		
		
		
	 7709d9df20
			
		
	
	
		7709d9df20
		
			
		
	
	
	
	
		
			
			Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: jZonG <jzongcode@gmail.com>
		
			
				
	
	
		
			55 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 'use client'
 | |
| import { useState } from 'react'
 | |
| import cn from '@/utils/classnames'
 | |
| 
 | |
| export type AvatarProps = {
 | |
|   name: string
 | |
|   avatar: string | null
 | |
|   size?: number
 | |
|   className?: string
 | |
|   textClassName?: string
 | |
| }
 | |
| const Avatar = ({
 | |
|   name,
 | |
|   avatar,
 | |
|   size = 30,
 | |
|   className,
 | |
|   textClassName,
 | |
| }: AvatarProps) => {
 | |
|   const avatarClassName = 'shrink-0 flex items-center rounded-full bg-primary-600'
 | |
|   const style = { width: `${size}px`, height: `${size}px`, fontSize: `${size}px`, lineHeight: `${size}px` }
 | |
|   const [imgError, setImgError] = useState(false)
 | |
| 
 | |
|   const handleError = () => {
 | |
|     setImgError(true)
 | |
|   }
 | |
| 
 | |
|   if (avatar && !imgError) {
 | |
|     return (
 | |
|       <img
 | |
|         className={cn(avatarClassName, className)}
 | |
|         style={style}
 | |
|         alt={name}
 | |
|         src={avatar}
 | |
|         onError={handleError}
 | |
|       />
 | |
|     )
 | |
|   }
 | |
| 
 | |
|   return (
 | |
|     <div
 | |
|       className={cn(avatarClassName, className)}
 | |
|       style={style}
 | |
|     >
 | |
|       <div
 | |
|         className={cn(textClassName, 'scale-[0.4] text-center text-white')}
 | |
|         style={style}
 | |
|       >
 | |
|         {name[0].toLocaleUpperCase()}
 | |
|       </div>
 | |
|     </div>
 | |
|   )
 | |
| }
 | |
| 
 | |
| export default Avatar
 |