feat: [closes #32] group messages under peer names

This commit is contained in:
Jeremy Kahn 2022-10-03 21:29:28 -05:00
parent 44c328fd1f
commit c38a203f07
3 changed files with 44 additions and 15 deletions

View File

@ -49,12 +49,20 @@ export const ChatTranscript = ({
paddingY: theme.spacing(1),
})}
>
{messageLog.map(message => {
{messageLog.map((message, idx) => {
const previousMessage = messageLog[idx - 1]
const isFirstMessageInGroup =
previousMessage?.authorId !== message.authorId
return (
// This wrapper div is necessary for accurate layout calculations
// when new messages cause the transcript to scroll to the bottom.
<div key={message.id}>
<Message message={message} userId={userId} />
<Message
message={message}
userId={userId}
showAuthor={isFirstMessageInGroup}
/>
</div>
)
})}

View File

@ -24,19 +24,37 @@ const mockReceivedMessage: ReceivedMessage = {
describe('Message', () => {
test('renders unsent message text', () => {
render(<Message message={mockUnsentMessage} userId={mockUserId} />)
render(
<Message
message={mockUnsentMessage}
userId={mockUserId}
showAuthor={false}
/>
)
screen.getByText(mockUnsentMessage.text)
})
test('renders received message text', () => {
render(<Message message={mockReceivedMessage} userId={mockUserId} />)
render(
<Message
message={mockReceivedMessage}
userId={mockUserId}
showAuthor={false}
/>
)
screen.getByText(mockReceivedMessage.text)
})
test('renders message author', () => {
render(<Message message={mockReceivedMessage} userId={mockUserId} />)
render(
<Message
message={mockReceivedMessage}
userId={mockUserId}
showAuthor={true}
/>
)
screen.getByText(funAnimalName(mockUserId))
})

View File

@ -26,6 +26,7 @@ import './Message.sass'
export interface MessageProps {
message: IMessage
showAuthor: boolean
userId: string
}
@ -71,7 +72,7 @@ const componentMap = {
},
}
export const Message = ({ message, userId }: MessageProps) => {
export const Message = ({ message, showAuthor, userId }: MessageProps) => {
let backgroundColor: string
if (message.authorId === userId) {
@ -84,6 +85,7 @@ export const Message = ({ message, userId }: MessageProps) => {
return (
<Box className="Message">
{showAuthor && (
<Typography
variant="caption"
display="block"
@ -93,6 +95,7 @@ export const Message = ({ message, userId }: MessageProps) => {
>
<PeerNameDisplay>{message.authorId}</PeerNameDisplay>
</Typography>
)}
<Box
sx={{
color: 'primary.contrastText',