Created
July 29, 2022 01:33
-
-
Save RichardSPrins/ec25a3d54112b2d39f213f2069c395ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { HiTrash } from 'react-icons/hi'; | |
import { useLongPress } from '../lib/hooks'; | |
import ChatHeadshot from './ChatHeadshot'; | |
import DeleteConversationModal from './DeleteConversationModal'; | |
import { useDeviceDetect } from '../lib/hooks'; | |
import { killEvent } from '../lib/utils'; | |
type ConversationPreviewProps = { | |
conversation: any; | |
shouldStopLongPress?: boolean; | |
loadConversation: (conversation: any) => void; | |
clearConversation: () => void; | |
}; | |
export default function ConversationPreview({ | |
conversation, | |
shouldStopLongPress = false, | |
loadConversation, | |
clearConversation, | |
}: ConversationPreviewProps) { | |
const { isMobile } = useDeviceDetect(); | |
const [unreadMessageCount, setUnreadMessageCount] = React.useState<number>(0); | |
const [_showDelete, setShowDelete] = React.useState(false); | |
const [deleteModalOpen, setDeleteModalOpen] = React.useState(false); | |
const handleDeleteConversation = () => { | |
setDeleteModalOpen(true); | |
}; | |
const defaultOptions = { | |
shouldPreventDefault: true, | |
delay: 700, | |
}; | |
console.log(isMobile); | |
React.useEffect(() => { | |
setUnreadMessageCount(conversation.unreadMessageCount); | |
}, [conversation]); | |
const selectConversation = () => { | |
loadConversation(conversation); | |
setUnreadMessageCount(0); | |
}; | |
const longPressEvent = useLongPress(handleDeleteConversation, selectConversation, defaultOptions); | |
const messagePreviewText = () => { | |
switch (conversation.lastMessage.type) { | |
case 'text': | |
return conversation.lastMessage.text; | |
case 'video': | |
case 'audio': | |
case 'image': | |
case 'file': | |
case 'media': | |
return 'Media Message'; | |
case 'meeting': | |
return conversation.data?.customData?.meetingURL; | |
default: | |
return ' '; | |
} | |
}; | |
return ( | |
<> | |
<div | |
{...(!shouldStopLongPress && { ...longPressEvent })} | |
key={conversation.id} | |
onMouseOver={() => setShowDelete(true)} | |
onMouseLeave={() => setShowDelete(false)} | |
onClick={selectConversation} | |
className="relative flex flex-col cursor-pointer p-2 border-b-2 border-gray hover:bg-gray-50" | |
> | |
<div className="flex items-center"> | |
<ChatHeadshot | |
size="small" | |
image={conversation?.conversationWith?.avatar} | |
userId={conversation?.conversationWith?.uid} | |
/> | |
<div className="overflow-hidden"> | |
<p className="m-0 ml-4 text-lg">{conversation?.conversationWith?.name}</p> | |
{conversation?.lastMessage && <p className="m-0 ml-4 truncate">{messagePreviewText()}</p>} | |
</div> | |
{unreadMessageCount > 0 && ( | |
<div className="ml-auto text-2xs bg-blue-600 rounded-full text-white flex items-center justify-center p-1"></div> | |
)} | |
</div> | |
</div> | |
<DeleteConversationModal | |
isOpen={deleteModalOpen} | |
setIsOpen={setDeleteModalOpen} | |
conversation={conversation} | |
deleteConversation={clearConversation} | |
/> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment