import React, { useState } from 'react'; import { useChatHistory } from '../hooks/useChatHistory'; const History: React.FC = () => { const [chatHistory, setSelectedIndex, setChatHistory] = useChatHistory() const [isEditing, setIsEditing] = useState(false); const [inputValue, setInputValue] = useState(''); const [hoveredIndex, setHoveredIndex] = useState(null) const handleEditButtonClick = () => { setIsEditing(true); }; const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); }; const handleSaveButtonClick = () => { setIsEditing(false); chatHistory.chats.push({ name: inputValue, messages: [], timestamp: 5 }) setInputValue("") }; const handleHistoryClick = (index: number) => { setSelectedIndex(index) console.log("index",index); } const handleHistoryHover = (index:number) => { setHoveredIndex(index) } const handleHistoryNotHover = () => { setHoveredIndex(null) } console.log("chat length",chatHistory.chats.length) console.log("index",chatHistory.selectedIndex) const handleHistoryDeletion = (index: number) => { const currentIndex = chatHistory.selectedIndex; // Create a new copy of the current chat history const copyChats = { ...chatHistory }; copyChats.chats = [...chatHistory.chats] // Remove the chat at the specified index copyChats.chats.splice(index,1) // Determine new selectedIndex let newSelectedIndex = currentIndex; // Adjust selectedIndex based on the deletion if (index === currentIndex) { // If the deleted index is the currently selected one, reset the selected index newSelectedIndex = copyChats.chats.length > 0 ? (index > 0 ? index - 1 : 0) : -1; // Set to previous or first chat or -1 if no chats left } else if (index < currentIndex) { // If the deleted chat is before the current selected index, decrement the selected index newSelectedIndex = currentIndex - 1; } copyChats.selectedIndex = newSelectedIndex console.log(copyChats) // Set the updated chat history setChatHistory(copyChats); }; return (
); } export default History;