import { IconCheck, IconMessage, IconPencil, IconTrash, IconX, } from '@tabler/icons-react'; import { DragEvent, KeyboardEvent, MouseEventHandler, useContext, useEffect, useState, } from 'react'; import { Conversation } from '@/types/chat'; import HomeContext from '@/pages/api/home/home.context'; import SidebarActionButton from '@/components/Buttons/SidebarActionButton'; import ChatbarContext from '@/components/Chatbar/Chatbar.context'; interface Props { conversation: Conversation; } export const ConversationComponent = ({ conversation }: Props) => { const { state: { selectedConversation, messageIsStreaming }, handleSelectConversation, handleUpdateConversation, } = useContext(HomeContext); const { handleDeleteConversation } = useContext(ChatbarContext); const [isDeleting, setIsDeleting] = useState(false); const [isRenaming, setIsRenaming] = useState(false); const [renameValue, setRenameValue] = useState(''); const handleEnterDown = (e: KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault(); selectedConversation && handleRename(selectedConversation); } }; const handleDragStart = ( e: DragEvent, conversation: Conversation, ) => { if (e.dataTransfer) { e.dataTransfer.setData('conversation', JSON.stringify(conversation)); } }; const handleRename = (conversation: Conversation) => { if (renameValue.trim().length > 0) { handleUpdateConversation(conversation, { key: 'name', value: renameValue, }); setRenameValue(''); setIsRenaming(false); } }; const handleConfirm: MouseEventHandler = (e) => { e.stopPropagation(); if (isDeleting) { handleDeleteConversation(conversation); } else if (isRenaming) { handleRename(conversation); } setIsDeleting(false); setIsRenaming(false); }; const handleCancel: MouseEventHandler = (e) => { e.stopPropagation(); setIsDeleting(false); setIsRenaming(false); }; const handleOpenRenameModal: MouseEventHandler = (e) => { e.stopPropagation(); setIsRenaming(true); selectedConversation && setRenameValue(selectedConversation.name); }; const handleOpenDeleteModal: MouseEventHandler = (e) => { e.stopPropagation(); setIsDeleting(true); }; useEffect(() => { if (isRenaming) { setIsDeleting(false); } else if (isDeleting) { setIsRenaming(false); } }, [isRenaming, isDeleting]); return (
{isRenaming && selectedConversation?.id === conversation.id ? (
setRenameValue(e.target.value)} onKeyDown={handleEnterDown} autoFocus />
) : ( )} {(isDeleting || isRenaming) && selectedConversation?.id === conversation.id && (
)} {selectedConversation?.id === conversation.id && !isDeleting && !isRenaming && (
)}
); };