refac: api template menu
This commit is contained in:
@@ -1,16 +1,14 @@
|
|||||||
import React from "react";
|
import React, { useContext } from "react";
|
||||||
import { ChatStore, TemplateAPI, TemplateChatStore } from "@/types/chatstore";
|
import {
|
||||||
|
ChatStore,
|
||||||
|
TemplateAPI,
|
||||||
|
TemplateChatStore,
|
||||||
|
TemplateTools,
|
||||||
|
} from "@/types/chatstore";
|
||||||
import { Tr } from "@/translate";
|
import { Tr } from "@/translate";
|
||||||
|
|
||||||
import {
|
|
||||||
NavigationMenuContent,
|
|
||||||
NavigationMenuItem,
|
|
||||||
NavigationMenuLink,
|
|
||||||
NavigationMenuTrigger,
|
|
||||||
} from "@/components/ui/navigation-menu";
|
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { useContext } from "react";
|
|
||||||
import { AppChatStoreContext, AppContext } from "@/pages/App";
|
import { AppChatStoreContext, AppContext } from "@/pages/App";
|
||||||
import {
|
import {
|
||||||
NavigationMenu,
|
NavigationMenu,
|
||||||
@@ -83,63 +81,53 @@ function APIsDropdownList({
|
|||||||
API = templateAPIsImageGen;
|
API = templateAPIsImageGen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [open, setOpen] = React.useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NavigationMenuItem>
|
<div className="flex items-center space-x-4 mx-3">
|
||||||
<NavigationMenuTrigger>
|
<p className="text-sm text-muted-foreground">
|
||||||
<span className="lg:hidden">{shortLabel}</span>
|
<Tr>{label}</Tr>
|
||||||
<span className="hidden lg:inline">
|
</p>
|
||||||
{label}{" "}
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
|
<PopoverTrigger asChild>
|
||||||
|
<Button variant="outline" className="w-[150px] justify-start">
|
||||||
{API.find(
|
{API.find(
|
||||||
(t: TemplateAPI) =>
|
(t: TemplateAPI) =>
|
||||||
chatStore[apiField as keyof ChatStore] === t.endpoint &&
|
chatStore[apiField as keyof ChatStore] === t.endpoint &&
|
||||||
chatStore[keyField as keyof ChatStore] === t.key
|
chatStore[keyField as keyof ChatStore] === t.key
|
||||||
)?.name &&
|
)?.name || `+ ${shortLabel}`}
|
||||||
`: ${
|
</Button>
|
||||||
API.find(
|
</PopoverTrigger>
|
||||||
(t: TemplateAPI) =>
|
<PopoverContent className="p-0" side="bottom" align="start">
|
||||||
chatStore[apiField as keyof ChatStore] === t.endpoint &&
|
<Command>
|
||||||
chatStore[keyField as keyof ChatStore] === t.key
|
<CommandInput placeholder="Search template..." />
|
||||||
)?.name
|
<CommandList>
|
||||||
}`}
|
<CommandEmpty>
|
||||||
</span>
|
<Tr>No results found.</Tr>
|
||||||
</NavigationMenuTrigger>
|
</CommandEmpty>
|
||||||
<NavigationMenuContent>
|
<CommandGroup>
|
||||||
<ul className="grid w-[400px] gap-3 p-4 md:w-[500px] md:grid-cols-2 lg:w-[600px] ">
|
|
||||||
{API.map((t: TemplateAPI, index: number) => (
|
{API.map((t: TemplateAPI, index: number) => (
|
||||||
<li key={index}>
|
<CommandItem
|
||||||
<NavigationMenuLink
|
key={index}
|
||||||
asChild
|
value={t.name}
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
// @ts-ignore
|
|
||||||
chatStore[apiField as keyof ChatStore] = t.endpoint;
|
|
||||||
// @ts-ignore
|
|
||||||
chatStore[keyField] = t.key;
|
|
||||||
setChatStore({
|
setChatStore({
|
||||||
...chatStore,
|
...chatStore,
|
||||||
|
[apiField]: t.endpoint,
|
||||||
|
[keyField]: t.key,
|
||||||
});
|
});
|
||||||
|
setOpen(false); // Close popover after selecting
|
||||||
}}
|
}}
|
||||||
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",
|
|
||||||
chatStore[apiField as keyof ChatStore] === t.endpoint &&
|
|
||||||
chatStore[keyField as keyof ChatStore] === t.key
|
|
||||||
? "bg-accent text-accent-foreground"
|
|
||||||
: ""
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
<a>
|
|
||||||
<div className="text-sm font-medium leading-none">
|
|
||||||
{t.name}
|
{t.name}
|
||||||
</div>
|
</CommandItem>
|
||||||
<p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
|
|
||||||
{new URL(t.endpoint).host}
|
|
||||||
</p>
|
|
||||||
</a>
|
|
||||||
</NavigationMenuLink>
|
|
||||||
</li>
|
|
||||||
))}
|
))}
|
||||||
</ul>
|
</CommandGroup>
|
||||||
</NavigationMenuContent>
|
</CommandList>
|
||||||
</NavigationMenuItem>
|
</Command>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +186,7 @@ function ToolsDropdownList() {
|
|||||||
<BrushIcon /> <Tr>Clear tools</Tr>
|
<BrushIcon /> <Tr>Clear tools</Tr>
|
||||||
</CommandItem>
|
</CommandItem>
|
||||||
)}
|
)}
|
||||||
{ctx.templateTools.map((t, index) => (
|
{ctx.templateTools.map((t: TemplateTools, index: number) => (
|
||||||
<CommandItem
|
<CommandItem
|
||||||
key={index}
|
key={index}
|
||||||
value={t.toolsString}
|
value={t.toolsString}
|
||||||
@@ -251,7 +239,10 @@ function ChatTemplateDropdownList() {
|
|||||||
value={t.name}
|
value={t.name}
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
// Update chatStore with the selected template
|
// Update chatStore with the selected template
|
||||||
if (chatStore.history.length > 0 || chatStore.systemMessageContent) {
|
if (
|
||||||
|
chatStore.history.length > 0 ||
|
||||||
|
chatStore.systemMessageContent
|
||||||
|
) {
|
||||||
console.log("you clicked", t.name);
|
console.log("you clicked", t.name);
|
||||||
const confirm = window.confirm(
|
const confirm = window.confirm(
|
||||||
"This will replace the current chat history. Are you sure? "
|
"This will replace the current chat history. Are you sure? "
|
||||||
@@ -264,7 +255,9 @@ function ChatTemplateDropdownList() {
|
|||||||
setChatStore({
|
setChatStore({
|
||||||
...newChatStore({
|
...newChatStore({
|
||||||
...chatStore,
|
...chatStore,
|
||||||
...{ use_this_history: t.history ?? chatStore.history },
|
...{
|
||||||
|
use_this_history: t.history ?? chatStore.history,
|
||||||
|
},
|
||||||
...t,
|
...t,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
@@ -289,8 +282,6 @@ const APIListMenu: React.FC = () => {
|
|||||||
<div className="flex flex-col my-2 gap-2 w-full">
|
<div className="flex flex-col my-2 gap-2 w-full">
|
||||||
{ctx.templateTools.length > 0 && <ToolsDropdownList />}
|
{ctx.templateTools.length > 0 && <ToolsDropdownList />}
|
||||||
{ctx.templates.length > 0 && <ChatTemplateDropdownList />}
|
{ctx.templates.length > 0 && <ChatTemplateDropdownList />}
|
||||||
<NavigationMenu>
|
|
||||||
<NavigationMenuList>
|
|
||||||
{ctx.templateAPIs.length > 0 && (
|
{ctx.templateAPIs.length > 0 && (
|
||||||
<APIsDropdownList
|
<APIsDropdownList
|
||||||
label="Chat API"
|
label="Chat API"
|
||||||
@@ -323,8 +314,6 @@ const APIListMenu: React.FC = () => {
|
|||||||
keyField="image_gen_key"
|
keyField="image_gen_key"
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</NavigationMenuList>
|
|
||||||
</NavigationMenu>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user