86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
import { useChatHandler } from "@/components/chat/chat-hooks/use-chat-handler"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger
|
|
} from "@/components/ui/dialog"
|
|
import { ChatbotUIContext } from "@/context/context"
|
|
import { deleteChat } from "@/db/chats"
|
|
import useHotkey from "@/lib/hooks/use-hotkey"
|
|
import { Tables } from "@/supabase/types"
|
|
import { IconTrash } from "@tabler/icons-react"
|
|
import { FC, useContext, useRef, useState } from "react"
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
interface DeleteChatProps {
|
|
chat: Tables<"chats">
|
|
}
|
|
|
|
export const DeleteChat: FC<DeleteChatProps> = ({ chat }) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
useHotkey("Backspace", () => setShowChatDialog(true))
|
|
|
|
const { setChats } = useContext(ChatbotUIContext)
|
|
const { handleNewChat } = useChatHandler()
|
|
|
|
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
|
|
const [showChatDialog, setShowChatDialog] = useState(false)
|
|
|
|
const handleDeleteChat = async () => {
|
|
await deleteChat(chat.id)
|
|
|
|
setChats(prevState => prevState.filter(c => c.id !== chat.id))
|
|
|
|
setShowChatDialog(false)
|
|
|
|
handleNewChat()
|
|
}
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
if (e.key === "Enter") {
|
|
buttonRef.current?.click()
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={showChatDialog} onOpenChange={setShowChatDialog}>
|
|
<DialogTrigger asChild>
|
|
<IconTrash className="hover:opacity-50" size={18} />
|
|
</DialogTrigger>
|
|
|
|
<DialogContent onKeyDown={handleKeyDown}>
|
|
<DialogHeader>
|
|
<DialogTitle>t("side.deleteChatTitle") {chat.name}</DialogTitle>
|
|
|
|
<DialogDescription>
|
|
{t("side.deleteChatConfirm")}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<DialogFooter>
|
|
<Button variant="ghost" onClick={() => setShowChatDialog(false)}>
|
|
{t("side.cancel")}
|
|
</Button>
|
|
|
|
<Button
|
|
ref={buttonRef}
|
|
variant="destructive"
|
|
onClick={handleDeleteChat}
|
|
>
|
|
{t("side.delete")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|