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