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" type Props = {} export const Sidebar = ({}: Props) => { const { setMessages, setHistory, setHistoryId, historyId, clearChat } = useMessageOption() const [processingId, setProcessingId] = useState("") const client = useQueryClient() const { data: chatHistories, status } = useQuery({ queryKey: ["fetchChatHistory"], queryFn: async () => { const db = new PageAssitDatabase() const history = await db.getChatHistories() return history } }) const { isPending: isDeleting, mutate: deleteHistory } = useMutation({ mutationKey: ["deleteHistory"], mutationFn: deleteByHistoryId, onSuccess: (history_id) => { client.invalidateQueries({ queryKey: ["fetchChatHistory"] }) setProcessingId("") if (historyId === history_id) { clearChat() } } }) const { isPending: isEditing, mutate: editHistory } = useMutation({ mutationKey: ["editHistory"], mutationFn: async (data: { id: string; title: string }) => { return await updateHistory(data.id, data.title) }, onSuccess: () => { client.invalidateQueries({ queryKey: ["fetchChatHistory"] }) setProcessingId("") } }) return (
{status === "success" && chatHistories.length === 0 && (
)} {status === "pending" && (
)} {status === "error" && (
Error loading history
)} {status === "success" && chatHistories.length > 0 && (
{chatHistories.map((chat, index) => (
))}
)}
) }