Skip to content

Commit

Permalink
✨ feat: 支持 renderMessageExtra
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jul 22, 2023
1 parent 63d51b0 commit 0f9692e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
11 changes: 9 additions & 2 deletions src/ChatItem/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -96,6 +99,7 @@ const ChatItem = memo<ChatItemProps>(
editing,
onChange,
onEditingChange,
messageExtra,
...properties
}) => {
const { cx, styles } = useStyles({
Expand All @@ -106,6 +110,7 @@ const ChatItem = memo<ChatItemProps>(
title: avatar.title,
type,
});

return (
<div className={cx(styles.container, className)} {...properties}>
<div className={styles.avatarContainer}>
Expand All @@ -130,14 +135,16 @@ const ChatItem = memo<ChatItemProps>(
{alert ? (
<Alert className={styles.alert} showIcon {...alert} />
) : (
<div className={styles.message} style={editing ? { padding: 12 } : {}}>
<Flexbox className={styles.message} style={editing ? { padding: 12 } : {}}>
<EditableMessage
editing={editing}
onChange={onChange}
onEditingChange={onEditingChange}
value={String(message || '...')}
/>
</div>

{messageExtra ? <div className={styles.messageExtra}>{messageExtra}</div> : null}

Check warning on line 146 in src/ChatItem/index.tsx

View workflow job for this annotation

GitHub Actions / test

Use `undefined` instead of `null`
</Flexbox>
)}
{!editing && (
<div className={styles.actions} role="chat-item-actions">
Expand Down
1 change: 1 addition & 0 deletions src/ChatItem/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand Down
58 changes: 36 additions & 22 deletions src/ChatList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo } from 'react';
import { ReactNode, memo } from 'react';

import ChatItem, { ChatItemProps } from '@/ChatItem';
import type { DivProps } from '@/types';
Expand All @@ -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
Expand All @@ -31,31 +32,44 @@ export interface ChatListProps extends DivProps {
}

const ChatList = memo<ChatListProps>(
({ onActionClick, className, data, type = 'chat', showTitle, ...props }) => {
({
onActionClick,
renderMessageExtra: MessageExtra,
className,
data,
type = 'chat',
showTitle,
...props
}) => {
const { cx, styles } = useStyles();

return (
<div className={cx(styles.container, className)} {...props}>
{data.map((item) => (
<ChatItem
actions={
<ActionsBar
onActionClick={
onActionClick ? (actionKey) => 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 ? <MessageExtra {...item} /> : undefined;

return (
<ChatItem
actions={
<ActionsBar
onActionClick={
onActionClick ? (actionKey) => 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'}
/>
);
})}
</div>
);
},
Expand Down
1 change: 0 additions & 1 deletion src/types/chatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export interface ChatMessage extends BaseDataModel {
target: string;
to: string;
};
// 语音
} & Record<string, any>;

parentId?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/types/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface MetaData {
}

export interface BaseDataModel {
createAt?: number;
createAt: number;
id: string;
meta: MetaData;
updateAt?: number;
updateAt: number;
}

1 comment on commit 0f9692e

@vercel
Copy link

@vercel vercel bot commented on 0f9692e Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lobe-ui – ./

lobe-ui-git-master-lobehub.vercel.app
lobe-ui.vercel.app
lobe-ui-lobehub.vercel.app
ui.lobehub.com

Please sign in to comment.