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