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