import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { PageAssitDatabase, formatToChatHistory, formatToMessage, deleteByHistoryId, updateHistory } from "~/libs/db" import { Empty, Skeleton } from "antd" import { useMessageOption } from "~/hooks/useMessageOption" import { useState } from "react" import { PencilIcon, Trash2 } from "lucide-react" import { useNavigate } from "react-router-dom" type Props = { onClose: () => void } export const Sidebar = ({ onClose }: Props) => { const { setMessages, setHistory, setHistoryId, historyId, clearChat } = useMessageOption() 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 (