Refactor ListAPIs and ListToolsTemplates components to use Card and Carousel for improved UI; enhance template management with Edit and Delete buttons
This commit is contained in:
@@ -1,6 +1,17 @@
|
||||
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";
|
||||
import { Button } from "./components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface Props {
|
||||
chatStore: ChatStore;
|
||||
setChatStore: (cs: ChatStore) => void;
|
||||
@@ -20,20 +31,27 @@ export function ListAPIs({
|
||||
keyField,
|
||||
}: Props) {
|
||||
return (
|
||||
<div className="break-all opacity-80 p-3 rounded base-200 my-3 text-left">
|
||||
<h2>{Tr(`Saved ${label} templates`)}</h2>
|
||||
<hr className="my-2" />
|
||||
<div className="flex flex-wrap">
|
||||
<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) => (
|
||||
<div
|
||||
className={`cursor-pointer rounded ${
|
||||
// @ts-ignore
|
||||
chatStore[apiField] === t.endpoint &&
|
||||
// @ts-ignore
|
||||
chatStore[keyField] === t.key
|
||||
? "bg-info"
|
||||
: "bg-base-300"
|
||||
} w-fit p-2 m-1 flex flex-col`}
|
||||
<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;
|
||||
@@ -42,27 +60,31 @@ export function ListAPIs({
|
||||
setChatStore({ ...chatStore });
|
||||
}}
|
||||
>
|
||||
<span className="w-full text-center">{t.name}</span>
|
||||
<span className="flex justify-between gap-x-2">
|
||||
<button
|
||||
className="link"
|
||||
{t.name}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardFooter className="flex justify-center gap-4">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
const name = prompt(`Give **${label}** template a name`);
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
const name = prompt(
|
||||
`Give **${label}** template a name`
|
||||
);
|
||||
if (!name) return;
|
||||
t.name = name;
|
||||
setTmps(structuredClone(tmps));
|
||||
}}
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
<button
|
||||
className="link"
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
if (
|
||||
!confirm(
|
||||
`Are you sure to delete this **${label}** template?`,
|
||||
`Are you sure to delete this **${label}** template?`
|
||||
)
|
||||
) {
|
||||
return;
|
||||
@@ -72,11 +94,16 @@ export function ListAPIs({
|
||||
}}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</span>
|
||||
</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</div>
|
||||
</CarouselItem>
|
||||
))}
|
||||
</div>
|
||||
</CarouselContent>
|
||||
<CarouselPrevious />
|
||||
<CarouselNext />
|
||||
</Carousel>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
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";
|
||||
import { Button } from "./components/ui/button";
|
||||
|
||||
interface Props {
|
||||
templateTools: TemplateTools[];
|
||||
@@ -14,66 +23,85 @@ export function ListToolsTempaltes({
|
||||
setChatStore,
|
||||
}: Props) {
|
||||
return (
|
||||
<div className="break-all opacity-80 p-3 rounded bg-white my-3 text-left dark:text-black">
|
||||
<h2>
|
||||
<div className="p-3">
|
||||
<h2 className="text-lg font-semibold mb-4 flex items-center">
|
||||
<span>{Tr(`Saved tools templates`)}</span>
|
||||
<button
|
||||
className="mx-2 underline cursor-pointer"
|
||||
<Button
|
||||
variant="link"
|
||||
className="ml-2 text-sm"
|
||||
onClick={() => {
|
||||
chatStore.toolsString = "";
|
||||
setChatStore({ ...chatStore });
|
||||
}}
|
||||
>
|
||||
{Tr(`Clear`)}
|
||||
</button>
|
||||
</Button>
|
||||
</h2>
|
||||
<hr className="my-2" />
|
||||
<div className="flex flex-wrap">
|
||||
<Carousel className="w-full">
|
||||
<CarouselContent>
|
||||
{templateTools.map((t, index) => (
|
||||
<div
|
||||
className={`cursor-pointer rounded ${
|
||||
<CarouselItem key={index} className="md:basis-1/2 lg:basis-1/3">
|
||||
<div className="p-1">
|
||||
<Card
|
||||
className={`cursor-pointer ${
|
||||
chatStore.toolsString === t.toolsString
|
||||
? "bg-info"
|
||||
: "bg-base-300"
|
||||
} w-fit p-2 m-1 flex flex-col`}
|
||||
? "border-primary"
|
||||
: ""
|
||||
}`}
|
||||
onClick={() => {
|
||||
chatStore.toolsString = t.toolsString;
|
||||
setChatStore({ ...chatStore });
|
||||
}}
|
||||
>
|
||||
<span className="w-full text-center">{t.name}</span>
|
||||
<span className="flex justify-between gap-x-2">
|
||||
<button
|
||||
className="link"
|
||||
onClick={() => {
|
||||
const name = prompt(`Give **tools** template a name`);
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
<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">
|
||||
<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`
|
||||
);
|
||||
if (!name) return;
|
||||
t.name = name;
|
||||
setTemplateTools(structuredClone(templateTools));
|
||||
}}
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
<button
|
||||
className="link"
|
||||
onClick={() => {
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="text-red-500 hover:text-red-600"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (
|
||||
!confirm(`Are you sure to delete this **tools** template?`)
|
||||
) {
|
||||
!confirm(
|
||||
`Are you sure to delete this **tools** template?`
|
||||
)
|
||||
)
|
||||
return;
|
||||
}
|
||||
templateTools.splice(index, 1);
|
||||
setTemplateTools(structuredClone(templateTools));
|
||||
}}
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</CarouselItem>
|
||||
))}
|
||||
</div>
|
||||
</CarouselContent>
|
||||
<CarouselPrevious />
|
||||
<CarouselNext />
|
||||
</Carousel>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -44,8 +44,20 @@ import { Textarea } from "@/components/ui/textarea";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ChatInput } from "@/components/ui/chat/chat-input";
|
||||
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
|
||||
import { CornerDownLeftIcon, ImageIcon } from "lucide-react";
|
||||
import {
|
||||
CornerDownLeftIcon,
|
||||
GlobeIcon,
|
||||
ImageIcon,
|
||||
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";
|
||||
|
||||
export default function ChatBOX(props: {
|
||||
db: Promise<IDBPDatabase<ChatStore>>;
|
||||
@@ -472,15 +484,27 @@ export default function ChatBOX(props: {
|
||||
)}
|
||||
<ChatMessageList>
|
||||
{!chatStore.apiKey && (
|
||||
<p className="bg-base-200 p-6 rounded my-3 text-left">
|
||||
<Alert>
|
||||
<KeyIcon className="h-4 w-4" />
|
||||
<AlertTitle>Heads up!</AlertTitle>
|
||||
<AlertDescription>
|
||||
{Tr("Please click above to set")} (OpenAI) API KEY
|
||||
</p>
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
{!chatStore.apiEndpoint && (
|
||||
<p className="bg-base-200 p-6 rounded my-3 text-left">
|
||||
<Alert>
|
||||
<GlobeIcon className="h-4 w-4" />
|
||||
<AlertTitle>Heads up!</AlertTitle>
|
||||
<AlertDescription>
|
||||
{Tr("Please click above to set")} API Endpoint
|
||||
</p>
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
<Accordion type="single" collapsible className="w-full">
|
||||
<AccordionItem value="item-1">
|
||||
<AccordionTrigger>Saved Presets</AccordionTrigger>
|
||||
<AccordionContent>
|
||||
{templateAPIs.length > 0 && (
|
||||
<ListAPIs
|
||||
label="API"
|
||||
@@ -537,6 +561,9 @@ export default function ChatBOX(props: {
|
||||
setChatStore={setChatStore}
|
||||
/>
|
||||
)}
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
|
||||
{chatStore.history.filter((msg) => !msg.example).length == 0 && (
|
||||
<div className="bg-base-200 break-all p-3 my-3 text-left">
|
||||
|
||||
Reference in New Issue
Block a user