From 092ac46c15851a2377a8926fd27142a28bba56c4 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 27 Dec 2024 17:39:03 +0800 Subject: [PATCH] move status to AppContext --- src/addImage.tsx | 20 ++-- src/components/Settings.tsx | 220 ++++++++++++++++------------------- src/components/Templates.tsx | 14 +-- src/listAPIs.tsx | 49 ++++---- src/message.tsx | 15 ++- src/pages/App.tsx | 142 ++++++++++++++++++++-- src/pages/Chatbox.tsx | 190 +++++------------------------- src/search.tsx | 18 +-- 8 files changed, 317 insertions(+), 351 deletions(-) diff --git a/src/addImage.tsx b/src/addImage.tsx index ad2a196..14f6e99 100644 --- a/src/addImage.tsx +++ b/src/addImage.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useContext, useState } from "react"; import { ChatStore } from "@/types/chatstore"; import { MessageDetail } from "@/chatgpt"; import { Tr } from "@/translate"; @@ -20,10 +20,9 @@ import { Checkbox } from "./components/ui/checkbox"; import { Label } from "./components/ui/label"; import { Textarea } from "./components/ui/textarea"; import { Separator } from "./components/ui/separator"; +import { AppContext } from "./pages/App"; interface Props { - chatStore: ChatStore; - setChatStore: (cs: ChatStore) => void; images: MessageDetail[]; showAddImage: boolean; setShowAddImage: (se: boolean) => void; @@ -35,13 +34,14 @@ interface ImageResponse { revised_prompt: string; } export function AddImage({ - chatStore, - setChatStore, showAddImage, setShowAddImage, setImages, images, }: Props) { + const ctx = useContext(AppContext); + if (ctx === null) return <>; + const [enableHighResolution, setEnableHighResolution] = useState(true); const [imageGenPrompt, setImageGenPrompt] = useState(""); const [imageGenModel, setImageGenModel] = useState("dall-e-3"); @@ -134,7 +134,7 @@ export function AddImage({ - {chatStore.image_gen_api && chatStore.image_gen_key && ( + {ctx.chatStore.image_gen_api && ctx.chatStore.image_gen_key && (

Generate Image

@@ -240,11 +240,11 @@ export function AddImage({ body.style = imageGenStyle; } const resp: ImageResponse[] = ( - await fetch(chatStore.image_gen_api, { + await fetch(ctx.chatStore.image_gen_api, { method: "POST", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${chatStore.image_gen_key}`, + Authorization: `Bearer ${ctx.chatStore.image_gen_key}`, }, body: JSON.stringify(body), }).then((resp) => resp.json()) @@ -258,7 +258,7 @@ export function AddImage({ url = "data:image/png;base64," + image.b64_json; if (!url) continue; - chatStore.history.push({ + ctx.chatStore.history.push({ role: "assistant", content: [ { @@ -281,7 +281,7 @@ export function AddImage({ response_model_name: imageGenModel, }); - setChatStore({ ...chatStore }); + ctx.setChatStore({ ...ctx.chatStore }); } } catch (e) { console.error(e); diff --git a/src/components/Settings.tsx b/src/components/Settings.tsx index 7efa558..9fccba4 100644 --- a/src/components/Settings.tsx +++ b/src/components/Settings.tsx @@ -77,6 +77,7 @@ import { import { Separator } from "@/components/ui/separator"; import { Slider } from "@/components/ui/slider"; import { ScrollArea } from "@/components/ui/scroll-area"; +import { AppContext } from "@/pages/App"; const TTS_VOICES: string[] = [ "alloy", @@ -96,14 +97,13 @@ const Help = (props: { children: any; help: string; field: string }) => { ); }; -const SelectModel = (props: { - chatStore: ChatStore; - setChatStore: (cs: ChatStore) => void; - help: string; -}) => { +const SelectModel = (props: { help: string }) => { + const ctx = useContext(AppContext); + if (ctx === null) return <>; + let shouldIUseCustomModel: boolean = true; for (const model in models) { - if (props.chatStore.model === model) { + if (ctx.chatStore.model === model) { shouldIUseCustomModel = false; } } @@ -143,22 +143,22 @@ const SelectModel = (props: { {useCustomModel ? ( ) => { - props.chatStore.model = e.target.value; - props.setChatStore({ ...props.chatStore }); + ctx.chatStore.model = e.target.value; + ctx.setChatStore({ ...ctx.chatStore }); }} /> ) : (