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

View File

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

View File

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