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