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