import { useEffect, useRef, useState } from 'react'; import { useSelector } from 'react-redux'; import Edit from '../assets/edit.svg'; import Exit from '../assets/exit.svg'; import Message from '../assets/message.svg'; import CheckMark from '../assets/checkmark.svg'; import Trash from '../assets/trash.svg'; import { selectConversationId } from '../preferences/preferenceSlice'; import { useOutsideAlerter } from '../hooks'; interface ConversationProps { name: string; id: string; } interface ConversationTileProps { conversation: ConversationProps; selectConversation: (arg1: string) => void; onDeleteConversation: (arg1: string) => void; onSave: ({ name, id }: ConversationProps) => void; } export default function ConversationTile({ conversation, selectConversation, onDeleteConversation, onSave, }: ConversationTileProps) { const conversationId = useSelector(selectConversationId); const tileRef = useRef(null); const [isEdit, setIsEdit] = useState(false); const [conversationName, setConversationsName] = useState(''); useOutsideAlerter( tileRef, () => handleSaveConversation({ id: conversationId || conversation.id, name: conversationName, }), [conversationName], ); useEffect(() => { setConversationsName(conversation.name); }, [conversation.name]); function handleEditConversation() { setIsEdit(true); } function handleSaveConversation(changedConversation: ConversationProps) { if (changedConversation.name.trim().length) { onSave(changedConversation); setIsEdit(false); } else { onClear(); } } function onClear() { setConversationsName(conversation.name); setIsEdit(false); } return (
{ selectConversation(conversation.id); }} className={`my-auto mx-4 mt-4 flex h-9 cursor-pointer items-center justify-between gap-4 rounded-3xl hover:bg-gray-100 ${ conversationId === conversation.id ? 'bg-gray-100' : '' }`} >
{isEdit ? ( setConversationsName(e.target.value)} /> ) : (

{conversationName}

)}
{conversationId === conversation.id ? (
Edit { event.stopPropagation(); isEdit ? handleSaveConversation({ id: conversationId, name: conversationName, }) : handleEditConversation(); }} /> Exit { event.stopPropagation(); isEdit ? onClear() : onDeleteConversation(conversation.id); }} />
) : null}
); }