diff --git a/src/apps/home/index.tsx b/src/apps/home/index.tsx index bc3d147..34266be 100644 --- a/src/apps/home/index.tsx +++ b/src/apps/home/index.tsx @@ -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(null); useEffect(() => { @@ -37,26 +37,32 @@ const useFocus = () => { export const App = () => { const inputRef = useFocus(); const [content, setContent] = useState(''); - const { inputValue, setInputValue, isLoading, setLoading } = useHomeStore(); + const { inputValue, setInputValue, isLoading, setLoading, messages, addMessage } = useHomeStore(); - return
-
- { + return + }, [messages]); + return
+
+ { 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() }); + }} />
-
- {content} +
+ {msg}
; } diff --git a/src/apps/home/store.ts b/src/apps/home/store.ts index c008c8d..95be982 100644 --- a/src/apps/home/store.ts +++ b/src/apps/home/store.ts @@ -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((set) => ({ @@ -24,4 +26,8 @@ export const useHomeStore = create((set) => ({ setInputValue: (value) => set({ inputValue: value }), setLoading: (loading) => set({ isLoading: loading }), + messages: [], + addMessage: (message) => { + set((state) => ({ messages: [...state.messages, message] })) + } }));