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