-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grouping messages #11
Changes from 4 commits
feee3df
7d12ed9
035c6e6
f5114fa
b2c038f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { Message as ChatKittyMessage, isFileMessage, isTextMessage, isUserMessage } from 'chatkitty'; | ||
import { | ||
Message as ChatKittyMessage, | ||
isFileMessage, | ||
isTextMessage, | ||
isUserMessage, | ||
} from 'chatkitty'; | ||
import moment from 'moment'; | ||
import { ChatAppContext } from 'providers/ChatAppProvider'; | ||
import React, { ReactElement, useContext, useEffect, useState } from 'react'; | ||
|
@@ -14,118 +19,167 @@ import { useHover } from 'react-chat-ui-kit'; | |
|
||
import replyIcon from '../assets/images/reply-icon.png'; | ||
|
||
|
||
import Message from './Message'; | ||
import PopupEmojiWindow from './PopupEmojiWindow'; | ||
import Reactions from './Reactions'; | ||
|
||
interface MessageListItemProps { | ||
message: ChatKittyMessage; | ||
avatar: ReactElement; | ||
index?: number; | ||
} | ||
|
||
|
||
|
||
const MessageListItem: React.FC<MessageListItemProps> = ({ | ||
message, | ||
avatar, | ||
index, | ||
}: MessageListItemProps) => { | ||
const sender: { displayName: string } = isUserMessage(message) | ||
? message.user | ||
: { | ||
displayName: 'ChatKitty', | ||
}; | ||
|
||
const [isHovering, hoverProps] = useHover({ mouseEnterDelayMS: 0 }); | ||
const {changeReply, getMessageParent} = useContext(ChatAppContext); | ||
const [messageParent, setMessageParent] = useState<ChatKittyMessage | null>(null); | ||
const { changeReply, getMessageParent, messages } = | ||
useContext(ChatAppContext); | ||
const [messageParent, setMessageParent] = useState<ChatKittyMessage | null>( | ||
null | ||
); | ||
|
||
const [sameUser, setSameUser] = useState<boolean | null>(true); | ||
|
||
useEffect(() => { | ||
getMessageParent(message).then((message) => { | ||
setMessageParent(message) | ||
setMessageParent(message); | ||
}); | ||
},[]) | ||
|
||
if (index !== undefined && index < messages.length - 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the |
||
const previousMessage = messages[index + 1]; | ||
const messageTime = message.createdTime | ||
.split('T') | ||
.join('-') | ||
.split('-') | ||
.join(':') | ||
.split(':'); | ||
const previousMessageTime = previousMessage.createdTime | ||
.split('T') | ||
.join('-') | ||
.split('-') | ||
.join(':') | ||
.split(':'); | ||
let sameTime = true; | ||
|
||
for (let i = 0; i < 5; i++) { | ||
if (messageTime[i] !== previousMessageTime[i]) { | ||
console.log(message.id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this console log |
||
sameTime = false; | ||
break; | ||
} | ||
} | ||
|
||
setSameUser( | ||
isUserMessage(previousMessage) && | ||
previousMessage.user.displayName === sender.displayName && | ||
sameTime | ||
); | ||
} | ||
}, []); | ||
|
||
const changeReplyMessage = () => { | ||
changeReply(message); | ||
} | ||
}; | ||
|
||
const scrollToElement = () => { | ||
const element = document.getElementById(String(messageParent?.id)) | ||
if(element){ | ||
const element = document.getElementById(String(messageParent?.id)); | ||
|
||
if (element) { | ||
element.scrollIntoView(false); | ||
} | ||
} | ||
|
||
|
||
}; | ||
|
||
return (<> | ||
{messageParent && isUserMessage(messageParent) && | ||
<FlexRow | ||
style={{marginLeft:'20px', cursor:'pointer'}} | ||
return ( | ||
<div style={{ position: 'relative' }}> | ||
{messageParent && isUserMessage(messageParent) && ( | ||
<FlexRow | ||
style={{ marginLeft: '20px', cursor: 'pointer' }} | ||
alignItems="flex-start" | ||
bg={isHovering ? 'backgrounds.contentHover' : ''} | ||
{...hoverProps} | ||
onClick={scrollToElement} | ||
> | ||
<strong>@{messageParent.user.displayName}</strong> | ||
{isTextMessage(messageParent) && <p>: {messageParent.body}</p>} | ||
{isFileMessage(messageParent) && <p>: {messageParent.file.name}</p>} | ||
</FlexRow> | ||
)} | ||
<FlexRow | ||
py="1" | ||
px={[5, 6]} | ||
alignItems="flex-start" | ||
bg={isHovering ? 'backgrounds.contentHover' : ''} | ||
{...hoverProps} | ||
onClick={scrollToElement} | ||
> | ||
<strong>@{messageParent.user.displayName}</strong> | ||
{isTextMessage(messageParent) && <p>: {messageParent.body}</p>} | ||
{isFileMessage(messageParent) && <p>: {messageParent.file.name}</p>} | ||
</FlexRow> | ||
} | ||
<FlexRow | ||
py="1" | ||
px={[5, 6]} | ||
alignItems="flex-start" | ||
bg={isHovering ? 'backgrounds.contentHover' : ''} | ||
{...hoverProps} | ||
> | ||
{avatar} | ||
<FlexColumn marginLeft="5" flexGrow={1}> | ||
<FlexRow marginBottom="1"> | ||
<StyledBox marginRight="3"> | ||
<Heading>{sender.displayName}</Heading> | ||
</StyledBox> | ||
<Label size={LabelSizes.SMALL}> | ||
{moment(message.createdTime).fromNow()} | ||
</Label> | ||
{isHovering && ( | ||
<FlexRow> | ||
<StyledBox | ||
style={{ | ||
position: 'relative', | ||
left: '100px', | ||
borderRadius: '20%', | ||
marginLeft:'5px', | ||
height: '17px', | ||
width: '15px', | ||
}} | ||
{(!sameUser || messageParent) && avatar} | ||
<FlexColumn marginLeft="5" flexGrow={1}> | ||
<FlexRow marginBottom="1"> | ||
{(!sameUser || messageParent) && ( | ||
<StyledBox marginRight="3"> | ||
<Heading>{sender.displayName}</Heading> | ||
</StyledBox> | ||
)} | ||
{(!sameUser || messageParent) && ( | ||
<Label size={LabelSizes.SMALL}> | ||
{moment(message.createdTime).fromNow()} | ||
</Label> | ||
)} | ||
</FlexRow> | ||
|
||
<FlexRow> | ||
<div | ||
style={{ | ||
position: 'relative', | ||
left: sameUser && !messageParent ? '30px' : '0px', | ||
}} | ||
> | ||
<Message message={message} /> | ||
</div> | ||
{isHovering && ( | ||
<FlexRow | ||
style={{ position: 'absolute', top: '10px', right: '50px' }} | ||
> | ||
<PopupEmojiWindow message={message} /> | ||
|
||
</StyledBox> | ||
<div style={{ | ||
position: 'relative', | ||
left: '100px', | ||
borderRadius: '20%', | ||
marginLeft:'5px', | ||
height: '17px', | ||
width: '15px', | ||
}}> | ||
<img src={replyIcon} style={{width:'20px', cursor:'pointer'}} onClick={changeReplyMessage}/> | ||
<div | ||
style={{ | ||
position: 'absolute', | ||
top: '0px', | ||
right: '30px', | ||
borderRadius: '20%', | ||
height: '17px', | ||
width: '15px', | ||
}} | ||
> | ||
<img | ||
src={replyIcon} | ||
style={{ width: '20px', cursor: 'pointer' }} | ||
onClick={changeReplyMessage} | ||
/> | ||
</div> | ||
</FlexRow> | ||
)} | ||
</FlexRow> | ||
</FlexRow> | ||
)} | ||
</FlexRow> | ||
|
||
<Message message={message} /> | ||
<Reactions message={message} /> | ||
</FlexColumn> | ||
</FlexRow> | ||
</> | ||
<div | ||
style={{ | ||
position: 'relative', | ||
marginLeft: sameUser && !messageParent ? '30px' : '0px', | ||
}} | ||
> | ||
<Reactions message={message} /> | ||
</div> | ||
</FlexColumn> | ||
</FlexRow> | ||
</div> | ||
); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,7 +164,11 @@ const ChatAppContextProvider: React.FC<ChatAppContextProviderProps> = ({ | |
const views: Set<View> = new Set(); | ||
|
||
const demoUsers = [ | ||
'910746e1-d6e1-4df1-80b6-88ad90d7d2ad' | ||
'b2a6da08-88bf-4778-b993-7234e6d8a3ff', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
'c6f75947-af48-4893-a78e-0e0b9bd68580', | ||
'abc4264d-f1b1-41c0-b4cc-1e9daadfc893', | ||
'2989c53a-d0c5-4222-af8d-fbf7b0c74ec6', | ||
'8fadc920-f3e6-49ff-9398-1e58b3dc44dd', | ||
]; | ||
|
||
const getLayout = (): LayoutState => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can pass the previous message directly.
MessageListItem
shouldn't know aboutmessages
or run presentational logic.