Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3610 stevensc 1
import React, { useState } from 'react'
2
import { Link } from 'react-router-dom'
3
import { useForm } from 'react-hook-form'
4
import styled from 'styled-components'
5
import SendIcon from '@mui/icons-material/Send'
6
import IconButton from '@mui/material/IconButton'
7
import ArrowBackIcon from '@mui/icons-material/ArrowBack'
8
import AttachFileIcon from '@mui/icons-material/AttachFile'
9
import InsertEmoticonIcon from '@mui/icons-material/InsertEmoticon'
10
 
11
import FileModal from '@components/modals/FileModal'
12
import Emojione from '@components/chat/emojione/Emojione'
13
 
14
const ChatContainer = styled.div`
15
  background-color: var(--bg-color);
16
  border-radius: var(--border-radius);
17
  border: 1px solid var(--border-primary);
18
  height: 80vh;
19
  display: flex;
20
  flex-direction: column;
21
  flex-grow: 1;
22
  padding: 0px 0px !important;
23
`
24
 
25
const StyledChatHeader = styled.div`
26
  align-items: center;
27
  border-bottom: 1px solid var(--border-primary);
28
  display: flex;
29
  justify-content: center;
30
  padding: 1rem 0.5rem;
31
  position: relative;
32
 
33
  & > button:first-child {
34
    position: absolute;
35
    left: 1rem;
36
    top: 50%;
37
    transform: translateY(-50%);
38
    display: inline-flex;
39
 
40
    @media (min-width: 768px) {
41
      display: none;
42
    }
43
  }
44
`
45
 
46
const StyledTitle = styled.h2`
47
  font-size: 16px;
48
  font-weight: 600;
49
  width: fit-content;
50
  max-width: 20ch;
51
  text-align: center;
52
  color: #1d315c;
53
 
54
  @media (min-width: 768px) {
55
    max-width: 30ch;
56
  }
57
`
58
 
59
const StyledForm = styled.form`
60
  border-top: 1px solid var(--border-primary);
61
  padding: 0.5rem;
62
  position: relative;
63
  display: flex;
64
  justify-content: center;
65
  align-items: center;
66
  gap: 0.5rem;
67
`
68
 
69
const StyledInput = styled.input`
70
  border: none;
71
  outline: none;
72
  width: 100%;
73
  padding: 0.5rem 1rem;
74
  border-radius: 30px;
75
  background: var(--bg-color-secondary);
76
 
77
  &:focus {
78
    background: var(--bg-color-secondary);
79
  }
80
`
81
 
82
const Header = ({ children, onClose }) => {
83
  return (
84
    <StyledChatHeader>
85
      <IconButton onClick={onClose}>
86
        <ArrowBackIcon />
87
      </IconButton>
88
      {children}
89
    </StyledChatHeader>
90
  )
91
}
92
 
93
const Title = ({ children, url }) => {
94
  if (!url) {
95
    return <StyledTitle>{children}</StyledTitle>
96
  }
97
 
98
  return (
99
    <Link to={url} style={{ width: 'fit-content' }}>
100
      <StyledTitle>{children}</StyledTitle>
101
    </Link>
102
  )
103
}
104
 
105
const SubmitForm = ({ onSend }) => {
106
  const [showEmojione, setShowEmojione] = useState(false)
107
  const [isShowFileModal, setIsShowFileModal] = useState(false)
108
  const [isSending, setIsSending] = useState(false)
109
 
110
  const { handleSubmit, setValue, register, reset, getValues } = useForm()
111
 
112
  const onSubmit = handleSubmit(({ message }) => {
113
    onSend(message)
114
    reset()
115
  })
116
 
117
  const sendFile = (file) => {
118
    setIsSending(true)
119
    onSend(file)
120
  }
121
 
122
  const toggleEmojione = () => {
123
    setShowEmojione(!showEmojione)
124
  }
125
 
126
  const toggleFileModal = () => {
127
    setIsShowFileModal(!isShowFileModal)
128
  }
129
 
130
  const onClickEmoji = (event) => {
131
    const shortname = event.currentTarget.dataset.shortname
132
    const currentMessage = getValues('message')
133
    // eslint-disable-next-line no-undef
134
    const unicode = emojione.shortnameToUnicode(shortname)
135
    setValue('message', `${currentMessage}${unicode}`)
136
  }
137
 
138
  return (
139
    <>
140
      <StyledForm onSubmit={onSubmit}>
141
        {showEmojione && <Emojione onClickEmoji={onClickEmoji} />}
142
        <IconButton onClick={toggleFileModal}>
143
          <AttachFileIcon />
144
        </IconButton>
145
        <IconButton onClick={toggleEmojione}>
146
          <InsertEmoticonIcon />
147
        </IconButton>
148
        <StyledInput
149
          type='text'
150
          name='message'
151
          placeholder='Escribe un mensaje'
152
          ref={register({ required: true })}
153
        />
154
        <IconButton type='submit'>
155
          <SendIcon />
156
        </IconButton>
157
      </StyledForm>
158
      <FileModal
159
        isShow={isShowFileModal}
160
        onHide={toggleFileModal}
161
        onComplete={sendFile}
162
        loading={isSending}
163
      />
164
    </>
165
  )
166
}
167
 
168
ChatContainer.Header = Header
169
ChatContainer.Title = Title
170
 
171
ChatContainer.SubmitForm = SubmitForm
172
 
173
export default ChatContainer