Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 197 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { memo, useEffect, useRef, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useForm } from 'react-hook-form'
4
import { useDispatch } from 'react-redux'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
import parse from 'html-react-parser'
7
import styled, { css } from 'styled-components'
8
import SendIcon from '@mui/icons-material/Send'
9
import IconButton from '@mui/material/IconButton'
10
import ArrowBackIcon from '@mui/icons-material/ArrowBack'
11
import AttachFileIcon from '@mui/icons-material/AttachFile'
12
import InsertEmoticonIcon from '@mui/icons-material/InsertEmoticon'
13
 
14
import Options from '../UI/Option'
15
import Emojione from './emojione/Emojione'
16
import FileModal from '../modals/FileModal'
17
import LoaderContainer from '../UI/LoaderContainer'
18
import Spinner from '../UI/Spinner'
19
 
20
const StyledChatContainer = styled.div`
21
  background-color: var(--bg-color);
22
  border-radius: var(--border-radius);
23
  border: 1px solid var(--border-primary);
24
  height: 80vh;
25
  display: flex;
26
  flex-direction: column;
27
  flex-grow: 1;
28
`
29
 
30
const StyledChatHeader = styled.div`
31
  align-items: center;
32
  border-bottom: 1px solid var(--border-primary);
33
  display: flex;
34
  justify-content: center;
35
  padding: 1rem 0.5rem;
36
  position: relative;
37
 
38
  & > button:first-child {
39
    position: absolute;
40
    left: 1rem;
41
    top: 50%;
42
    transform: translateY(-50%);
43
    display: inline-flex;
44
 
45
    @media (min-width: 768px) {
46
      display: none;
47
    }
48
  }
49
`
50
 
51
const StyledTitle = styled.h2`
52
  font-size: 1.5rem;
53
  font-weight: 500;
54
  width: fit-content;
55
  max-width: 25ch;
56
  text-align: center;
57
 
58
  @media (min-width: 768px) {
59
    max-width: 30ch;
60
  }
61
`
62
 
63
const StyledMessageList = styled.div`
64
  gap: 0.5rem;
65
  display: flex;
66
  flex-direction: column-reverse;
67
  height: -webkit-fill-available;
68
  padding: 0.5rem 0;
69
  overflow: auto;
70
`
71
 
72
const StyledMessage = styled.div`
73
  box-shadow: var(--light-shadow);
74
  display: inline-flex;
75
  flex-direction: column;
76
  gap: 0.5rem;
77
  margin-bottom: 0.5rem;
78
  max-width: 70%;
79
  min-width: 4rem;
80
  padding: 0.5rem;
81
  position: relative;
82
 
83
  & > p {
84
    color: var(--chat-color);
85
    max-width: 100%;
86
    overflow: hidden;
87
    text-overflow: ellipsis;
88
    word-break: break-word;
89
  }
90
 
91
  & > img,
92
  & > video {
93
    max-width: 250px;
94
    max-height: 250px;
95
    object-fit: contain;
96
  }
97
 
98
  &:first-child {
99
    margin-top: 0.5rem;
100
  }
101
 
102
  .time {
103
    color: $subtitle-color;
104
    font-size: 0.8rem;
105
  }
106
 
107
  .emojione {
108
    width: 1rem;
109
    height: 1rem;
110
  }
111
 
112
  ${(props) => {
113
    if (props.send) {
114
      return css`
115
        align-self: flex-end;
116
        background-color: var(--chat-send);
117
        border-radius: 10px 0 10px 10px;
118
        margin-right: 0.5rem;
119
      `
120
    }
121
    return css`
122
      align-self: flex-start;
123
      background-color: var(--chat-received);
124
      border-radius: 0 10px 10px 10px;
125
      margin-left: 0.5rem;
126
    `
127
  }}
128
`
129
 
130
const StyledForm = styled.form`
131
  border-top: 1px solid var(--border-primary);
132
  padding: 0.5rem;
133
  position: relative;
134
  display: flex;
135
  justify-content: center;
136
  align-items: center;
137
  gap: 0.5rem;
138
`
139
 
140
const StyledInput = styled.input`
141
  border: none;
142
  outline: none;
143
  flex: 1;
144
  padding: 0.5rem 1rem;
145
  border-radius: 30px;
146
  background: $bg-color-secondary;
147
 
148
  &:focus {
149
    background: $bg-color-secondary;
150
  }
151
`
152
 
153
const StyledLoader = styled(LoaderContainer)`
154
  max-height: 50px;
155
  max-width: 50px;
156
`
157
 
158
function messageAreEqual(oldProps, newProps) {
159
  return oldProps.message.id
160
    ? oldProps.message.id === newProps.message.id
161
    : oldProps.message.uuid === newProps.message.uuid
162
}
163
 
164
const Chat = ({ children }) => {
165
  return <StyledChatContainer>{children}</StyledChatContainer>
166
}
167
 
168
const Header = ({ children, options = [], onClose }) => {
169
  return (
170
    <StyledChatHeader>
171
      <IconButton onClick={onClose}>
172
        <ArrowBackIcon />
173
      </IconButton>
174
      {children}
175
      {!!options.length && <Options options={options} right="1rem" />}
176
    </StyledChatHeader>
177
  )
178
}
179
 
180
const Title = ({ children, url }) => {
181
  if (!url) {
182
    return <StyledTitle>{children}</StyledTitle>
183
  }
184
 
185
  return (
186
    <a href={url} style={{ width: 'fit-content' }}>
187
      <StyledTitle>{children}</StyledTitle>
188
    </a>
189
  )
190
}
191
 
