Refactor ListToolsTemplates component to use NavigationMenu for improved navigation; enhance template management with Edit and Delete buttons

This commit is contained in:
ecwu
2024-12-21 12:51:07 +08:00
parent 1656e16c7c
commit e8f0c0ffa5
3 changed files with 489 additions and 453 deletions

View File

@@ -1,14 +1,16 @@
import { ChatStore, TemplateAPI } from "@/types/chatstore";
import { Tr } from "@/translate";
import { Card, CardHeader, CardTitle, CardFooter } from "@/components/ui/card";
import {
Carousel,
CarouselContent,
CarouselItem,
CarouselNext,
CarouselPrevious,
} from "@/components/ui/carousel";
NavigationMenu,
NavigationMenuContent,
NavigationMenuIndicator,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
NavigationMenuViewport,
} from "@/components/ui/navigation-menu";
import { Button } from "./components/ui/button";
import { cn } from "@/lib/utils";
@@ -31,27 +33,14 @@ export function ListAPIs({
keyField,
}: Props) {
return (
<div className="p-3 space-y-4">
<h2 className="text-2xl font-semibold">
{Tr(`Saved ${label} templates`)}
</h2>
<Carousel className="w-full">
<CarouselContent>
<NavigationMenuItem>
<NavigationMenuTrigger>{label}</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid w-[400px] gap-3 p-4 md:w-[500px] md:grid-cols-2 lg:w-[600px] ">
{tmps.map((t, index) => (
<CarouselItem key={index} className="md:basis-1/4 lg:basis-1/6">
<div className="p-1">
<Card
className={cn(
"cursor-pointer transition-colors",
chatStore[apiField as keyof ChatStore] === t.endpoint &&
chatStore[keyField as keyof ChatStore] === t.key
? "bg-primary/10"
: ""
)}
>
<CardHeader>
<CardTitle
className="text-center"
<li>
<NavigationMenuLink asChild>
<a
onClick={() => {
// @ts-ignore
chatStore[apiField] = t.endpoint;
@@ -59,18 +48,28 @@ export function ListAPIs({
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",
chatStore[apiField as keyof ChatStore] === t.endpoint &&
chatStore[keyField as keyof ChatStore] === t.key
? "bg-accent text-accent-foreground"
: ""
)}
>
<div className="text-sm font-medium leading-none">
{t.name}
</CardTitle>
</CardHeader>
<CardFooter className="flex justify-center gap-4">
</div>
<p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
{label === "API" ? t.endpoint : t.key}
</p>
</a>
</NavigationMenuLink>
<div className="mt-2 flex justify-between">
<Button
variant="ghost"
size="sm"
onClick={() => {
const name = prompt(
`Give **${label}** template a name`
);
const name = prompt(`Give **${label}** template a name`);
if (!name) return;
t.name = name;
setTmps(structuredClone(tmps));
@@ -95,15 +94,55 @@ export function ListAPIs({
>
Delete
</Button>
</CardFooter>
</Card>
</div>
</CarouselItem>
</li>
))}
</CarouselContent>
<CarouselPrevious />
<CarouselNext />
</Carousel>
</div>
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
// <div className="p-3 space-y-4">
// <h2 className="text-2xl font-semibold">
// {Tr(`Saved ${label} templates`)}
// </h2>
// <Carousel className="w-full">
// <CarouselContent>
// {tmps.map((t, index) => (
// <CarouselItem key={index} className="md:basis-1/4 lg:basis-1/6">
// <div className="p-1">
// <Card
// className={cn(
// "cursor-pointer transition-colors",
// chatStore[apiField as keyof ChatStore] === t.endpoint &&
// chatStore[keyField as keyof ChatStore] === t.key
// ? "bg-primary/10"
// : ""
// )}
// >
// <CardHeader>
// <CardTitle
// className="text-center"
// onClick={() => {
// // @ts-ignore
// chatStore[apiField] = t.endpoint;
// // @ts-ignore
// chatStore[keyField] = t.key;
// setChatStore({ ...chatStore });
// }}
// >
// {t.name}
// </CardTitle>
// </CardHeader>
// <CardFooter className="flex justify-center gap-4">
// </CardFooter>
// </Card>
// </div>
// </CarouselItem>
// ))}
// </CarouselContent>
// <CarouselPrevious />
// <CarouselNext />
// </Carousel>
// </div>
);
}

View File

@@ -1,13 +1,16 @@
import { ChatStore, TemplateTools } from "@/types/chatstore";
import { Tr } from "@/translate";
import { Card, CardContent } from "@/components/ui/card";
import {
Carousel,
CarouselContent,
CarouselItem,
CarouselNext,
CarouselPrevious,
} from "@/components/ui/carousel";
NavigationMenu,
NavigationMenuContent,
NavigationMenuIndicator,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
NavigationMenuViewport,
} from "@/components/ui/navigation-menu";
import { cn } from "@/lib/utils";
import { Button } from "./components/ui/button";
interface Props {
@@ -23,8 +26,8 @@ export function ListToolsTempaltes({
setChatStore,
}: Props) {
return (
<div className="p-3">
<h2 className="text-lg font-semibold mb-4 flex items-center">
<NavigationMenuItem className="p-3">
<NavigationMenuTrigger>
<span>{Tr(`Saved tools templates`)}</span>
<Button
variant="link"
@@ -36,36 +39,38 @@ export function ListToolsTempaltes({
>
{Tr(`Clear`)}
</Button>
</h2>
<Carousel className="w-full">
<CarouselContent>
</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid w-[400px] gap-3 p-4 md:w-[500px] md:grid-cols-2 lg:w-[600px]">
{templateTools.map((t, index) => (
<CarouselItem key={index} className="md:basis-1/2 lg:basis-1/3">
<div className="p-1">
<Card
className={`cursor-pointer ${
chatStore.toolsString === t.toolsString
? "border-primary"
: ""
}`}
<li key={index}>
<NavigationMenuLink asChild>
<a
onClick={() => {
chatStore.toolsString = t.toolsString;
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",
chatStore.toolsString === t.toolsString
? "bg-accent text-accent-foreground"
: ""
)}
>
<CardContent className="p-4">
<div className="flex flex-col gap-2">
<span className="font-medium text-center">{t.name}</span>
<div className="flex justify-between mt-2">
<div className="text-sm font-medium leading-none">
{t.name}
</div>
<p className="line-clamp-2 text-sm leading-snug text-muted-foreground">
{t.toolsString}
</p>
</a>
</NavigationMenuLink>
<div className="mt-2 flex justify-between">
<Button
variant="ghost"
size="sm"
className="text-blue-500 hover:text-blue-600"
onClick={(e) => {
e.stopPropagation();
const name = prompt(
`Give **tools** template a name`
);
onClick={() => {
const name = prompt(`Give **tools** template a name`);
if (!name) return;
t.name = name;
setTemplateTools(structuredClone(templateTools));
@@ -76,15 +81,14 @@ export function ListToolsTempaltes({
<Button
variant="ghost"
size="sm"
className="text-red-500 hover:text-red-600"
onClick={(e) => {
e.stopPropagation();
onClick={() => {
if (
!confirm(
`Are you sure to delete this **tools** template?`
)
)
) {
return;
}
templateTools.splice(index, 1);
setTemplateTools(structuredClone(templateTools));
}}
@@ -92,16 +96,10 @@ export function ListToolsTempaltes({
Delete
</Button>
</div>
</div>
</CardContent>
</Card>
</div>
</CarouselItem>
</li>
))}
</CarouselContent>
<CarouselPrevious />
<CarouselNext />
</Carousel>
</div>
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
);
}

View File

@@ -51,13 +51,16 @@ import {
KeyIcon,
} from "lucide-react";
import { Switch } from "@/components/ui/switch";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import {
NavigationMenu,
NavigationMenuContent,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
navigationMenuTriggerStyle,
} from "@/components/ui/navigation-menu";
export default function ChatBOX(props: {
db: Promise<IDBPDatabase<ChatStore>>;
@@ -453,7 +456,8 @@ export default function ChatBOX(props: {
const userInputRef = createRef();
return (
<div className="grow flex flex-col p-2 w-full">
<>
<div className="flex flex-col w-full">
{true && (
<Settings
chatStore={chatStore}
@@ -482,7 +486,7 @@ export default function ChatBOX(props: {
setShow={setShowSearch}
/>
)}
<ChatMessageList>
{!chatStore.apiKey && (
<Alert>
<KeyIcon className="h-4 w-4" />
@@ -501,10 +505,8 @@ export default function ChatBOX(props: {
</AlertDescription>
</Alert>
)}
<Accordion type="single" collapsible className="w-full">
<AccordionItem value="item-1">
<AccordionTrigger>Saved Presets</AccordionTrigger>
<AccordionContent>
<NavigationMenu>
<NavigationMenuList>
{templateAPIs.length > 0 && (
<ListAPIs
label="API"
@@ -516,7 +518,6 @@ export default function ChatBOX(props: {
keyField="apiKey"
/>
)}
{templateAPIsWhisper.length > 0 && (
<ListAPIs
label="Whisper API"
@@ -528,7 +529,6 @@ export default function ChatBOX(props: {
keyField="whisper_key"
/>
)}
{templateAPIsTTS.length > 0 && (
<ListAPIs
label="TTS API"
@@ -540,7 +540,6 @@ export default function ChatBOX(props: {
keyField="tts_key"
/>
)}
{templateAPIsImageGen.length > 0 && (
<ListAPIs
label="Image Gen API"
@@ -552,7 +551,6 @@ export default function ChatBOX(props: {
keyField="image_gen_key"
/>
)}
{toolsTemplates.length > 0 && (
<ListToolsTempaltes
templateTools={toolsTemplates}
@@ -561,10 +559,11 @@ export default function ChatBOX(props: {
setChatStore={setChatStore}
/>
)}
</AccordionContent>
</AccordionItem>
</Accordion>
</NavigationMenuList>
</NavigationMenu>
</div>
<div className="grow flex flex-col p-2 w-full">
<ChatMessageList>
{chatStore.history.filter((msg) => !msg.example).length == 0 && (
<div className="bg-base-200 break-all p-3 my-3 text-left">
<h2>
@@ -617,7 +616,6 @@ export default function ChatBOX(props: {
</div>
</div>
)}
{chatStore.history.map((_, messageIndex) => (
<Message
chatStore={chatStore}
@@ -789,5 +787,6 @@ export default function ChatBOX(props: {
)}
</div>
</div>
</>
);
}