simon | 26e79f7 | 2022-10-05 22:16:08 -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 | */ |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 18 | import { Box, Chip, Divider, Stack, Tooltip, Typography } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 19 | import { styled } from '@mui/material/styles'; |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 20 | import dayjs, { Dayjs } from 'dayjs'; |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 21 | import { Message } from 'jami-web-common'; |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 22 | import { ReactElement, ReactNode, useCallback, useMemo, useState } from 'react'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 23 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 24 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 25 | import { Account } from '../models/account'; |
| 26 | import { Contact } from '../models/contact'; |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 27 | import { getMessageCallText, getMessageMemberText } from '../utils/chatmessages'; |
| 28 | import { formatRelativeDate, formatTime } from '../utils/dates×'; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 29 | import { EmojiButton, MoreButton, ReplyMessageButton } from './Button'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | import ConversationAvatar from './ConversationAvatar'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 31 | import PopoverList, { PopoverListItemData } from './PopoverList'; |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 32 | import { |
| 33 | ArrowLeftCurved, |
| 34 | ArrowLeftDown, |
| 35 | ArrowRightUp, |
| 36 | OppositeArrowsIcon, |
| 37 | TrashBinIcon, |
| 38 | TwoSheetsIcon, |
| 39 | } from './SvgIcon'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 40 | |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 41 | type MessagePosition = 'start' | 'end'; |
| 42 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 43 | const notificationMessageTypes = ['initial', 'member'] as const; |
| 44 | type NotificationMessageType = typeof notificationMessageTypes[number]; |
| 45 | const checkIsNotificationMessageType = (type: Message['type'] | undefined): type is NotificationMessageType => { |
| 46 | return notificationMessageTypes.includes(type as NotificationMessageType); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 47 | }; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 48 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 49 | const invisibleMessageTypes = ['application/update-profile', 'merge', 'vote'] as const; |
| 50 | type InvisibleMessageType = typeof invisibleMessageTypes[number]; |
| 51 | const checkIsInvisibleMessageType = (type: Message['type'] | undefined): type is InvisibleMessageType => { |
| 52 | return invisibleMessageTypes.includes(type as InvisibleMessageType); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 54 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 55 | const userMessageTypes = ['text/plain', 'application/data-transfer+json', 'application/call-history+json'] as const; |
| 56 | type UserMessageType = typeof userMessageTypes[number]; |
| 57 | const checkIsUserMessageType = (type: Message['type'] | undefined): type is UserMessageType => { |
| 58 | return userMessageTypes.includes(type as UserMessageType); |
| 59 | }; |
| 60 | |
| 61 | const checkShowsTime = (time: Dayjs, previousTime: Dayjs) => { |
| 62 | return !previousTime.isSame(time) && !time.isBetween(previousTime, previousTime?.add(1, 'minute')); |
| 63 | }; |
| 64 | |
| 65 | const findPreviousVisibleMessage = (messages: Message[], messageIndex: number) => { |
| 66 | for (let i = messageIndex + 1; i < messages.length; ++i) { |
| 67 | const message = messages[i]; |
| 68 | if (!checkIsInvisibleMessageType(message?.type)) { |
| 69 | return message; |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | const findNextVisibleMessage = (messages: Message[], messageIndex: number) => { |
| 75 | for (let i = messageIndex - 1; i >= 0; --i) { |
| 76 | const message = messages[i]; |
| 77 | if (!checkIsInvisibleMessageType(message?.type)) { |
| 78 | return message; |
| 79 | } |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | const avatarSize = '22px'; |
| 84 | const spacingBetweenAvatarAndBubble = '10px'; |
| 85 | const bubblePadding = '16px'; |
| 86 | |
| 87 | interface MessageCallProps { |
| 88 | message: Message; |
| 89 | isAccountMessage: boolean; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 90 | isFirstOfGroup: boolean; |
| 91 | isLastOfGroup: boolean; |
| 92 | } |
| 93 | |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 94 | const MessageCall = ({ message, isAccountMessage, isFirstOfGroup, isLastOfGroup }: MessageCallProps) => { |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 95 | const position = isAccountMessage ? 'end' : 'start'; |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 96 | |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 97 | const { i18n } = useTranslation(); |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 98 | const { bubbleColor, Icon, text, textColor } = useMemo(() => { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 99 | const text = getMessageCallText(isAccountMessage, message, i18n); |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 100 | const callDuration = dayjs.duration(parseInt(message?.duration || '')); |
| 101 | if (callDuration.asSeconds() === 0) { |
| 102 | if (isAccountMessage) { |
| 103 | return { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 104 | text, |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 105 | Icon: ArrowLeftCurved, |
| 106 | textColor: 'white', |
| 107 | bubbleColor: '#005699' + '80', // opacity 50% |
| 108 | }; |
| 109 | } else { |
| 110 | return { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 111 | text, |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 112 | Icon: ArrowLeftCurved, |
| 113 | textColor: 'black', |
| 114 | bubbleColor: '#C6C6C6', |
| 115 | }; |
| 116 | } |
| 117 | } else { |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 118 | if (isAccountMessage) { |
| 119 | return { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 120 | text, |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 121 | Icon: ArrowRightUp, |
| 122 | textColor: 'white', |
| 123 | bubbleColor: '#005699', |
| 124 | }; |
| 125 | } else { |
| 126 | return { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 127 | text, |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 128 | Icon: ArrowLeftDown, |
| 129 | textcolor: 'black', |
| 130 | bubbleColor: '#E5E5E5', |
| 131 | }; |
| 132 | } |
| 133 | } |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 134 | }, [isAccountMessage, message, i18n]); |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 135 | |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 136 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 137 | <Bubble position={position} isFirstOfGroup={isFirstOfGroup} isLastOfGroup={isLastOfGroup} bubbleColor={bubbleColor}> |
idillon-sfl | ec73545 | 2022-10-27 13:18:41 -0400 | [diff] [blame] | 138 | <Stack direction="row" spacing="10px" alignItems="center"> |
| 139 | <Icon sx={{ fontSize: '16px', color: textColor }} /> |
| 140 | <Typography variant="body1" color={textColor} textAlign={position} fontWeight="bold" textTransform="uppercase"> |
| 141 | {text} |
| 142 | </Typography> |
| 143 | </Stack> |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 144 | </Bubble> |
| 145 | ); |
| 146 | }; |
| 147 | |
| 148 | const MessageInitial = () => { |
| 149 | const { t } = useTranslation(); |
| 150 | return <>{t('message_swarm_created')}</>; |
| 151 | }; |
| 152 | |
| 153 | interface MessageDataTransferProps { |
| 154 | message: Message; |
| 155 | isAccountMessage: boolean; |
| 156 | isFirstOfGroup: boolean; |
| 157 | isLastOfGroup: boolean; |
| 158 | } |
| 159 | |
| 160 | const MessageDataTransfer = ({ isAccountMessage, isFirstOfGroup, isLastOfGroup }: MessageDataTransferProps) => { |
| 161 | const position = isAccountMessage ? 'end' : 'start'; |
| 162 | return ( |
| 163 | <Bubble bubbleColor="#E5E5E5" position={position} isFirstOfGroup={isFirstOfGroup} isLastOfGroup={isLastOfGroup}> |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 164 | "data-transfer" |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 165 | </Bubble> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 166 | ); |
| 167 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 168 | |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 169 | interface MessageMemberProps { |
| 170 | message: Message; |
| 171 | } |
| 172 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 173 | const MessageMember = ({ message }: MessageMemberProps) => { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 174 | const { i18n } = useTranslation(); |
| 175 | |
| 176 | const text = getMessageMemberText(message, i18n); |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 177 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 178 | <Chip |
| 179 | sx={{ |
| 180 | width: 'fit-content', |
| 181 | }} |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 182 | label={text} |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 183 | /> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 184 | ); |
| 185 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 186 | |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 187 | interface MessageTextProps { |
| 188 | message: Message; |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 189 | isAccountMessage: boolean; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 190 | isFirstOfGroup: boolean; |
| 191 | isLastOfGroup: boolean; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 192 | } |
| 193 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 194 | const MessageText = ({ message, isAccountMessage, isFirstOfGroup, isLastOfGroup }: MessageTextProps) => { |
| 195 | const position = isAccountMessage ? 'end' : 'start'; |
| 196 | const bubbleColor = isAccountMessage ? '#005699' : '#E5E5E5'; |
| 197 | const textColor = isAccountMessage ? 'white' : 'black'; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 198 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 199 | <MessageTooltip position={position}> |
| 200 | <Bubble |
| 201 | bubbleColor={bubbleColor} |
| 202 | position={position} |
| 203 | isFirstOfGroup={isFirstOfGroup} |
| 204 | isLastOfGroup={isLastOfGroup} |
| 205 | > |
| 206 | <Typography variant="body1" color={textColor} textAlign={position}> |
| 207 | {message.body} |
| 208 | </Typography> |
| 209 | </Bubble> |
| 210 | </MessageTooltip> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 211 | ); |
| 212 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 213 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 214 | interface DateIndicatorProps { |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 215 | time: Dayjs; |
| 216 | } |
| 217 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 218 | const DateIndicator = ({ time }: DateIndicatorProps) => { |
idillon-sfl | 0e1a0d9 | 2022-10-25 16:52:44 -0400 | [diff] [blame] | 219 | const { i18n } = useTranslation(); |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 220 | const textDate = useMemo(() => formatRelativeDate(time, i18n), [time, i18n]); |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 221 | |
| 222 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 223 | <Box marginTop="30px"> |
idillon | 04245a1 | 2022-09-01 11:12:17 -0400 | [diff] [blame] | 224 | <Divider |
| 225 | sx={{ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 226 | '.MuiDivider-wrapper': { |
idillon | 04245a1 | 2022-09-01 11:12:17 -0400 | [diff] [blame] | 227 | margin: 0, |
| 228 | padding: 0, |
| 229 | }, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 230 | '&::before': { |
| 231 | borderTop: '1px solid #E5E5E5', |
idillon | 04245a1 | 2022-09-01 11:12:17 -0400 | [diff] [blame] | 232 | }, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 233 | '&::after': { |
| 234 | borderTop: '1px solid #E5E5E5', |
idillon | 04245a1 | 2022-09-01 11:12:17 -0400 | [diff] [blame] | 235 | }, |
| 236 | }} |
| 237 | > |
| 238 | <Typography |
| 239 | variant="caption" |
| 240 | fontWeight={700} |
| 241 | border="1px solid #E5E5E5" |
| 242 | borderRadius="5px" |
| 243 | padding="10px 16px" |
idillon-sfl | 0e1a0d9 | 2022-10-25 16:52:44 -0400 | [diff] [blame] | 244 | textTransform="capitalize" |
idillon | 04245a1 | 2022-09-01 11:12:17 -0400 | [diff] [blame] | 245 | > |
| 246 | {textDate} |
| 247 | </Typography> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 248 | </Divider> |
| 249 | </Box> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 250 | ); |
| 251 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 252 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 253 | interface TimeIndicatorProps { |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 254 | time: Dayjs; |
| 255 | hasDateOnTop: boolean; |
| 256 | } |
| 257 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 258 | const TimeIndicator = ({ time, hasDateOnTop }: TimeIndicatorProps) => { |
idillon-sfl | 0e1a0d9 | 2022-10-25 16:52:44 -0400 | [diff] [blame] | 259 | const { i18n } = useTranslation(); |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame^] | 260 | const textTime = useMemo(() => formatTime(time, i18n), [time, i18n]); |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 261 | |
| 262 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 263 | <Stack direction="row" justifyContent="center" marginTop={hasDateOnTop ? '20px' : '30px'}> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 264 | <Typography variant="caption" color="#A7A7A7" fontWeight={700}> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 265 | {textTime} |
| 266 | </Typography> |
| 267 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 268 | ); |
| 269 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 270 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 271 | interface NotificationMessageRowProps { |
| 272 | message: Message; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 273 | } |
| 274 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 275 | const NotificationMessageRow = ({ message }: NotificationMessageRowProps) => { |
| 276 | let messageComponent; |
| 277 | switch (message.type) { |
| 278 | case 'initial': |
| 279 | messageComponent = <MessageInitial />; |
| 280 | break; |
| 281 | case 'member': |
| 282 | messageComponent = <MessageMember message={message} />; |
| 283 | break; |
| 284 | default: |
| 285 | console.error(`${NotificationMessageRow.name} received unhandled message type: ${message.type}`); |
| 286 | return null; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 287 | } |
| 288 | |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 289 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 290 | <Stack paddingTop={'30px'} alignItems="center"> |
| 291 | {messageComponent} |
| 292 | </Stack> |
| 293 | ); |
| 294 | }; |
| 295 | |
| 296 | interface UserMessageRowProps { |
| 297 | message: Message; |
| 298 | isAccountMessage: boolean; |
| 299 | previousMessage: Message | undefined; |
| 300 | nextMessage: Message | undefined; |
| 301 | time: Dayjs; |
| 302 | showsTime: boolean; |
| 303 | author: Account | Contact; |
| 304 | } |
| 305 | |
| 306 | const UserMessageRow = ({ |
| 307 | message, |
| 308 | previousMessage, |
| 309 | nextMessage, |
| 310 | isAccountMessage, |
| 311 | time, |
| 312 | showsTime, |
| 313 | author, |
| 314 | }: UserMessageRowProps) => { |
| 315 | const authorName = author.getDisplayName(); |
| 316 | const position = isAccountMessage ? 'end' : 'start'; |
| 317 | |
| 318 | const previousIsUserMessageType = checkIsUserMessageType(previousMessage?.type); |
| 319 | const nextIsUserMessageType = checkIsUserMessageType(nextMessage?.type); |
| 320 | const nextTime = dayjs.unix(Number(nextMessage?.timestamp)); |
| 321 | const nextShowsTime = checkShowsTime(nextTime, time); |
| 322 | const isFirstOfGroup = showsTime || !previousIsUserMessageType || previousMessage?.author !== message.author; |
| 323 | const isLastOfGroup = nextShowsTime || !nextIsUserMessageType || message.author !== nextMessage?.author; |
| 324 | |
| 325 | const props = { |
| 326 | message, |
| 327 | isAccountMessage, |
| 328 | isFirstOfGroup, |
| 329 | isLastOfGroup, |
| 330 | }; |
| 331 | |
| 332 | let MessageComponent; |
| 333 | switch (message.type) { |
| 334 | case 'text/plain': |
| 335 | MessageComponent = MessageText; |
| 336 | break; |
| 337 | case 'application/data-transfer+json': |
| 338 | MessageComponent = MessageDataTransfer; |
| 339 | break; |
| 340 | case 'application/call-history+json': |
| 341 | MessageComponent = MessageCall; |
| 342 | break; |
| 343 | default: |
| 344 | console.error(`${UserMessageRow.name} received unhandled message type: ${message.type}`); |
| 345 | return null; |
| 346 | } |
| 347 | |
| 348 | const participantNamePadding = isAccountMessage |
| 349 | ? bubblePadding |
| 350 | : parseInt(avatarSize) + parseInt(spacingBetweenAvatarAndBubble) + parseInt(bubblePadding) + 'px'; |
| 351 | |
| 352 | return ( |
| 353 | <Stack alignItems={position}> |
| 354 | {isFirstOfGroup && ( |
| 355 | <Box padding={`30px ${participantNamePadding} 0 ${participantNamePadding}`}> |
| 356 | <ParticipantName name={authorName} /> |
| 357 | </Box> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 358 | )} |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 359 | <Stack |
| 360 | direction="row" |
| 361 | justifyContent={position} |
| 362 | alignItems="end" |
| 363 | spacing={spacingBetweenAvatarAndBubble} |
| 364 | paddingTop="6px" |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 365 | width="66.66%" |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 366 | > |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 367 | <Box sx={{ width: avatarSize }}> |
| 368 | {!isAccountMessage && isLastOfGroup && ( |
| 369 | <ConversationAvatar |
| 370 | displayName={authorName} |
| 371 | sx={{ width: avatarSize, height: avatarSize, fontSize: '15px' }} |
| 372 | /> |
| 373 | )} |
| 374 | </Box> |
| 375 | <MessageComponent {...props} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 376 | </Stack> |
| 377 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 378 | ); |
| 379 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 380 | |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 381 | interface MessageTooltipProps { |
| 382 | className?: string; |
| 383 | position: MessagePosition; |
| 384 | children: ReactElement; |
| 385 | } |
| 386 | |
| 387 | const MessageTooltip = styled(({ className, position, children }: MessageTooltipProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 388 | const [open, setOpen] = useState(false); |
| 389 | const emojis = ['😎', '😄', '😍']; // Should be last three used emojis |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 390 | const additionalOptions: PopoverListItemData[] = [ |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 391 | { |
| 392 | Icon: TwoSheetsIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 393 | label: 'Copy', |
| 394 | onClick: () => {}, |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 395 | }, |
| 396 | { |
| 397 | Icon: OppositeArrowsIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 398 | label: 'Transfer', |
| 399 | onClick: () => {}, |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 400 | }, |
| 401 | { |
| 402 | Icon: TrashBinIcon, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 403 | label: 'Delete message', |
| 404 | onClick: () => {}, |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 405 | }, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 406 | ]; |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 407 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 408 | const toggleMoreMenu = useCallback(() => setOpen((open) => !open), [setOpen]); |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 409 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 410 | const onClose = useCallback(() => { |
| 411 | setOpen(false); |
| 412 | }, [setOpen]); |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 413 | |
| 414 | return ( |
| 415 | <Tooltip |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 416 | classes={{ tooltip: className }} // Required for styles. Don't know why |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 417 | placement={position === 'start' ? 'right-start' : 'left-start'} |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 418 | PopperProps={{ |
| 419 | modifiers: [ |
| 420 | { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 421 | name: 'offset', |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 422 | options: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 423 | offset: [-2, -30], |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 424 | }, |
| 425 | }, |
| 426 | ], |
| 427 | }} |
| 428 | onClose={onClose} |
| 429 | title={ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 430 | <Stack> |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 431 | <Stack // Main options |
| 432 | direction="row" |
| 433 | spacing="16px" |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 434 | padding="16px" |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 435 | > |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 436 | {emojis.map((emoji) => ( |
| 437 | <EmojiButton key={emoji} emoji={emoji} /> |
| 438 | ))} |
| 439 | <ReplyMessageButton /> |
| 440 | <MoreButton onClick={toggleMoreMenu} /> |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 441 | </Stack> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 442 | {open && ( |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 443 | <> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 444 | <Divider sx={{ marginX: '16px' }} /> |
| 445 | <PopoverList items={additionalOptions} /> |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 446 | </> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 447 | )} |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 448 | </Stack> |
| 449 | } |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 450 | > |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 451 | {/* div fixes 'Function components cannot be given refs' error */} |
| 452 | <div>{children}</div> |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 453 | </Tooltip> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 454 | ); |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 455 | })(({ position }) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 456 | const largeRadius = '20px'; |
| 457 | const smallRadius = '5px'; |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 458 | return { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 459 | backgroundColor: 'white', |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 460 | padding: '0px', |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 461 | boxShadow: '3px 3px 7px #00000029', |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 462 | borderRadius: largeRadius, |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 463 | borderStartStartRadius: position === 'start' ? smallRadius : largeRadius, |
| 464 | borderStartEndRadius: position === 'end' ? smallRadius : largeRadius, |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 465 | overflow: 'hidden', |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 466 | }; |
idillon | 927b759 | 2022-09-15 12:56:45 -0400 | [diff] [blame] | 467 | }); |
| 468 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 469 | interface BubbleProps { |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 470 | position: MessagePosition; |
| 471 | isFirstOfGroup: boolean; |
| 472 | isLastOfGroup: boolean; |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 473 | bubbleColor: string; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 474 | children: ReactNode; |
| 475 | } |
| 476 | |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 477 | const Bubble = ({ position, isFirstOfGroup, isLastOfGroup, bubbleColor, children }: BubbleProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 478 | const largeRadius = '20px'; |
| 479 | const smallRadius = '5px'; |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 480 | const radius = useMemo(() => { |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 481 | if (position === 'start') { |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 482 | return { |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 483 | borderStartStartRadius: isFirstOfGroup ? largeRadius : smallRadius, |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 484 | borderStartEndRadius: largeRadius, |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 485 | borderEndStartRadius: isLastOfGroup ? largeRadius : smallRadius, |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 486 | borderEndEndRadius: largeRadius, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 487 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 488 | } |
| 489 | return { |
| 490 | borderStartStartRadius: largeRadius, |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 491 | borderStartEndRadius: isFirstOfGroup ? largeRadius : smallRadius, |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 492 | borderEndStartRadius: largeRadius, |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 493 | borderEndEndRadius: isLastOfGroup ? largeRadius : smallRadius, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 494 | }; |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 495 | }, [isFirstOfGroup, isLastOfGroup, position]); |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 496 | |
| 497 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 498 | <Box |
| 499 | sx={{ |
| 500 | width: 'fit-content', |
| 501 | backgroundColor: bubbleColor, |
| 502 | padding: bubblePadding, |
| 503 | ...radius, |
| 504 | }} |
| 505 | > |
| 506 | {children} |
| 507 | </Box> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 508 | ); |
| 509 | }; |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 510 | |
idillon-sfl | 9d956ab | 2022-10-20 16:33:24 -0400 | [diff] [blame] | 511 | interface ParticipantNameProps { |
| 512 | name: string; |
| 513 | } |
| 514 | |
| 515 | const ParticipantName = ({ name }: ParticipantNameProps) => { |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 516 | return ( |
idillon-sfl | 118ae44 | 2022-10-25 10:42:54 -0400 | [diff] [blame] | 517 | <Typography variant="caption" color="#A7A7A7" fontWeight={700}> |
| 518 | {name} |
| 519 | </Typography> |
| 520 | ); |
| 521 | }; |
| 522 | |
| 523 | interface MessageProps { |
| 524 | messageIndex: number; |
| 525 | messages: Message[]; |
| 526 | isAccountMessage: boolean; |
| 527 | author: Account | Contact; |
| 528 | } |
| 529 | |
| 530 | export const MessageRow = ({ messageIndex, messages, isAccountMessage, author }: MessageProps) => { |
| 531 | const message = messages[messageIndex]; |
| 532 | const previousMessage = findPreviousVisibleMessage(messages, messageIndex); |
| 533 | const nextMessage = findNextVisibleMessage(messages, messageIndex); |
| 534 | const time = dayjs.unix(Number(message.timestamp)); |
| 535 | const previousTime = dayjs.unix(Number(previousMessage?.timestamp)); |
| 536 | const showDate = |
| 537 | message?.type === 'initial' || previousTime.year() !== time.year() || previousTime.dayOfYear() !== time.dayOfYear(); |
| 538 | const showTime = checkShowsTime(time, previousTime); |
| 539 | let messageComponent; |
| 540 | if (checkIsUserMessageType(message.type)) { |
| 541 | messageComponent = ( |
| 542 | <UserMessageRow |
| 543 | message={message} |
| 544 | previousMessage={previousMessage} |
| 545 | nextMessage={nextMessage} |
| 546 | time={time} |
| 547 | showsTime={showTime} |
| 548 | isAccountMessage={isAccountMessage} |
| 549 | author={author} |
| 550 | /> |
| 551 | ); |
| 552 | } else if (checkIsNotificationMessageType(message.type)) { |
| 553 | messageComponent = <NotificationMessageRow message={message} />; |
| 554 | } else if (checkIsInvisibleMessageType(message.type)) { |
| 555 | return null; |
| 556 | } else { |
| 557 | const _exhaustiveCheck: never = message.type; |
| 558 | return _exhaustiveCheck; |
| 559 | } |
| 560 | |
| 561 | return ( |
| 562 | <Stack> |
| 563 | {showDate && <DateIndicator time={time} />} |
| 564 | {showTime && <TimeIndicator time={time} hasDateOnTop={showDate} />} |
| 565 | {messageComponent} |
| 566 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 567 | ); |
| 568 | }; |