refactor: rename ListAPI component to APIListMenu and update related references for improved clarity

This commit is contained in:
ecwu
2025-01-05 18:29:28 +08:00
parent 75a431360b
commit c92b8f04cc
2 changed files with 49 additions and 22 deletions

View File

@@ -17,28 +17,45 @@ import {
NavigationMenuList, NavigationMenuList,
} from "@/components/ui/navigation-menu"; } from "@/components/ui/navigation-menu";
interface APITemplateItemProps { interface APITemplateDropdownProps {
label: string; label: string;
shortLabel: string;
ctx: any;
apiField: string; apiField: string;
keyField: string; keyField: string;
} }
function ListAPIs({ label, apiField, keyField }: APITemplateItemProps) { function APIsDropdownList({
const ctx = useContext(AppContext); label,
if (ctx === null) return <></>; shortLabel,
ctx,
apiField,
keyField,
}: APITemplateDropdownProps) {
let API = ctx.templateAPIs;
if (label === "Chat API") {
API = ctx.templateAPIs;
} else if (label === "Whisper API") {
API = ctx.templateAPIsWhisper;
} else if (label === "TTS API") {
API = ctx.templateAPIsTTS;
} else if (label === "Image Gen API") {
API = ctx.templateAPIsImageGen;
}
return ( return (
<NavigationMenuItem> <NavigationMenuItem>
<NavigationMenuTrigger> <NavigationMenuTrigger>
{label}{" "} <span className="lg:hidden">{shortLabel}</span>
<span className="hidden lg:inline"> <span className="hidden lg:inline">
{ctx.templateAPIs.find( {label}{" "}
(t) => {API.find(
(t: TemplateAPI) =>
ctx.chatStore[apiField as keyof ChatStore] === t.endpoint && ctx.chatStore[apiField as keyof ChatStore] === t.endpoint &&
ctx.chatStore[keyField as keyof ChatStore] === t.key ctx.chatStore[keyField as keyof ChatStore] === t.key
)?.name && )?.name &&
`: ${ `: ${
ctx.templateAPIs.find( API.find(
(t) => (t: TemplateAPI) =>
ctx.chatStore[apiField as keyof ChatStore] === t.endpoint && ctx.chatStore[apiField as keyof ChatStore] === t.endpoint &&
ctx.chatStore[keyField as keyof ChatStore] === t.key ctx.chatStore[keyField as keyof ChatStore] === t.key
)?.name )?.name
@@ -47,7 +64,7 @@ function ListAPIs({ label, apiField, keyField }: APITemplateItemProps) {
</NavigationMenuTrigger> </NavigationMenuTrigger>
<NavigationMenuContent> <NavigationMenuContent>
<ul className="grid w-[400px] gap-3 p-4 md:w-[500px] md:grid-cols-2 lg:w-[600px] "> <ul className="grid w-[400px] gap-3 p-4 md:w-[500px] md:grid-cols-2 lg:w-[600px] ">
{ctx.templateAPIs.map((t, index) => ( {API.map((t: TemplateAPI, index: number) => (
<li> <li>
<NavigationMenuLink asChild> <NavigationMenuLink asChild>
<a <a
@@ -113,7 +130,7 @@ function ListAPIs({ label, apiField, keyField }: APITemplateItemProps) {
); );
} }
function ListToolsTemplates() { function ToolsDropdownList() {
const ctx = useContext(AppContext); const ctx = useContext(AppContext);
if (!ctx) return <div>error</div>; if (!ctx) return <div>error</div>;
@@ -198,7 +215,7 @@ function ListToolsTemplates() {
); );
} }
const ListAPI: React.FC = () => { const APIListMenu: React.FC = () => {
const ctx = useContext(AppContext); const ctx = useContext(AppContext);
if (!ctx) return <div>error</div>; if (!ctx) return <div>error</div>;
return ( return (
@@ -206,34 +223,46 @@ const ListAPI: React.FC = () => {
<NavigationMenu> <NavigationMenu>
<NavigationMenuList> <NavigationMenuList>
{ctx.templateAPIs.length > 0 && ( {ctx.templateAPIs.length > 0 && (
<ListAPIs <APIsDropdownList
label="Chat API" label="Chat API"
shortLabel="Chat"
ctx={ctx}
apiField="apiEndpoint" apiField="apiEndpoint"
keyField="apiKey" keyField="apiKey"
/> />
)} )}
{ctx.templateAPIsWhisper.length > 0 && ( {ctx.templateAPIsWhisper.length > 0 && (
<ListAPIs <APIsDropdownList
label="Whisper API" label="Whisper API"
shortLabel="Whisper"
ctx={ctx}
apiField="whisper_api" apiField="whisper_api"
keyField="whisper_key" keyField="whisper_key"
/> />
)} )}
{ctx.templateAPIsTTS.length > 0 && ( {ctx.templateAPIsTTS.length > 0 && (
<ListAPIs label="TTS API" apiField="tts_api" keyField="tts_key" /> <APIsDropdownList
label="TTS API"
shortLabel="TTS"
ctx={ctx}
apiField="tts_api"
keyField="tts_key"
/>
)} )}
{ctx.templateAPIsImageGen.length > 0 && ( {ctx.templateAPIsImageGen.length > 0 && (
<ListAPIs <APIsDropdownList
label="Image Gen API" label="Image Gen API"
shortLabel="ImgGen"
ctx={ctx}
apiField="image_gen_api" apiField="image_gen_api"
keyField="image_gen_key" keyField="image_gen_key"
/> />
)} )}
{ctx.templateTools.length > 0 && <ListToolsTemplates />} {ctx.templateTools.length > 0 && <ToolsDropdownList />}
</NavigationMenuList> </NavigationMenuList>
</NavigationMenu> </NavigationMenu>
</div> </div>
); );
}; };
export default ListAPI; export default APIListMenu;

View File

@@ -41,7 +41,7 @@ import { Switch } from "@/components/ui/switch";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { AppContext } from "./App"; import { AppContext } from "./App";
import ListAPI from "@/components/ListAPI"; import APIListMenu from "@/components/ListAPI";
export default function ChatBOX() { export default function ChatBOX() {
const ctx = useContext(AppContext); const ctx = useContext(AppContext);
@@ -61,8 +61,6 @@ export default function ChatBOX() {
const [showGenerating, setShowGenerating] = useState(false); const [showGenerating, setShowGenerating] = useState(false);
const [generatingMessage, setGeneratingMessage] = useState(""); const [generatingMessage, setGeneratingMessage] = useState("");
const [showRetry, setShowRetry] = useState(false); const [showRetry, setShowRetry] = useState(false);
const [showAddToolMsg, setShowAddToolMsg] = useState(false);
const [showSearch, setShowSearch] = useState(false);
let default_follow = localStorage.getItem("follow"); let default_follow = localStorage.getItem("follow");
if (default_follow === null) { if (default_follow === null) {
default_follow = "true"; default_follow = "true";
@@ -396,7 +394,7 @@ export default function ChatBOX() {
return ( return (
<> <>
<ListAPI /> <APIListMenu />
<div className="grow flex flex-col p-2 w-full"> <div className="grow flex flex-col p-2 w-full">
<ChatMessageList> <ChatMessageList>
{chatStore.history.length === 0 && ( {chatStore.history.length === 0 && (