I'm making a chat application in React (Nextjs to be precise) and I've done all the components so far: the chat container, having a header, a messages list and an input. The code is listening for new messages, with each new message adding a new message child component in the messages list component.
This is a schematic version of the chat I want to achieve. Each message contains the text itself and the avatar of the person. The message data also contains the author, so using flex display, it places the message on one side or the other of the messages list. I would like the avatar to appear only on the last group of messages on each side.
I think that it should be MessageList component responsible to know which message is the last one of each side every time, but I'm not sure how to achieve it. First I tried with CSS, but I can only display in the last message of each side (:last-child), but not on the last one of each group, each side. Second, having an array marking each last message, but then I had to reorder and search for the previous one, and that lead to having avatars in the wrong messages. Also, having a state for each message doesn't seem optimal and efficient.
<ChatContainer>
<Header />
<MessagesList >
<Message author='me' />
<Message author='me' /> // <- avatar here
<Message author='them' />
<Message author='them' /> // <- avatar here
<Message author='me' />
<Message author='me' />
<Message author='me' /> // <- avatar here
<MessageList />
<Input />
<ChatContainer />
My question is, how I can efficiently and also in a "reactjs way" keep track of last message of each group of messages on each side?
