import { useState, useEffect, FormEvent } from 'react'; import { JellyTriangle } from '@uiball/loaders'; import { useReplyWithChatGPTMutation } from '../app/services/message'; import Textarea from '../common/component/styled/Textarea'; import Button from '../common/component/styled/Button'; import clsx from 'clsx'; // type Props = {} type Conversation = { type: "prompt" | "reply", content: string }[] const GPTDemo = () => { // const promptRef = useRef(); // const [prompt, setPrompt] = useState(""); const [talk, setTalk] = useState([]); const [feedTheGPT, { data, isSuccess, isLoading }] = useReplyWithChatGPTMutation(); const handleSend = (evt: FormEvent) => { evt.preventDefault(); const form = evt.currentTarget; // 检查格式 if (!form?.checkValidity()) { form?.reportValidity(); return; } const content = new FormData(form).get("prompt") as string; feedTheGPT(content); setTalk(prevs => { return [...prevs, { type: "prompt", content }]; }); form.reset(); }; useEffect(() => { if (isSuccess && data) { const { message } = data; setTalk(prevs => { return [...prevs, { type: "reply", content: message }]; }); } }, [isSuccess, data]); return (
{talk.map(({ type, content }) => { return
{type}:
{content}
; })}
{isLoading && }