From e34cc7375d0f9f0af16e2e907c97d9fd0c7ef6d4 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 9 Jan 2025 14:42:12 +0800 Subject: [PATCH] refac: edit chat template --- src/components/ListAPI.tsx | 194 +++++++++++++++++++++++++++---------- 1 file changed, 141 insertions(+), 53 deletions(-) diff --git a/src/components/ListAPI.tsx b/src/components/ListAPI.tsx index b3879c8..1513262 100644 --- a/src/components/ListAPI.tsx +++ b/src/components/ListAPI.tsx @@ -29,10 +29,22 @@ import { PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; -import { BrushIcon } from "lucide-react"; +import { BrushIcon, DeleteIcon, EditIcon } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { newChatStore } from "@/types/newChatstore"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTrigger, +} from "./ui/dialog"; +import { DialogTitle } from "@radix-ui/react-dialog"; +import { Textarea } from "./ui/textarea"; +import { Label } from "./ui/label"; +import { Input } from "./ui/input"; +import { SetAPIsTemplate } from "./setAPIsTemplate"; +import { isVailedJSON } from "@/utils/isVailedJSON"; interface APITemplateDropdownProps { label: string; @@ -46,17 +58,29 @@ function APIsDropdownList({ apiField, keyField, }: APITemplateDropdownProps) { - const ctxAppChatStore = useContext(AppChatStoreContext); - const ctxApp = useContext(AppContext); - let API = ctxApp.templateAPIs; + const { chatStore, setChatStore } = useContext(AppChatStoreContext); + const { + templates, + templateAPIs, + templateAPIsImageGen, + templateAPIsTTS, + templateAPIsWhisper, + setTemplates, + setTemplateAPIs, + setTemplateAPIsImageGen, + setTemplateAPIsTTS, + setTemplateAPIsWhisper, + setTemplateTools, + } = useContext(AppContext); + let API = templateAPIs; if (label === "Chat API") { - API = ctxApp.templateAPIs; + API = templateAPIs; } else if (label === "Whisper API") { - API = ctxApp.templateAPIsWhisper; + API = templateAPIsWhisper; } else if (label === "TTS API") { - API = ctxApp.templateAPIsTTS; + API = templateAPIsTTS; } else if (label === "Image Gen API") { - API = ctxApp.templateAPIsImageGen; + API = templateAPIsImageGen; } return ( @@ -67,17 +91,14 @@ function APIsDropdownList({ {label}{" "} {API.find( (t: TemplateAPI) => - ctxAppChatStore.chatStore[apiField as keyof ChatStore] === - t.endpoint && - ctxAppChatStore.chatStore[keyField as keyof ChatStore] === t.key + chatStore[apiField as keyof ChatStore] === t.endpoint && + chatStore[keyField as keyof ChatStore] === t.key )?.name && `: ${ API.find( (t: TemplateAPI) => - ctxAppChatStore.chatStore[apiField as keyof ChatStore] === - t.endpoint && - ctxAppChatStore.chatStore[keyField as keyof ChatStore] === - t.key + chatStore[apiField as keyof ChatStore] === t.endpoint && + chatStore[keyField as keyof ChatStore] === t.key )?.name }`} @@ -90,20 +111,17 @@ function APIsDropdownList({ { // @ts-ignore - ctxAppChatStore.chatStore[apiField as keyof ChatStore] = - t.endpoint; + chatStore[apiField as keyof ChatStore] = t.endpoint; // @ts-ignore - ctxAppChatStore.chatStore[keyField] = t.key; - ctxAppChatStore.setChatStore({ - ...ctxAppChatStore.chatStore, + chatStore[keyField] = t.key; + setChatStore({ + ...chatStore, }); }} className={cn( "block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground", - ctxAppChatStore.chatStore[apiField as keyof ChatStore] === - t.endpoint && - ctxAppChatStore.chatStore[keyField as keyof ChatStore] === - t.key + chatStore[apiField as keyof ChatStore] === t.endpoint && + chatStore[keyField as keyof ChatStore] === t.key ? "bg-accent text-accent-foreground" : "" )} @@ -198,7 +216,7 @@ function ChatTemplateDropdownList() { const ctx = useContext(AppContext); const { chatStore, setChatStore } = useContext(AppChatStoreContext); - const { templates } = useContext(AppContext); + const { templates, setTemplates } = useContext(AppContext); return ( @@ -208,34 +226,104 @@ function ChatTemplateDropdownList() {