import React, { useCallback, useMemo, useRef } from 'react'; import { Avatar as AntAvatar } from 'antd'; import { UserOutlined } from '@ant-design/icons'; import type { UploadData } from '../../index'; import type { PicAvatarConfig } from '../index'; export interface PicAvatarProps extends PicAvatarConfig { value?: UploadData; } export function Avatar(props: PicAvatarProps) { const objUrlMap = useRef>(new Map()); const getObjUrl = useCallback((file: File) => { let objUrl = objUrlMap.current.get(file); if (!objUrl) { objUrl = URL.createObjectURL(file); objUrlMap.current.set(file, objUrl); } return objUrl; }, []); const icon = useMemo(() => props.icon || , [props.icon]); const value = useMemo(() => { if (!props.value) return ''; if (typeof props.value === 'string') return props.value; return props.value.originFileObj ? getObjUrl(props.value.originFileObj) : ''; }, [getObjUrl, props.value]); return !value && props.empty !== null && props.empty !== undefined ? ( <>{props.empty} ) : ( ); }