blob: a16edd88bcf3910d0fffae704f9e6f8b6c774544 [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"
Larbi Gharibe9af9732021-03-31 15:08:01 +01007
idillonbef18a52022-09-01 01:51:40 -04008dayjs.extend(isToday)
9dayjs.extend(isYesterday)
10
11export const MessageCall = (props) => {
12 return (
13 <Stack
14 alignItems="center"
15 >
16 "Appel"
17 </Stack>
18 )
Larbi Gharibe9af9732021-03-31 15:08:01 +010019}
20
idillonbef18a52022-09-01 01:51:40 -040021export const MessageInitial = (props) => {
idillon5815c732022-09-16 13:54:45 -040022 const { t } = useTranslation()
idillonbef18a52022-09-01 01:51:40 -040023 return (
24 <Stack
25 alignItems="center"
26 >
idillon5815c732022-09-16 13:54:45 -040027 {t("message_swarm_created")}
idillonbef18a52022-09-01 01:51:40 -040028 </Stack>
29 )
30}
31
32export 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
45export const MessageMember = (props) => {
idillon5815c732022-09-16 13:54:45 -040046 const { t } = useTranslation()
idillonbef18a52022-09-01 01:51:40 -040047 return (
48 <Stack
49 alignItems="center"
50 >
51 <Chip
52 sx={{
53 width: "fit-content",
54 }}
idillon5815c732022-09-16 13:54:45 -040055 label={t("message_user_joined", {user: props.message.author})}
idillonbef18a52022-09-01 01:51:40 -040056 />
57 </Stack>
58 )
59}
60
61export const MessageMerge = (props) => {
62 return (
63 <Stack
64 alignItems="center"
65 >
66 "merge"
67 </Stack>
68 )
69}
70
71export 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
86export 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 (
idillon04245a12022-09-01 11:12:17 -0400102 <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>
idillonbef18a52022-09-01 01:51:40 -0400126 </Divider>
127 </Box>
128 )
129}
130
131export 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
154export 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
210const 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
244const 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}