blob: b86c43f6693d77c89d26fdbbbd5e16394d61425d [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 (
99 <Box marginTop="30px">
100 <Divider>
101 {textDate}
102 </Divider>
103 </Box>
104 )
105}
106
107export const MessageTime = ({time, hasDateOnTop}) => {
108 const hour = time.hour().toString().padStart(2,'0')
109 const minute = time.minute().toString().padStart(2,'0')
110 const textTime = `${hour}:${minute}`
111
112 return (
113 <Stack
114 direction="row"
115 justifyContent="center"
116 margin="30px"
117 marginTop={hasDateOnTop ? "20px" : "30px"}
118 >
119 <Typography
120 variant="caption"
121 color="#A7A7A7"
122 fontWeight={700}
123 >
124 {textTime}
125 </Typography>
126 </Stack>
127 )
128}
129
130export const MessageBubblesGroup = (props) => {
131
132 const isUser = true // should access user from the store
133 const position = isUser ? "end" : "start"
134 const bubbleColor = isUser ? "#005699" : "#E5E5E5"
135 const textColor = isUser ? "white" : "black"
136
137 return (
138 <Stack // Container for an entire row with a single group of bubbles
139 direction="row"
140 justifyContent={position}
141 >
142 <Stack // Container for a group of bubbles with the partipants informations
143 width="66.66%"
144 paddingTop="30px"
145 alignItems={position}
146 >
147 <ParticipantName
148 name={props.messages[0]?.author}
149 position={position}
150 />
151 <Stack // Container for the bubbles alone
152 spacing="6px"
153 alignItems={position}
154 direction="column-reverse"
155 >
156 {props.messages.map(
157 (message, index) => {
158 let Component
159 switch (message.type) {
160 case "text/plain":
161 Component = MessageText
162 break
163 case "application/data-transfer+json":
164 Component = MessageDataTransfer
165 break
166 }
167 return (
168 <Component // Container for a single bubble
169 key={message.id}
170 message={message}
171 textColor={textColor}
172 position={position}
173 bubbleColor={bubbleColor}
174 isFirstOfGroup={index == props.messages.length-1}
175 isLastOfGroup={index == 0}
176 />
177 )
178 }
179 )}
180 </Stack>
181 </Stack>
182 </Stack>
183 )
184}
185
186const MessageBubble = (props) => {
187 const largeRadius = "20px"
188 const smallRadius = "5px"
189 const radius = React.useMemo(() => {
190 if (props.position == "start") {
191 return {
192 borderStartStartRadius: props.isFirstOfGroup ? largeRadius : smallRadius,
193 borderStartEndRadius: largeRadius,
194 borderEndStartRadius: props.isLastOfGroup ? largeRadius : smallRadius,
195 borderEndEndRadius: largeRadius,
196 }
197 }
198 return {
199 borderStartStartRadius: largeRadius,
200 borderStartEndRadius: props.isFirstOfGroup ? largeRadius : smallRadius,
201 borderEndStartRadius: largeRadius,
202 borderEndEndRadius: props.isLastOfGroup ? largeRadius : smallRadius,
203 }
204 }, [props.isFirstOfGroup, props.isLastOfGroup, props.position])
205
206 return (
207 <Box
208 sx={{
209 width: "fit-content",
210 backgroundColor: props.backgroundColor,
211 padding: "16px",
212 ...radius,
213 }}
214 >
215 {props.children}
216 </Box>
217 )
218}
219
220const ParticipantName = (props) => {
221 return (
222 <Box
223 marginBottom="6px"
224 marginLeft="16px"
225 marginRight="16px"
226 >
227 <Typography
228 variant="caption"
229 color="#A7A7A7"
230 fontWeight={700}
231 >
232 {props.name}
233 </Typography>
234 </Box>
235 )
236}