From 9383cf045aac5a0d6bca14088bc3d24d28f1362b Mon Sep 17 00:00:00 2001 From: ecwu Date: Sun, 5 Jan 2025 00:00:11 +0800 Subject: [PATCH] refactor: replace ListAPIs component with ListAPI for improved structure and organization --- src/components/ListAPI.tsx | 254 +++++++++++++++++++++++++++++++++++++ src/listAPIs.tsx | 109 ---------------- src/pages/Chatbox.tsx | 47 +------ 3 files changed, 256 insertions(+), 154 deletions(-) create mode 100644 src/components/ListAPI.tsx diff --git a/src/components/ListAPI.tsx b/src/components/ListAPI.tsx new file mode 100644 index 0000000..e7e2b9e --- /dev/null +++ b/src/components/ListAPI.tsx @@ -0,0 +1,254 @@ +import React from "react"; +import { ChatStore, TemplateAPI } from "@/types/chatstore"; +import { Tr } from "@/translate"; + +import { + NavigationMenuContent, + NavigationMenuItem, + NavigationMenuLink, + NavigationMenuTrigger, +} from "@/components/ui/navigation-menu"; +import { Button } from "@/components/ui/button"; +import { cn } from "@/lib/utils"; +import { useContext } from "react"; +import { AppContext } from "@/pages/App"; +import { + NavigationMenu, + NavigationMenuList, +} from "@/components/ui/navigation-menu"; + +interface APITemplateItemProps { + label: string; + shortLabel: string; + apiField: string; + keyField: string; +} +function ListAPIs({ + label, + shortLabel, + apiField, + keyField, +}: APITemplateItemProps) { + const ctx = useContext(AppContext); + if (ctx === null) return <>; + + return ( + + + {shortLabel} + + {label}{" "} + {ctx.templateAPIs.find( + (t) => + ctx.chatStore[apiField as keyof ChatStore] === t.endpoint && + ctx.chatStore[keyField as keyof ChatStore] === t.key + )?.name && + `: ${ + ctx.templateAPIs.find( + (t) => + ctx.chatStore[apiField as keyof ChatStore] === t.endpoint && + ctx.chatStore[keyField as keyof ChatStore] === t.key + )?.name + }`} + + + + + + + ); +} + +function ListToolsTemplates() { + const ctx = useContext(AppContext); + if (!ctx) return
error
; + + const { chatStore, setChatStore } = ctx; + + return ( + + + {Tr(`Saved tools templates`)} + + + + + + + ); +} + +const ListAPI: React.FC = () => { + const ctx = useContext(AppContext); + if (!ctx) return
error
; + return ( +
+ + + {ctx.templateAPIs.length > 0 && ( + + )} + {ctx.templateAPIsWhisper.length > 0 && ( + + )} + {ctx.templateAPIsTTS.length > 0 && ( + + )} + {ctx.templateAPIsImageGen.length > 0 && ( + + )} + {ctx.templateTools.length > 0 && } + + +
+ ); +}; + +export default ListAPI; diff --git a/src/listAPIs.tsx b/src/listAPIs.tsx index a48f4fe..139597f 100644 --- a/src/listAPIs.tsx +++ b/src/listAPIs.tsx @@ -1,111 +1,2 @@ -import { ChatStore, TemplateAPI } from "@/types/chatstore"; -import { Tr } from "@/translate"; -import { - NavigationMenuContent, - NavigationMenuItem, - NavigationMenuLink, - NavigationMenuTrigger, -} from "@/components/ui/navigation-menu"; -import { Button } from "./components/ui/button"; -import { cn } from "@/lib/utils"; -import { useContext } from "react"; -import { AppContext } from "./pages/App"; -interface Props { - label: string; - shortLabel: string; - apiField: string; - keyField: string; -} -export function ListAPIs({ label, shortLabel, apiField, keyField }: Props) { - const ctx = useContext(AppContext); - if (ctx === null) return <>; - - return ( - - - {shortLabel} - - {label}{" "} - {ctx.templateAPIs.find( - (t) => - ctx.chatStore[apiField as keyof ChatStore] === t.endpoint && - ctx.chatStore[keyField as keyof ChatStore] === t.key - )?.name && - `: ${ - ctx.templateAPIs.find( - (t) => - ctx.chatStore[apiField as keyof ChatStore] === t.endpoint && - ctx.chatStore[keyField as keyof ChatStore] === t.key - )?.name - }`} - - - - - - - ); -} diff --git a/src/pages/Chatbox.tsx b/src/pages/Chatbox.tsx index a06175f..e4a4d75 100644 --- a/src/pages/Chatbox.tsx +++ b/src/pages/Chatbox.tsx @@ -15,8 +15,6 @@ import { ChatStoreMessage } from "../types/chatstore"; import Message from "@/components/MessageBubble"; import { models } from "@/types/models"; import { AddImage } from "@/addImage"; -import { ListAPIs } from "@/listAPIs"; -import { ListToolsTemplates } from "@/listToolsTemplates"; import { autoHeight } from "@/utils/textAreaHelp"; import VersionHint from "@/components/VersionHint"; import WhisperButton from "@/components/WhisperButton"; @@ -41,12 +39,9 @@ import { } from "lucide-react"; import { Switch } from "@/components/ui/switch"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; -import { - NavigationMenu, - NavigationMenuList, -} from "@/components/ui/navigation-menu"; import { AppContext } from "./App"; +import ListAPI from "@/components/ListAPI"; export default function ChatBOX() { const ctx = useContext(AppContext); @@ -401,45 +396,7 @@ export default function ChatBOX() { return ( <> -
- - - {ctx.templateAPIs.length > 0 && ( - - )} - {ctx.templateAPIsWhisper.length > 0 && ( - - )} - {ctx.templateAPIsTTS.length > 0 && ( - - )} - {ctx.templateAPIsImageGen.length > 0 && ( - - )} - {ctx.templateTools.length > 0 && } - - -
+
{chatStore.history.length === 0 && (