blob: 44d423ab48c777e03d538b3597773459c995adfc [file] [log] [blame]
idillonbef18a52022-09-01 01:51:40 -04001import { Box, Chip, Divider, Stack, Typography } from "@mui/material"
2import dayjs from "dayjs"
3import isToday from "dayjs/plugin/isToday"
4import isYesterday from "dayjs/plugin/isYesterday"
5import React from "react"
idillon5815c732022-09-16 13:54:45 -04006import { useTranslation } from "react-i18next"
idillon89720a82022-09-06 18:47:05 -04007import ConversationAvatar from "./ConversationAvatar"
Larbi Gharibe9af9732021-03-31 15:08:01 +01008
idillonbef18a52022-09-01 01:51:40 -04009dayjs.extend(isToday)
10dayjs.extend(isYesterday)
11
12export const MessageCall = (props) => {
13 return (
14 <Stack
15 alignItems="center"
16 >
17 "Appel"
18 </Stack>
19 )
Larbi Gharibe9af9732021-03-31 15:08:01 +010020}
21
idillonbef18a52022-09-01 01:51:40 -040022export const MessageInitial = (props) => {
idillon5815c732022-09-16 13:54:45 -040023 const { t } = useTranslation()
idillonbef18a52022-09-01 01:51:40 -040024 return (
25 <Stack
26 alignItems="center"
27 >
idillon5815c732022-09-16 13:54:45 -040028 {t("message_swarm_created")}
idillonbef18a52022-09-01 01:51:40 -040029 </Stack>
30 )
31}
32
33export 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
46export const MessageMember = (props) => {
idillon5815c732022-09-16 13:54:45 -040047 const { t } = useTranslation()
idillonbef18a52022-09-01 01:51:40 -040048 return (
49 <Stack
50 alignItems="center"
51 >
52 <Chip
53 sx={{
54 width: "fit-content",
55 }}
idillon5815c732022-09-16 13:54:45 -040056 label={t("message_user_joined", {user: props.message.author})}
idillonbef18a52022-09-01 01:51:40 -040057 />
58 </Stack>
59 )
60}
61
62export const MessageMerge = (props) => {
63 return (
64 <Stack
65 alignItems="center"
66 >
67 "merge"
68 </Stack>
69 )
70}
71
72export const MessageText = (props) => {
73 return (
74 <MessageBubble
75 backgroundColor={props.bubbleColor}
76 position={props.position}
77 isFirstOfGroup={props.isFirstOfGroup}
78 isLastOfGroup={props.isLastOfGroup}
79 >
idillon89720a82022-09-06 18:47:05 -040080 <Typography variant="body1" color={props.textColor} textAlign={props.position}>
idillonbef18a52022-09-01 01:51:40 -040081 {props.message.body}
82 </Typography>
83 </MessageBubble>
84 )
85}
86
87export 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 (
idillon04245a12022-09-01 11:12:17 -0400103 <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>
idillonbef18a52022-09-01 01:51:40 -0400127 </Divider>
128 </Box>
129 )
130}
131
132export 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
155export const MessageBubblesGroup = (props) => {
idillon89720a82022-09-06 18:47:05 -0400156 const isUser = false // should access user from the store
idillonbef18a52022-09-01 01:51:40 -0400157 const position = isUser ? "end" : "start"
158 const bubbleColor = isUser ? "#005699" : "#E5E5E5"
159 const textColor = isUser ? "white" : "black"
160
161 return (
idillon89720a82022-09-06 18:47:05 -0400162 <Stack // Row for a group of message bubbles with the user's infos
idillonbef18a52022-09-01 01:51:40 -0400163 direction="row"
164 justifyContent={position}
idillon89720a82022-09-06 18:47:05 -0400165 alignItems="end"
166 spacing="10px"
idillonbef18a52022-09-01 01:51:40 -0400167 >
idillon89720a82022-09-06 18:47:05 -0400168 {
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
idillonbef18a52022-09-01 01:51:40 -0400177 width="66.66%"
idillonbef18a52022-09-01 01:51:40 -0400178 alignItems={position}
179 >
180 <ParticipantName
181 name={props.messages[0]?.author}
182 position={position}
183 />
idillon89720a82022-09-06 18:47:05 -0400184 <Stack // Container for a group of message bubbles
idillonbef18a52022-09-01 01:51:40 -0400185 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 (
idillon89720a82022-09-06 18:47:05 -0400201 <Component // Single message
idillonbef18a52022-09-01 01:51:40 -0400202 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
219const 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
253const 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}