()
| 19 | import { Template } from "../utils/templateStorage"; |
| 20 | |
| 21 | const Home: NextPage = () => { |
| 22 | const t = useTranslations('Index') |
| 23 | |
| 24 | const [loading, setLoading] = useState(false); |
| 25 | const [chat, setChat] = useState(""); |
| 26 | const [form, setForm] = useState<FormType>("paragraphForm"); |
| 27 | const [api_key, setAPIKey] = useState("") |
| 28 | const [generatedChat, setGeneratedChat] = useState<String>(""); |
| 29 | const [isFromHistory, setIsFromHistory] = useState(false); |
| 30 | const [showMobileHistory, setShowMobileHistory] = useState(false); |
| 31 | const [historyCount, setHistoryCount] = useState(0); |
| 32 | const [historyRefreshTrigger, setHistoryRefreshTrigger] = useState(0); |
| 33 | const [showTemplateSelect, setShowTemplateSelect] = useState(false); |
| 34 | const [showTemplateManager, setShowTemplateManager] = useState(false); |
| 35 | |
| 36 | // 更新历史记录数量和触发刷新 |
| 37 | const updateHistoryCount = () => { |
| 38 | setHistoryCount(HistoryStorage.getHistory().length); |
| 39 | }; |
| 40 | |
| 41 | const triggerHistoryRefresh = () => { |
| 42 | setHistoryRefreshTrigger(prev => prev + 1); |
| 43 | updateHistoryCount(); |
| 44 | }; |
| 45 | |
| 46 | // 组件挂载时加载历史记录数量 |
| 47 | useEffect(() => { |
| 48 | updateHistoryCount(); |
| 49 | }, []); |
| 50 | |
| 51 | console.log("Streamed response: ", generatedChat); |
| 52 | |
| 53 | const prompt = |
| 54 | form === 'paragraphForm'? |
| 55 | `${chat}` |
| 56 | : `${chat}`; |
| 57 | |
| 58 | const useUserKey = process.env.NEXT_PUBLIC_USE_USER_KEY === "true" ? true : false; |
| 59 | |
| 60 | const generateChat = async (e: any) => { |
| 61 | e.preventDefault(); |
| 62 | setGeneratedChat(""); |
| 63 | setLoading(true); |
| 64 | if (useUserKey && api_key == ""){ |
| 65 | toast.error(t("API_KEY_NULL_ERROR")) |
| 66 | setLoading(false) |
| 67 | return |
| 68 | } |
| 69 | if (chat == ""){ |
| 70 | toast.error(t("CONTENT_NULL_ERROR")) |
| 71 | setLoading(false) |
| 72 | return |
| 73 | } |
| 74 | const response = useUserKey ? |
| 75 | await fetch("/api/generate", { |
| 76 | method: "POST", |
| 77 | headers: { |
| 78 | "Content-Type": "application/json", |
nothing calls this directly
no test coverage detected