import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { PageAssitDatabase, formatToChatHistory, formatToMessage, deleteByHistoryId, updateHistory } from "~libs/db" import { Dropdown, Empty, Skeleton, Spin } from "antd" import { useMessageOption } from "~hooks/useMessageOption" import { Trash } from "~icons/Trash" import { Fragment, useState } from "react" import { PencilIcon } from "~icons/PencilIcon" import { EllipsisHorizontalIcon } from "~icons/EllipsisHorizontalIcon" import { Menu, Transition } from "@headlessui/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) => (
))}
)}
) }