import { Button, Textarea } from "@mui/joy"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import * as api from "../helpers/api"; import useLoading from "../hooks/useLoading"; import { marked } from "../labs/marked"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import showSettingDialog from "./SettingDialog"; type Props = DialogProps; interface History { question: string; answer: string; } const AskAIDialog: React.FC = (props: Props) => { const { destroy, hide } = props; const fetchingState = useLoading(false); const [historyList, setHistoryList] = useState([]); const [isEnabled, setIsEnabled] = useState(true); const [question, setQuestion] = useState(""); useEffect(() => { api.checkOpenAIEnabled().then(({ data }) => { const { data: enabled } = data; setIsEnabled(enabled); }); }, []); const handleGotoSystemSetting = () => { showSettingDialog("system"); destroy(); }; const handleQuestionTextareaChange = async (event: React.ChangeEvent) => { setQuestion(event.currentTarget.value); }; const handleSendQuestionButtonClick = async () => { fetchingState.setLoading(); try { await askQuestion(question); } catch (error: any) { console.error(error); toast.error(error.response.data.error); } setQuestion(""); fetchingState.setFinish(); }; const askQuestion = async (question: string) => { if (question === "") { return; } const { data: { data: answer }, } = await api.postChatCompletion(question); setHistoryList([ { question, answer: answer.replace(/^\n\n/, ""), }, ...historyList, ]); }; return ( <>

Ask AI