idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { Stack } from '@mui/system'; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 19 | import { Account, ConversationMember, Message } from 'jami-web-common'; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 20 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 21 | import { MessageRow } from './Message'; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 22 | |
| 23 | interface MessageListProps { |
| 24 | account: Account; |
| 25 | members: ConversationMember[]; |
| 26 | messages: Message[]; |
| 27 | } |
| 28 | |
| 29 | export default function MessageList({ account, members, messages }: MessageListProps) { |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 30 | return ( |
| 31 | <Stack direction="column-reverse"> |
| 32 | { |
| 33 | // most recent messages first |
| 34 | messages.map((message, index) => { |
| 35 | const isAccountMessage = message.author === account.getUri(); |
| 36 | let author; |
| 37 | if (isAccountMessage) { |
| 38 | author = account; |
| 39 | } else { |
| 40 | const member = members.find((member) => message.author === member.contact.getUri()); |
| 41 | author = member?.contact; |
| 42 | } |
| 43 | if (!author) { |
| 44 | return null; |
| 45 | } |
| 46 | const props = { |
| 47 | messageIndex: index, |
| 48 | messages, |
| 49 | isAccountMessage, |
| 50 | author, |
| 51 | }; |
| 52 | return <MessageRow key={message.id} {...props} />; |
| 53 | }) |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 54 | } |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 55 | </Stack> |
| 56 | ); |
| 57 | } |