import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { PageAssitDatabase, formatToChatHistory, formatToMessage, deleteByHistoryId, updateHistory } from "@/db" import { Empty, Skeleton } from "antd" import { useMessageOption } from "~/hooks/useMessageOption" import { PencilIcon, Trash2 } from "lucide-react" import { useNavigate } from "react-router-dom" import { useTranslation } from "react-i18next" type Props = { onClose: () => void } export const Sidebar = ({ onClose }: Props) => { const { setMessages, setHistory, setHistoryId, historyId, clearChat } = useMessageOption() const { t } = useTranslation(["option", "common"]) const client = useQueryClient() const navigate = useNavigate() const { data: chatHistories, status } = useQuery({ queryKey: ["fetchChatHistory"], queryFn: async () => { const db = new PageAssitDatabase() const history = await db.getChatHistories() return history } }) const { mutate: deleteHistory } = useMutation({ mutationKey: ["deleteHistory"], mutationFn: deleteByHistoryId, onSuccess: (history_id) => { client.invalidateQueries({ queryKey: ["fetchChatHistory"] }) if (historyId === history_id) { clearChat() } } }) const { mutate: editHistory } = useMutation({ mutationKey: ["editHistory"], mutationFn: async (data: { id: string; title: string }) => { return await updateHistory(data.id, data.title) }, onSuccess: () => { client.invalidateQueries({ queryKey: ["fetchChatHistory"] }) } }) return (
{status === "success" && chatHistories.length === 0 && (
)} {status === "pending" && (
)} {status === "error" && (
Error loading history
)} {status === "success" && chatHistories.length > 0 && (
{chatHistories.map((chat, index) => (
))}
)}
) }