| 
									
										
										
										
											2024-10-07 16:41:31 +02:00
										 |  |  | import React, { useState } from 'react'; | 
					
						
							| 
									
										
										
										
											2024-10-08 15:33:21 +02:00
										 |  |  | import { useChatHistory } from '../hooks/useChatHistory'; | 
					
						
							| 
									
										
										
										
											2024-10-07 16:41:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 15:33:21 +02:00
										 |  |  | const History: React.FC = () => { | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |   const [chatHistory, setSelectedIndex, setChatHistory] = useChatHistory() | 
					
						
							| 
									
										
										
										
											2024-10-08 16:47:40 +02:00
										 |  |  |   const [isEditing, setIsEditing] = useState(false); | 
					
						
							|  |  |  |   const [inputValue, setInputValue] = useState<string>(''); | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |   const [hoveredIndex, setHoveredIndex] = useState<number | null>(null) | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2024-10-09 14:01:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 16:47:40 +02:00
										 |  |  |   const handleEditButtonClick = () => { | 
					
						
							|  |  |  |     setIsEditing(true); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | 
					
						
							|  |  |  |     setInputValue(e.target.value); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleSaveButtonClick = () => { | 
					
						
							|  |  |  |     setIsEditing(false); | 
					
						
							|  |  |  |     chatHistory.chats.push({ name: inputValue, messages: [], timestamp: 5 }) | 
					
						
							|  |  |  |     setInputValue("") | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2024-10-07 16:41:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleHistoryClick = (index: number) => { | 
					
						
							|  |  |  |     setSelectedIndex(index) | 
					
						
							| 
									
										
										
										
											2024-10-08 16:47:40 +02:00
										 |  |  |     console.log("index",index); | 
					
						
							| 
									
										
										
										
											2024-10-07 16:41:31 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-09-18 10:03:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |   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
 | 
					
						
							| 
									
										
										
										
											2024-10-11 08:44:30 +02:00
										 |  |  |     copyChats.chats.splice(index,1)  | 
					
						
							| 
									
										
										
										
											2024-10-08 16:47:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |   // 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); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2024-09-18 10:03:36 +02:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <div className="history-background"> | 
					
						
							|  |  |  |       <div className="history"> | 
					
						
							|  |  |  |         <ul> | 
					
						
							| 
									
										
										
										
											2024-09-18 11:17:34 +02:00
										 |  |  |           {/* Populate with history items */} | 
					
						
							| 
									
										
										
										
											2024-10-08 15:33:21 +02:00
										 |  |  |           {chatHistory.chats.map((chats, index) => ( | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |             <li key={index} onMouseOver={()=>handleHistoryHover(index)} onMouseOut={handleHistoryNotHover} > | 
					
						
							| 
									
										
										
										
											2024-10-09 15:07:05 +02:00
										 |  |  |               <a href="#" onClick={() => handleHistoryClick(index)} style={{ | 
					
						
							|  |  |  |                 backgroundColor: chatHistory.selectedIndex == index ? "var(--input-button-color)" : "", | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |                 borderRadius: "5px", | 
					
						
							|  |  |  |                 width: index==hoveredIndex && chatHistory.chats.length >1 ? "85%":"100%" | 
					
						
							| 
									
										
										
										
											2024-10-09 15:07:05 +02:00
										 |  |  |               }}> | 
					
						
							| 
									
										
										
										
											2024-10-09 13:27:35 +02:00
										 |  |  |                 {chatHistory.chats[index].name} | 
					
						
							|  |  |  |               </a> | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |               <button id="delete-chat-button" | 
					
						
							|  |  |  |                 onClick={()=>handleHistoryDeletion(index)} | 
					
						
							|  |  |  |                 disabled={(index == hoveredIndex && chatHistory.chats.length >1) ? false : true} | 
					
						
							|  |  |  |                 style={{ | 
					
						
							|  |  |  |                   width: index == hoveredIndex && chatHistory.chats.length >1 ? "15%" : "0", | 
					
						
							|  |  |  |                   visibility: index == hoveredIndex && chatHistory.chats.length >1 ? "visible" : "hidden", | 
					
						
							|  |  |  |                   marginLeft: index == hoveredIndex && chatHistory.chats.length >1? "0.5em":0 | 
					
						
							|  |  |  |               }}> | 
					
						
							|  |  |  |                 <svg viewBox="0 0 448 512"><path d="M135.2 17.7L128 32 32 32C14.3 32 0 46.3 0 64S14.3 96 32 96l384 0c17.7 0 32-14.3 32-32s-14.3-32-32-32l-96 0-7.2-14.3C307.4 6.8 296.3 0 284.2 0L163.8 0c-12.1 0-23.2 6.8-28.6 17.7zM416 128L32 128 53.2 467c1.6 25.3 22.6 45 47.9 45l245.8 0c25.3 0 46.3-19.7 47.9-45L416 128z"/></svg> | 
					
						
							|  |  |  |               </button> | 
					
						
							| 
									
										
										
										
											2024-09-18 11:17:34 +02:00
										 |  |  |             </li> | 
					
						
							|  |  |  |           ))} | 
					
						
							| 
									
										
										
										
											2024-10-08 16:47:40 +02:00
										 |  |  |           <li> | 
					
						
							|  |  |  |             {isEditing ? ( | 
					
						
							| 
									
										
										
										
											2024-10-09 13:27:35 +02:00
										 |  |  |               <div className="input-container"> | 
					
						
							|  |  |  |                 <input | 
					
						
							|  |  |  |                   type="text" | 
					
						
							|  |  |  |                   value={inputValue} | 
					
						
							|  |  |  |                   onChange={handleInputChange} | 
					
						
							|  |  |  |                   placeholder="Enter text" | 
					
						
							|  |  |  |                   className="chat-input" | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |                   /> | 
					
						
							| 
									
										
										
										
											2024-10-09 13:27:35 +02:00
										 |  |  |                 <button onClick={handleSaveButtonClick} className="save-btn"> | 
					
						
							|  |  |  |                   Save | 
					
						
							|  |  |  |                 </button> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ) : ( | 
					
						
							|  |  |  |               <button onClick={handleEditButtonClick} className="newChat-btn"> | 
					
						
							|  |  |  |                 New Chat | 
					
						
							|  |  |  |               </button> | 
					
						
							|  |  |  |             )} | 
					
						
							| 
									
										
										
										
											2024-10-08 16:47:40 +02:00
										 |  |  |           </li> | 
					
						
							| 
									
										
										
										
											2024-09-18 11:17:34 +02:00
										 |  |  |         </ul> | 
					
						
							| 
									
										
										
										
											2024-09-18 10:03:36 +02:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2024-10-09 13:27:35 +02:00
										 |  |  |   );   | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-09-18 10:03:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-10 10:37:02 +02:00
										 |  |  |   export default History; |