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