192
const List = ({ messages = [], onPagination, scrollRef, loading }) => {
193
  const loadMoreEl = useRef(null)
194
 
195
  useEffect(() => {
196
    const observer = new IntersectionObserver(onPagination)
197
 
198
    if (loadMoreEl.current) {
199
      observer.observe(loadMoreEl.current)
200
    }
201
 
202
    return () => {
203
      observer.disconnect()
204
    }
205
  }, [messages])
206
 
207
  return (
208
    <StyledMessageList ref={scrollRef}>
209
      {messages.map((message) => (
210
        <List.Message message={message} key={message.id} />
211
      ))}
212
      <span ref={loadMoreEl}>.</span>
213
      {loading && (
214
        <StyledLoader>
215
          <Spinner />
216
        </StyledLoader>
217
      )}
218
    </StyledMessageList>
219
  )
220
}
221
 
222
// eslint-disable-next-line react/display-name
223
const Message = memo(({ message }) => {
224
  const senderName = (message) => {
225
    if (message.type === 'group' && !message.u === 1) {
226
      return <span className="user_name">{message.user_name}</span>
227
    }
228
  }
229
 
230
  const messagesContent = {
231
    text: (
232
      // eslint-disable-next-line no-undef
233
      <p>{parse(emojione.shortnameToImage(message.m || message.message))}</p>
234
    ),
235
    image: <img src={message.m || message.filename} alt="chat_image" />,
236
    video: (
237
      <video src={message.m || message.filename} preload="none" controls />
238
    ),
239
    document: (
240
      <a href={message.m || message.filename} download>
241
        <img className="pdf" src="/images/extension/pdf.png" alt="pdf" />
242
      </a>
243
    ),
244
  }
245
 
246
  return (
247
    <>
248
      {senderName(message)}
249
      <StyledMessage
250
        send={message.u ? message.u === 1 : message.side === 'left'}
251
      >
252
        {messagesContent[message.mtype || message.type]}
253
        <label className="time">
254
          {!message.not_received && (
255
            <i
256
              className="fa fa-check"
257
              style={message.seen ? { color: 'blue' } : { color: 'gray' }}
258
            />
259
          )}
260
          {message.time || message.date}
261
        </label>
262
      </StyledMessage>
263
    </>
264
  )
265
}, messageAreEqual)
266
 
267
const SubmitForm = ({ sendUrl, uploadUrl, onSubmit: onComplete }) => {
268
  const [showEmojione, setShowEmojione] = useState(false)
269
  const [isShowFileModal, setIsShowFileModal] = useState(false)
270
  const [isSending, setIsSending] = useState(false)
271
  const dispatch = useDispatch()
272
 
273
  const { handleSubmit, setValue, register, reset, getValues } = useForm()
274
 
275
  const onSubmit = handleSubmit(({ message }) => {
276
    const formData = new FormData()
277
    // eslint-disable-next-line no-undef
278
    formData.append('message', emojione.toShort(message))
279
 
280
    axios.post(sendUrl, formData).then((response) => {
281
      const { success, data } = response.data
282
 
283
      if (!success) {
284
        const errorMessage =
285
          typeof data === 'string' ? data : 'Ha ocurrido un error'
286
 
287
        setShowEmojione(false)
288
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
289
        return
290
      }
291
 
292
      setShowEmojione(false)
293
      onComplete()
294
      reset()
295
    })
296
  })
297
 
298
  const sendFile = (file) => {
299
    setIsSending(true)
300
    const formData = new FormData()
301
    formData.append('file', file)
302
 
303
    axios
304
      .post(uploadUrl, formData)
305
      .then(({ data: response }) => {
306
        const { success, data } = response
307
        if (!success) {
308
          const errorMessage =
309
            typeof data === 'string' ? data : 'Ha ocurrido un error'
310
          dispatch(addNotification({ style: 'success', msg: errorMessage }))
311
          return
312
        }
313
 
314
        toggleFileModal()
315
        onComplete()
316
      })
317
      .finally(() => setIsSending(false))
318
  }
319
 
320
  const toggleEmojione = () => {
321
    setShowEmojione(!showEmojione)
322
  }
323
 
324
  const toggleFileModal = () => {
325
    setIsShowFileModal(!isShowFileModal)
326
  }
327
 
328
  const onClickEmoji = (event) => {
329
    const shortname = event.currentTarget.dataset.shortname
330
    const currentMessage = getValues('message')
331
    // eslint-disable-next-line no-undef
332
    const unicode = emojione.shortnameToUnicode(shortname)
333
    setValue('message', `${currentMessage}${unicode}`)
334
  }
335
 
336
  return (
337
    <>
338
      <StyledForm onSubmit={onSubmit}>
339
        {showEmojione && <Emojione onClickEmoji={onClickEmoji} />}
340
        <IconButton onClick={toggleFileModal}>
341
          <AttachFileIcon />
342
        </IconButton>
343
        <IconButton onClick={toggleEmojione}>
344
          <InsertEmoticonIcon />
345
        </IconButton>
346
        <StyledInput
347
          type="text"
348
          name="message"
349
          placeholder="Escribe un mensaje"
350
          ref={register({ required: true })}
351
        />
352
        <IconButton type="submit">
353
          <SendIcon />
354
        </IconButton>
355
      </StyledForm>
356
      <FileModal
357
        isShow={isShowFileModal}
358
        onHide={toggleFileModal}
359
        onComplete={sendFile}
360
        loading={isSending}
361
      />
362
    </>
363
  )
364
}
365
 
366
Chat.Header = Header
367
Chat.Title = Title
368
Chat.List = List
369
List.Message = Message
370
Chat.SubmitForm = SubmitForm
371
 
372
export default Chat