"use client"; import * as VisuallyHidden from "@radix-ui/react-visually-hidden"; import cx from "classnames"; import Link from "next/link"; import { useParams, usePathname } from "next/navigation"; import { User } from "next-auth"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import useSWR from "swr"; import { Chat } from "@/db/schema"; import { fetcher } from "@/lib/utils"; import { InfoIcon, MenuIcon, MoreHorizontalIcon, PencilEditIcon, TrashIcon, } from "./icons"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "../ui/alert-dialog"; import { Button } from "../ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "../ui/dropdown-menu"; import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, } from "../ui/sheet"; export const History = ({ user }: { user: User | undefined }) => { const { id } = useParams(); const pathname = usePathname(); const [isHistoryVisible, setIsHistoryVisible] = useState(false); const { data: history, isLoading, mutate, } = useSWR>(user ? "/api/history" : null, fetcher, { fallbackData: [], }); useEffect(() => { mutate(); }, [pathname, mutate]); const [deleteId, setDeleteId] = useState(null); const [showDeleteDialog, setShowDeleteDialog] = useState(false); const handleDelete = async () => { const deletePromise = fetch(`/api/chat?id=${deleteId}`, { method: "DELETE", }); toast.promise(deletePromise, { loading: "Deleting chat...", success: () => { mutate((history) => { if (history) { return history.filter((h) => h.id !== id); } }); return "Chat deleted successfully"; }, error: "Failed to delete chat", }); setShowDeleteDialog(false); }; return ( <> { setIsHistoryVisible(state); }} > History {history === undefined ? "loading" : history.length} chats
History
{history === undefined ? "loading" : history.length} chats
{user && ( )}
{!user ? (
Login to save and revisit previous chats!
) : null} {!isLoading && history?.length === 0 && user ? (
No chats found
) : null} {isLoading && user ? (
{[44, 32, 28, 52].map((item) => (
))}
) : null} {history && history.map((chat) => (
))}
Are you absolutely sure? This action cannot be undone. This will permanently delete your chat and remove it from our servers. Cancel Continue ); };