Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chat): update chat component #554

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/chat/content/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
overflow: auto;
&--disabled {
pointer-events: none;
}
&--valid {
overflow: auto;
}
}
&__inner__holder {
display: flex;
Expand Down
16 changes: 3 additions & 13 deletions src/chat/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { forwardRef, useImperativeHandle, useLayoutEffect, useRef, useSta
import classNames from 'classnames';

import { Message as MessageEntity, MessageStatus, Prompt as PromptEntity } from '../entity';
import { RobotIcon } from '../icon';
import Message from '../message';
import Prompt from '../prompt';
import { useContext } from '../useContext';
Expand All @@ -11,7 +10,6 @@ import './index.scss';
export interface IContentProps {
data: PromptEntity[];
placeholder?: React.ReactNode;
robotIcon?: boolean;
scrollable?: boolean;
onRegenerate?: (data: MessageEntity, prompt: PromptEntity) => void;
onStop?: (data: MessageEntity, prompt: PromptEntity) => void;
Expand All @@ -23,7 +21,7 @@ export interface IContentRef {
}

const Content = forwardRef<IContentRef, IContentProps>(function (
{ data, placeholder, robotIcon = true, scrollable = true, onRegenerate, onStop },
{ data, placeholder, scrollable = true, onRegenerate, onStop },
forwardedRef
) {
const { maxRegenerateCount, copy, regenerate } = useContext();
Expand Down Expand Up @@ -100,8 +98,7 @@ const Content = forwardRef<IContentRef, IContentProps>(function (
<div
className={classNames(
'dtc__aigc__content__container',
!scrollable && 'dtc__aigc__content__container--disabled',
dataValid && scrollable && 'dtc__aigc__content__container--valid'
!scrollable && 'dtc__aigc__content__container--disabled'
)}
ref={containerRef}
>
Expand Down Expand Up @@ -143,14 +140,7 @@ const Content = forwardRef<IContentRef, IContentProps>(function (
})}
</div>
) : (
<React.Fragment>
{placeholder}
{robotIcon && (
<RobotIcon
style={{ fontSize: 200, position: 'absolute', right: 0, bottom: -100 }}
/>
)}
</React.Fragment>
<React.Fragment>{placeholder}</React.Fragment>
)}
</div>
);
Expand Down
74 changes: 49 additions & 25 deletions src/chat/demos/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React, { useEffect, useState } from 'react';
import { LikeOutlined } from '@ant-design/icons';
import { Button } from 'antd';
import { Chat } from 'dt-react-component';
import { Chat, Flex } from 'dt-react-component';

import { mockSSE } from './mockSSE';

export default function () {
const chat = Chat.useChat();
const [value, setValue] = useState('');
const [value, setValue] = useState<string | undefined>('');

const handleSubmit = (raw: string = value) => {
const val = raw.trim();
const [convert, setConvert] = useState(false);

const handleSubmit = (raw = value) => {
const val = raw?.trim();
if (chat.loading() || !val) return;
setValue('');
const promptId = new Date().valueOf().toString();
Expand All @@ -35,35 +38,56 @@ export default function () {
}, []);

return (
<div style={{ width: '100%', height: 400 }}>
<Chat chat={chat}>
<div style={{ width: '100%', height: 400, marginBottom: 56 }}>
<Chat
chat={chat}
codeBlock={{
convert,
}}
messageIcons={() => <LikeOutlined className="dtc__message__icon" />}
components={{
a: ({ children }) => (
<Button
type="primary"
size="small"
ghost
onClick={() => setConvert((p) => !p)}
>
{children}
</Button>
),
}}
>
<Chat.Content
data={chat.conversation.get()?.prompts || []}
placeholder={
<h1>
有什么可以帮忙的?
<Chat.Welcome
title="dt-react-component"
description="React UI component library based on antd package"
/>
<br />
<Button onClick={() => handleSubmit('请告诉我一首诗')}>
返回一首诗
</Button>
<Flex vertical align="start" gap="4px">
<Chat.Tag onClick={() => handleSubmit('请告诉我一首诗')}>
返回一首诗
</Chat.Tag>
<Chat.Tag onClick={() => handleSubmit('生成 CodeBlock')}>
生成 CodeBlock
</Chat.Tag>
</Flex>
</h1>
}
/>
<div style={{ display: 'flex', gap: 4 }}>
<Chat.Input
value={value}
onChange={setValue}
onPressEnter={() => handleSubmit()}
placeholder="请输入想咨询的内容…"
/>
<Chat.Button
type="primary"
onClick={() => handleSubmit()}
disabled={chat.loading() || !value}
>
<Chat.Icon.SendIcon style={{ fontSize: 16 }} />
</Chat.Button>
</div>
<Chat.Input
value={value}
onChange={setValue}
onPressEnter={() => handleSubmit()}
onPressShiftEnter={() => setValue((v) => v + '\n')}
button={{
disabled: chat.loading() || !value?.trim(),
}}
placeholder="请输入想咨询的内容…"
/>
</Chat>
</div>
);
Expand Down
32 changes: 16 additions & 16 deletions src/chat/demos/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ export default function () {
<Chat.Button
type="primary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
/>
<Chat.Button
type="primary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
发送
按钮
</Chat.Button>
<Chat.Button
type="default"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
发送
按钮
</Chat.Button>
<Chat.Button
type="secondary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
发送
按钮
</Chat.Button>
<Chat.Button
type="secondary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
AI log parsing
</Chat.Button>
Expand All @@ -54,37 +54,37 @@ export default function () {
size="small"
type="primary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
/>
<Chat.Button
size="small"
type="primary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
发送
按钮
</Chat.Button>
<Chat.Button
size="small"
type="default"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
发送
按钮
</Chat.Button>
<Chat.Button
size="small"
type="secondary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
发送
按钮
</Chat.Button>
<Chat.Button
size="small"
type="secondary"
disabled={disabled}
icon={<Chat.Icon.SendIcon style={{ fontSize: 16 }} />}
icon={<Chat.Icon.ShiningIcon style={{ fontSize: 16 }} />}
>
AI log parsing
</Chat.Button>
Expand Down
44 changes: 22 additions & 22 deletions src/chat/demos/global-state/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useMemo, useState } from 'react';
import { Button, Tabs } from 'antd';
import { Tabs } from 'antd';
import { Chat } from 'dt-react-component';
import { Conversation, Message, MessageStatus, Prompt } from 'dt-react-component/chat/entity';
import { produce } from 'immer';
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function () {
return conversations[activeKey];
}, [activeKey, conversations]);

const handleSubmit = (val: string) => {
const handleSubmit = (val = '') => {
if (!data) {
const promptId = new Date().valueOf().toString();
const messageId = (new Date().valueOf() + 1).toString();
Expand Down Expand Up @@ -122,38 +122,38 @@ export default function () {
);
}

function AI({ data, onSubmit }: { data?: Conversation; onSubmit?: (str: string) => void }) {
function AI({ data, onSubmit }: { data?: Conversation; onSubmit?: (str?: string) => void }) {
const chat = Chat.useChat();
const [value, setValue] = useState('');
const [value, setValue] = useState<string | undefined>('');

return (
<div style={{ width: '100%', height: 400 }}>
<div style={{ width: '100%', height: 400, marginBottom: 46 }}>
<Chat chat={chat}>
<Chat.Content
data={data?.prompts || []}
placeholder={
<h1>
有什么可以帮忙的?
<Chat.Welcome
title="dt-react-component"
description="React UI component library based on antd package"
/>
<br />
<Button onClick={() => onSubmit?.('请告诉我一首诗')}>返回一首诗</Button>
<Chat.Tag onClick={() => onSubmit?.('请告诉我一首诗')}>
返回一首诗
</Chat.Tag>
</h1>
}
/>
<div style={{ display: 'flex', gap: 4 }}>
<Chat.Input
value={value}
onChange={setValue}
onPressEnter={() => onSubmit?.(value)}
placeholder="请输入想咨询的内容…"
/>
<Chat.Button
type="primary"
onClick={() => onSubmit?.(value)}
disabled={chat.loading() || !value}
>
<Chat.Icon.SendIcon style={{ fontSize: 16 }} />
</Chat.Button>
</div>
<Chat.Input
value={value}
onChange={setValue}
onPressEnter={() => onSubmit?.(value)}
onPressShiftEnter={() => setValue((v) => v + '\n')}
button={{
disabled: chat.loading() || !value?.trim(),
}}
placeholder="请输入想咨询的内容…"
/>
</Chat>
</div>
);
Expand Down
Loading
Loading