chatBoxAvatar.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React, { useMemo, ReactNode, ReactElement } from 'react';
  2. import Avatar from '../../avatar';
  3. import { Message, Metadata, RenderAvatarProps } from '../interface';
  4. import { cssClasses } from '@douyinfe/semi-foundation/chat/constants';
  5. import cls from 'classnames';
  6. const { PREFIX_CHAT_BOX } = cssClasses;
  7. interface ChatBoxAvatarProps {
  8. children?: string;
  9. role?: Metadata;
  10. continueSend?: boolean;
  11. message?: Message;
  12. customRenderFunc?: (props: RenderAvatarProps) => ReactNode
  13. }
  14. const ChatBoxAvatar = React.memo((props: ChatBoxAvatarProps) => {
  15. const { role, customRenderFunc, continueSend, message } = props;
  16. const node = useMemo(() => {
  17. const { avatar, color } = role;
  18. return (<Avatar
  19. className={cls(`${PREFIX_CHAT_BOX}-avatar`,
  20. {
  21. [`${PREFIX_CHAT_BOX}-avatar-hidden`]: continueSend
  22. })}
  23. src={avatar}
  24. size="extra-small"
  25. >
  26. </Avatar>);
  27. }, [role]);
  28. if (customRenderFunc && typeof customRenderFunc === 'function') {
  29. return customRenderFunc({
  30. role,
  31. defaultAvatar: node,
  32. message: message
  33. }) as ReactElement;
  34. }
  35. return node;
  36. });
  37. export default ChatBoxAvatar;