From be0da443ef4deaae5a5b0b4bd1015b85f78564b0 Mon Sep 17 00:00:00 2001 From: Mehdi Essoussi Date: Mon, 9 Dec 2024 11:03:38 -0500 Subject: [PATCH] Added delete-all-chats button that deletes all chats for the user --- app/api/chat/google/route.ts | 1 - components/chat/chat-helpers/index.ts | 7 +- .../sidebar/items/chat/delete-all-chats.tsx | 73 +++++++++++++++++++ components/utility/profile-settings.tsx | 12 ++- db/chats.ts | 10 +++ db/files.ts | 5 +- lib/build-prompt.ts | 54 +++++++------- lib/chat-setting-limits.ts | 2 +- lib/models/llm/google-llm-list.ts | 7 +- 9 files changed, 136 insertions(+), 35 deletions(-) create mode 100644 components/sidebar/items/chat/delete-all-chats.tsx diff --git a/app/api/chat/google/route.ts b/app/api/chat/google/route.ts index ad79139646..cb9820ee88 100644 --- a/app/api/chat/google/route.ts +++ b/app/api/chat/google/route.ts @@ -44,7 +44,6 @@ export async function POST(request: Request) { return new Response(readableStream, { headers: { "Content-Type": "text/plain" } }) - } catch (error: any) { let errorMessage = error.message || "An unexpected error occurred" const errorCode = error.status || 500 diff --git a/components/chat/chat-helpers/index.ts b/components/chat/chat-helpers/index.ts index 17a2089638..ec4a548cdc 100644 --- a/components/chat/chat-helpers/index.ts +++ b/components/chat/chat-helpers/index.ts @@ -208,9 +208,12 @@ export const handleHostedChat = async ( let draftMessages = await buildFinalMessages(payload, profile, chatImages) - let formattedMessages : any[] = [] + let formattedMessages: any[] = [] if (provider === "google") { - formattedMessages = await adaptMessagesForGoogleGemini(payload, draftMessages) + formattedMessages = await adaptMessagesForGoogleGemini( + payload, + draftMessages + ) } else { formattedMessages = draftMessages } diff --git a/components/sidebar/items/chat/delete-all-chats.tsx b/components/sidebar/items/chat/delete-all-chats.tsx new file mode 100644 index 0000000000..f1bd467edf --- /dev/null +++ b/components/sidebar/items/chat/delete-all-chats.tsx @@ -0,0 +1,73 @@ +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 { deleteAllChats } from "@/db/chats" +import { Tables } from "@/supabase/types" +import { IconTrash } from "@tabler/icons-react" +import { FC, useContext, useRef, useState } from "react" +import { toast } from "sonner" + +interface DeleteAllChatsProps { + profile: Tables<"profiles"> +} + +export const DeleteAllChats: FC = ({ profile }) => { + const { setChats, setChatMessages, setSelectedChat } = + useContext(ChatbotUIContext) + const { handleNewChat } = useChatHandler() + + const buttonRef = useRef(null) + const [showChatDialog, setShowChatDialog] = useState(false) + + const handleDeleteAllChats = async () => { + if (!profile) return + + await deleteAllChats(profile.user_id) + + setChats([]) + setChatMessages([]) + setSelectedChat(null) + + handleNewChat() + toast.success("All chats deleted!") + } + + return ( + + + + + + + + Delete All Chats + + Are you sure you want to delete all chats? + + + + + + + + + + ) +} diff --git a/components/utility/profile-settings.tsx b/components/utility/profile-settings.tsx index 576cee0eb3..4ca30817f4 100644 --- a/components/utility/profile-settings.tsx +++ b/components/utility/profile-settings.tsx @@ -42,6 +42,9 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs" import { TextareaAutosize } from "../ui/textarea-autosize" import { WithTooltip } from "../ui/with-tooltip" import { ThemeSwitcher } from "./theme-switcher" +import { deleteAllChats } from "@/db/chats" +import { useChatHandler } from "../chat/chat-hooks/use-chat-handler" +import { DeleteAllChats } from "../sidebar/items/chat/delete-all-chats" interface ProfileSettingsProps {} @@ -52,11 +55,16 @@ export const ProfileSettings: FC = ({}) => { envKeyMap, setAvailableHostedModels, setAvailableOpenRouterModels, - availableOpenRouterModels + availableOpenRouterModels, + setChats, + setChatMessages, + setSelectedChat } = useContext(ChatbotUIContext) const router = useRouter() + const { handleNewChat } = useChatHandler() + const buttonRef = useRef(null) const [isOpen, setIsOpen] = useState(false) @@ -747,6 +755,8 @@ export const ProfileSettings: FC = ({}) => { /> + +