'use client' import React from 'react' import { cn, nanoid } from '@/lib/utils' import { useChatStore } from '@/lib/stores/chat-store' import { clearChats, deleteChat, setActiveChatId } from '@/lib/stores/chat-actions' import { IconPlus, IconTrash } from '@/components/ui/icons' import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' import { EditChatTitleDialog } from '@/components/edit-chat-title-dialog' import { useStore } from '@/lib/hooks/use-store' import { Button } from '@/components/ui/button' import { ListSkeleton } from '@/components/skeleton' import { Separator } from '@/components/ui/separator' import { ClearChatsButton } from './clear-chats-button' interface ChatSessionsProps { className?: string } export const ChatSessions = ({ className }: ChatSessionsProps) => { const _hasHydrated = useStore(useChatStore, state => state._hasHydrated) const chats = useStore(useChatStore, state => state.chats) const activeChatId = useStore(useChatStore, state => state.activeChatId) const onDeleteClick = ( e: React.MouseEvent, chatId: string ) => { deleteChat(chatId) } const onNewChatClick = (e: React.MouseEvent) => { setActiveChatId(nanoid()) } const handleClearChats = () => { clearChats() } return ( <>
{!_hasHydrated ? ( ) : ( <> {chats?.map(chat => { const isActive = activeChatId === chat.id return (
setActiveChatId(chat.id)} className={cn( 'hover:bg-accent flex cursor-pointer items-center justify-between gap-3 rounded-lg px-3 py-2 text-zinc-900 transition-all hover:text-zinc-900 dark:text-zinc-50 hover:dark:bg-zinc-900 dark:hover:text-zinc-50', isActive && '!bg-zinc-200 dark:!bg-zinc-800' )} > {chat.title || '(Untitled)'} {isActive && (
e.stopPropagation()} > Delete
)}
) })} )}
) }