From 0f9692e7328c88b1aca164def5e2dd1b873af25d Mon Sep 17 00:00:00 2001 From: arvinxx Date: Sat, 22 Jul 2023 19:17:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=94=AF=E6=8C=81=20render?= =?UTF-8?q?MessageExtra?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ChatItem/index.tsx | 11 ++++++-- src/ChatItem/style.ts | 1 + src/ChatList/index.tsx | 58 +++++++++++++++++++++++++--------------- src/types/chatMessage.ts | 1 - src/types/meta.ts | 4 +-- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/ChatItem/index.tsx b/src/ChatItem/index.tsx index 4ab5a28b..864b92eb 100644 --- a/src/ChatItem/index.tsx +++ b/src/ChatItem/index.tsx @@ -1,6 +1,7 @@ import { Alert, type AlertProps } from 'antd'; import { Loader2 } from 'lucide-react'; import { ReactNode, memo } from 'react'; +import { Flexbox } from 'react-layout-kit'; import Avatar from '@/Avatar'; import EditableMessage from '@/EditableMessage'; @@ -45,6 +46,7 @@ export interface ChatItemProps { * @description The message content of the chat item */ message?: string; + messageExtra?: ReactNode; /** * @description Callback when the message content changes * @param value - The new message content @@ -68,6 +70,7 @@ export interface ChatItemProps { * @description Whether to show the title of the chat item */ showTitle?: boolean; + /** * @description The timestamp of the chat item */ @@ -96,6 +99,7 @@ const ChatItem = memo( editing, onChange, onEditingChange, + messageExtra, ...properties }) => { const { cx, styles } = useStyles({ @@ -106,6 +110,7 @@ const ChatItem = memo( title: avatar.title, type, }); + return (
@@ -130,14 +135,16 @@ const ChatItem = memo( {alert ? ( ) : ( -
+ -
+ + {messageExtra ?
{messageExtra}
: null} + )} {!editing && (
diff --git a/src/ChatItem/style.ts b/src/ChatItem/style.ts index cfa027fa..8e37a14e 100644 --- a/src/ChatItem/style.ts +++ b/src/ChatItem/style.ts @@ -168,6 +168,7 @@ export const useStyles = createStyles( gap: 8px; align-items: ${placement === 'left' ? 'flex-start' : 'flex-end'}; `, + messageExtra: cx('message-extra'), name: css` position: ${showTitle ? 'relative' : 'absolute'}; top: ${showTitle ? 'unset' : '-16px'}; diff --git a/src/ChatList/index.tsx b/src/ChatList/index.tsx index f61d3a7f..0bae6792 100644 --- a/src/ChatList/index.tsx +++ b/src/ChatList/index.tsx @@ -1,4 +1,4 @@ -import { memo } from 'react'; +import { ReactNode, memo } from 'react'; import ChatItem, { ChatItemProps } from '@/ChatItem'; import type { DivProps } from '@/types'; @@ -18,6 +18,7 @@ export interface ChatListProps extends DivProps { * @param {string} messageId - The id of the message */ onActionClick?: (actionKey: string, messageId: string) => void; + renderMessageExtra?: (props: ChatMessage) => ReactNode; /** * @description Whether to show name of the chat item * @default false @@ -31,31 +32,44 @@ export interface ChatListProps extends DivProps { } const ChatList = memo( - ({ onActionClick, className, data, type = 'chat', showTitle, ...props }) => { + ({ + onActionClick, + renderMessageExtra: MessageExtra, + className, + data, + type = 'chat', + showTitle, + ...props + }) => { const { cx, styles } = useStyles(); return (
- {data.map((item) => ( - onActionClick?.(actionKey, item.id) : undefined - } - primary={item.role === 'user'} - /> - } - avatar={item.meta} - key={item.id} - message={item.content} - placement={type === 'chat' ? (item.role === 'user' ? 'right' : 'left') : 'left'} - primary={item.role === 'user'} - showTitle={showTitle} - time={item.updateAt || item.createAt} - type={type === 'chat' ? 'block' : 'pure'} - /> - ))} + {data.map((item) => { + const renderMessageExtra = MessageExtra ? : undefined; + + return ( + onActionClick?.(actionKey, item.id) : undefined + } + primary={item.role === 'user'} + /> + } + avatar={item.meta} + key={item.id} + message={item.content} + messageExtra={renderMessageExtra} + placement={type === 'chat' ? (item.role === 'user' ? 'right' : 'left') : 'left'} + primary={item.role === 'user'} + showTitle={showTitle} + time={item.updateAt || item.createAt} + type={type === 'chat' ? 'block' : 'pure'} + /> + ); + })}
); }, diff --git a/src/types/chatMessage.ts b/src/types/chatMessage.ts index f21c034e..460e2935 100644 --- a/src/types/chatMessage.ts +++ b/src/types/chatMessage.ts @@ -27,7 +27,6 @@ export interface ChatMessage extends BaseDataModel { target: string; to: string; }; - // 语音 } & Record; parentId?: string; diff --git a/src/types/meta.ts b/src/types/meta.ts index 4d1e33d9..77abe8b1 100644 --- a/src/types/meta.ts +++ b/src/types/meta.ts @@ -19,8 +19,8 @@ export interface MetaData { } export interface BaseDataModel { - createAt?: number; + createAt: number; id: string; meta: MetaData; - updateAt?: number; + updateAt: number; }