import { useRecoilValue } from 'recoil'; import { useState, useRef } from 'react'; import { useParams } from 'react-router-dom'; import { useUpdateConversationMutation } from 'librechat-data-provider'; import type { MouseEvent, FocusEvent, KeyboardEvent } from 'react'; import { useConversations, useNavigateToConvo } from '~/hooks'; import { MinimalIcon } from '~/components/Endpoints'; import { NotificationSeverity } from '~/common'; import { useToastContext } from '~/Providers'; import DeleteButton from './NewDeleteButton'; import RenameButton from './RenameButton'; import store from '~/store'; type KeyEvent = KeyboardEvent; export default function Conversation({ conversation, retainView, toggleNav, i }) { const { conversationId: currentConvoId } = useParams(); const activeConvos = useRecoilValue(store.allConversationsSelector); const updateConvoMutation = useUpdateConversationMutation(currentConvoId ?? ''); const { refreshConversations } = useConversations(); const { navigateToConvo } = useNavigateToConvo(); const { showToast } = useToastContext(); const { conversationId, title } = conversation; const inputRef = useRef(null); const [titleInput, setTitleInput] = useState(title); const [renaming, setRenaming] = useState(false); const clickHandler = async () => { if (currentConvoId === conversationId) { return; } toggleNav(); // set document title document.title = title; // set conversation to the new conversation if (conversation?.endpoint === 'gptPlugins') { const lastSelectedTools = JSON.parse(localStorage.getItem('lastSelectedTools') ?? '') || []; navigateToConvo({ ...conversation, tools: lastSelectedTools }); } else { navigateToConvo(conversation); } }; const renameHandler = (e: MouseEvent) => { e.preventDefault(); setTitleInput(title); setRenaming(true); setTimeout(() => { if (!inputRef.current) { return; } inputRef.current.focus(); }, 25); }; const onRename = (e: MouseEvent | FocusEvent | KeyEvent) => { e.preventDefault(); setRenaming(false); if (titleInput === title) { return; } updateConvoMutation.mutate( { conversationId, title: titleInput }, { onSuccess: () => refreshConversations(), onError: () => { setTitleInput(title); showToast({ message: 'Failed to rename conversation', severity: NotificationSeverity.ERROR, showIcon: true, }); }, }, ); }; const icon = MinimalIcon({ size: 20, endpoint: conversation.endpoint, model: conversation.model, error: false, className: 'mr-0', isCreatedByUser: false, }); const handleKeyDown = (e: KeyEvent) => { if (e.key === 'Enter') { onRename(e); } }; const aProps = { className: 'animate-flash group relative flex cursor-pointer items-center gap-3 break-all rounded-md bg-gray-900 py-3 px-3 pr-14 hover:bg-gray-900', }; const activeConvo = currentConvoId === conversationId || (i === 0 && currentConvoId === 'new' && activeConvos[0] && activeConvos[0] !== 'new'); if (!activeConvo) { aProps.className = 'group relative flex cursor-pointer items-center gap-3 break-all rounded-md py-3 px-3 hover:bg-gray-900 hover:pr-4'; } return ( clickHandler()} {...aProps}> {icon}
{renaming === true ? ( setTitleInput(e.target.value)} onBlur={onRename} onKeyDown={handleKeyDown} /> ) : ( title )}
{activeConvo ? (
) : (
)} ); }