This commit is contained in:
2025-12-07 12:45:37 +08:00
parent 35d7272872
commit 47ce1962c0
2 changed files with 24 additions and 12 deletions

View File

@@ -1,14 +1,14 @@
import { app } from '../ai';
import { Sender, XProvider } from '@ant-design/x';
import { useEffect, useRef, useState } from 'react';
import { Sender, XProvider, Bubble } from '@ant-design/x';
import { useEffect, useMemo, useRef, useState } from 'react';
import { postChat } from './chat';
import { Nav } from '../nav';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useHomeStore } from './store';
import { nanoid } from 'nanoid';
const useFocus = () => {
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
@@ -37,26 +37,32 @@ const useFocus = () => {
export const App = () => {
const inputRef = useFocus();
const [content, setContent] = useState<string>('');
const { inputValue, setInputValue, isLoading, setLoading } = useHomeStore();
const { inputValue, setInputValue, isLoading, setLoading, messages, addMessage } = useHomeStore();
return <div className='container mx-auto px-4 py-4 md:p-4'>
<div className='md:top-20 left-0 mb-4 right-0 w-full mx-auto px-4 md:px-0' ref={inputRef}>
<Sender
allowSpeech
const msg = useMemo(() => {
return <Bubble.List items={messages} autoScroll ></Bubble.List>
}, [messages]);
return <div className='container mx-auto px-4 py-4 md:p-4 flex flex-col' style={{ height: 'calc(100vh - 64px)' }}>
<div className='flex-shrink-0 mb-4 w-full mx-auto px-4 md:px-0' ref={inputRef}>
<Sender
allowSpeech
value={inputValue}
onChange={setInputValue}
loading={isLoading}
onSubmit={async (message) => {
console.log('Submitted', message);
setLoading(true);
addMessage({ role: 'user', content: message, key: nanoid() });
const res = await postChat(message);
setContent(res || ' ');
setLoading(false);
}}
addMessage({ role: 'assistant', content: res || ' ', key: nanoid() });
}}
/>
</div>
<div className='mb-20 md:mb-16 px-2 md:px-0'>
{content}
<div className='flex-1 overflow-y-auto px-2 md:px-0 mb-4'>
{msg}
</div>
</div >;
}

View File

@@ -12,10 +12,12 @@ interface HomeState {
inputValue: string;
// 加载状态
isLoading: boolean;
// Actions
setInputValue: (value: string) => void;
setLoading: (loading: boolean) => void;
messages: { role: 'user' | 'assistant'; content: string, key: string }[];
addMessage: (message: { role: 'user' | 'assistant'; content: string, key: string }) => void;
}
export const useHomeStore = create<HomeState>((set) => ({
@@ -24,4 +26,8 @@ export const useHomeStore = create<HomeState>((set) => ({
setInputValue: (value) => set({ inputValue: value }),
setLoading: (loading) => set({ isLoading: loading }),
messages: [],
addMessage: (message) => {
set((state) => ({ messages: [...state.messages, message] }))
}
}));