idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 1 | import { Box, Chip, Divider, Stack, Typography } from "@mui/material" |
| 2 | import dayjs from "dayjs" |
| 3 | import isToday from "dayjs/plugin/isToday" |
| 4 | import isYesterday from "dayjs/plugin/isYesterday" |
| 5 | import React from "react" |
idillon | 5815c73 | 2022-09-16 13:54:45 -0400 | [diff] [blame] | 6 | import { useTranslation } from "react-i18next" |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 7 | import ConversationAvatar from "./ConversationAvatar" |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 8 | |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 9 | dayjs.extend(isToday) |
| 10 | dayjs.extend(isYesterday) |
| 11 | |
| 12 | export const MessageCall = (props) => { |
| 13 | return ( |
| 14 | <Stack |
| 15 | alignItems="center" |
| 16 | > |
| 17 | "Appel" |
| 18 | </Stack> |
| 19 | ) |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 20 | } |
| 21 | |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 22 | export const MessageInitial = (props) => { |
idillon | 5815c73 | 2022-09-16 13:54:45 -0400 | [diff] [blame] | 23 | const { t } = useTranslation() |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 24 | return ( |
| 25 | <Stack |
| 26 | alignItems="center" |
| 27 | > |
idillon | 5815c73 | 2022-09-16 13:54:45 -0400 | [diff] [blame] | 28 | {t("message_swarm_created")} |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 29 | </Stack> |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | export const MessageDataTransfer = (props) => { |
| 34 | return ( |
| 35 | <MessageBubble |
| 36 | backgroundColor={"#E5E5E5"} |
| 37 | position={props.position} |
| 38 | isFirstOfGroup={props.isFirstOfGroup} |
| 39 | isLastOfGroup={props.isLastOfGroup} |
| 40 | > |
| 41 | "data-transfer" |
| 42 | </MessageBubble> |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | export const MessageMember = (props) => { |
idillon | 5815c73 | 2022-09-16 13:54:45 -0400 | [diff] [blame] | 47 | const { t } = useTranslation() |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 48 | return ( |
| 49 | <Stack |
| 50 | alignItems="center" |
| 51 | > |
| 52 | <Chip |
| 53 | sx={{ |
| 54 | width: "fit-content", |
| 55 | }} |
idillon | 5815c73 | 2022-09-16 13:54:45 -0400 | [diff] [blame] | 56 | label={t("message_user_joined", {user: props.message.author})} |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 57 | /> |
| 58 | </Stack> |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | export const MessageMerge = (props) => { |
| 63 | return ( |
| 64 | <Stack |
| 65 | alignItems="center" |
| 66 | > |
| 67 | "merge" |
| 68 | </Stack> |
| 69 | ) |
| 70 | } |
| 71 | |
| 72 | export const MessageText = (props) => { |
| 73 | return ( |
| 74 | <MessageBubble |
| 75 | backgroundColor={props.bubbleColor} |
| 76 | position={props.position} |
| 77 | isFirstOfGroup={props.isFirstOfGroup} |
| 78 | isLastOfGroup={props.isLastOfGroup} |
| 79 | > |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 80 | <Typography variant="body1" color={props.textColor} textAlign={props.position}> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 81 | {props.message.body} |
| 82 | </Typography> |
| 83 | </MessageBubble> |
| 84 | ) |
| 85 | } |
| 86 | |
| 87 | export const MessageDate = ({time}) => { |
| 88 | let textDate |
| 89 | |
| 90 | if (time.isToday()) { |
| 91 | textDate = "Today" |
| 92 | } |
| 93 | else if (time.isYesterday()) { |
| 94 | textDate = "Yesterday" |
| 95 | } |
| 96 | else { |
| 97 | const date = time.date().toString().padStart(2,'0') |
| 98 | const month = (time.month()+1).toString().padStart(2,'0') |
| 99 | textDate = `${date}/${month}/${time.year()}` |
| 100 | } |
| 101 | |
| 102 | return ( |
idillon | 04245a1 | 2022-09-01 11:12:17 -0400 | [diff] [blame] | 103 | <Box marginTop="30px" > |
| 104 | <Divider |
| 105 | sx={{ |
| 106 | ".MuiDivider-wrapper": { |
| 107 | margin: 0, |
| 108 | padding: 0, |
| 109 | }, |
| 110 | "&::before": { |
| 111 | borderTop: "1px solid #E5E5E5", |
| 112 | }, |
| 113 | "&::after": { |
| 114 | borderTop: "1px solid #E5E5E5", |
| 115 | }, |
| 116 | }} |
| 117 | > |
| 118 | <Typography |
| 119 | variant="caption" |
| 120 | fontWeight={700} |
| 121 | border="1px solid #E5E5E5" |
| 122 | borderRadius="5px" |
| 123 | padding="10px 16px" |
| 124 | > |
| 125 | {textDate} |
| 126 | </Typography> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 127 | </Divider> |
| 128 | </Box> |
| 129 | ) |
| 130 | } |
| 131 | |
| 132 | export const MessageTime = ({time, hasDateOnTop}) => { |
| 133 | const hour = time.hour().toString().padStart(2,'0') |
| 134 | const minute = time.minute().toString().padStart(2,'0') |
| 135 | const textTime = `${hour}:${minute}` |
| 136 | |
| 137 | return ( |
| 138 | <Stack |
| 139 | direction="row" |
| 140 | justifyContent="center" |
| 141 | margin="30px" |
| 142 | marginTop={hasDateOnTop ? "20px" : "30px"} |
| 143 | > |
| 144 | <Typography |
| 145 | variant="caption" |
| 146 | color="#A7A7A7" |
| 147 | fontWeight={700} |
| 148 | > |
| 149 | {textTime} |
| 150 | </Typography> |
| 151 | </Stack> |
| 152 | ) |
| 153 | } |
| 154 | |
| 155 | export const MessageBubblesGroup = (props) => { |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 156 | const isUser = false // should access user from the store |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 157 | const position = isUser ? "end" : "start" |
| 158 | const bubbleColor = isUser ? "#005699" : "#E5E5E5" |
| 159 | const textColor = isUser ? "white" : "black" |
| 160 | |
| 161 | return ( |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 162 | <Stack // Row for a group of message bubbles with the user's infos |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 163 | direction="row" |
| 164 | justifyContent={position} |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 165 | alignItems="end" |
| 166 | spacing="10px" |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 167 | > |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 168 | { |
| 169 | !isUser |
| 170 | && |
| 171 | <ConversationAvatar |
| 172 | displayName="TempDisplayName" |
| 173 | sx={{ width: "22px", height: "22px", fontSize: "15px" }} |
| 174 | /> |
| 175 | } |
| 176 | <Stack // Container to align the bubbles to the same side of a row |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 177 | width="66.66%" |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 178 | alignItems={position} |
| 179 | > |
| 180 | <ParticipantName |
| 181 | name={props.messages[0]?.author} |
| 182 | position={position} |
| 183 | /> |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 184 | <Stack // Container for a group of message bubbles |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 185 | spacing="6px" |
| 186 | alignItems={position} |
| 187 | direction="column-reverse" |
| 188 | > |
| 189 | {props.messages.map( |
| 190 | (message, index) => { |
| 191 | let Component |
| 192 | switch (message.type) { |
| 193 | case "text/plain": |
| 194 | Component = MessageText |
| 195 | break |
| 196 | case "application/data-transfer+json": |
| 197 | Component = MessageDataTransfer |
| 198 | break |
| 199 | } |
| 200 | return ( |
idillon | 89720a8 | 2022-09-06 18:47:05 -0400 | [diff] [blame] | 201 | <Component // Single message |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 202 | key={message.id} |
| 203 | message={message} |
| 204 | textColor={textColor} |
| 205 | position={position} |
| 206 | bubbleColor={bubbleColor} |
| 207 | isFirstOfGroup={index == props.messages.length-1} |
| 208 | isLastOfGroup={index == 0} |
| 209 | /> |
| 210 | ) |
| 211 | } |
| 212 | )} |
| 213 | </Stack> |
| 214 | </Stack> |
| 215 | </Stack> |
| 216 | ) |
| 217 | } |
| 218 | |
| 219 | const MessageBubble = (props) => { |
| 220 | const largeRadius = "20px" |
| 221 | const smallRadius = "5px" |
| 222 | const radius = React.useMemo(() => { |
| 223 | if (props.position == "start") { |
| 224 | return { |
| 225 | borderStartStartRadius: props.isFirstOfGroup ? largeRadius : smallRadius, |
| 226 | borderStartEndRadius: largeRadius, |
| 227 | borderEndStartRadius: props.isLastOfGroup ? largeRadius : smallRadius, |
| 228 | borderEndEndRadius: largeRadius, |
| 229 | } |
| 230 | } |
| 231 | return { |
| 232 | borderStartStartRadius: largeRadius, |
| 233 | borderStartEndRadius: props.isFirstOfGroup ? largeRadius : smallRadius, |
| 234 | borderEndStartRadius: largeRadius, |
| 235 | borderEndEndRadius: props.isLastOfGroup ? largeRadius : smallRadius, |
| 236 | } |
| 237 | }, [props.isFirstOfGroup, props.isLastOfGroup, props.position]) |
| 238 | |
| 239 | return ( |
| 240 | <Box |
| 241 | sx={{ |
| 242 | width: "fit-content", |
| 243 | backgroundColor: props.backgroundColor, |
| 244 | padding: "16px", |
| 245 | ...radius, |
| 246 | }} |
| 247 | > |
| 248 | {props.children} |
| 249 | </Box> |
| 250 | ) |
| 251 | } |
| 252 | |
| 253 | const ParticipantName = (props) => { |
| 254 | return ( |
| 255 | <Box |
| 256 | marginBottom="6px" |
| 257 | marginLeft="16px" |
| 258 | marginRight="16px" |
| 259 | > |
| 260 | <Typography |
| 261 | variant="caption" |
| 262 | color="#A7A7A7" |
| 263 | fontWeight={700} |
| 264 | > |
| 265 | {props.name} |
| 266 | </Typography> |
| 267 | </Box> |
| 268 | ) |
| 269 | } |