Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4119 stevensc 1
/* eslint-disable react/prop-types */
5043 stevensc 2
import React from "react"
5037 stevensc 3
import { useState, useEffect } from "react"
4
import { axios } from "../../utils"
2739 stevensc 5
import { FiMaximize2 } from 'react-icons/fi'
4860 stevensc 6
import AddIcon from '@mui/icons-material/Add'
1 www 7
 
5033 stevensc 8
// Components
5037 stevensc 9
import NotificationAlert from "../../shared/notification/NotificationAlert"
10
import PersonalChat from "./personal-chat/PersonalChat"
11
import ContactsModal from "./components/ContactsModal"
5043 stevensc 12
import ContactsFilters from "./components/contactsFilters"
5033 stevensc 13
 
5037 stevensc 14
const notifyAudio = new Audio("/audio/chat.mp3")
1 www 15
 
5043 stevensc 16
const Chat = ({ defaultNetwork, emojiOnePath, timezones }) => {
5037 stevensc 17
  const [contacts, setContacts] = useState([])
18
  const [activeChats, setActiveChats] = useState([])
5033 stevensc 19
 
5037 stevensc 20
  const [isChatOpen, setIsChatOpen] = useState(false)
21
  const [isMuted, setIsMuted] = useState(false)
22
  const [showModal, setShowModal] = useState(false)
23
  const [loading, setLoading] = useState(false)
5033 stevensc 24
 
4868 stevensc 25
  const [pendingConversation, setPendingConversation] = useState('')
5027 stevensc 26
 
976 stevensc 27
  const handleEntities = entities => {
687 steven 28
    entities.map((entity) => {
3111 stevensc 29
      if (entity.not_received_messages) handleNewMessage(entity)
30
      if (entity.not_seen_messages) handleNotSeenMessage(entity)
5035 stevensc 31
      if (entity.is_open) handleOpenConversation(entity, false)
32
      if (entity.type === 'user') handleUpdateOnline(entity)
33
    })
34
    setContacts(entities)
687 steven 35
  }
2790 stevensc 36
 
687 steven 37
  const heartBeat = async () => {
689 steven 38
    try {
3119 stevensc 39
      const { data } = await axios.get("/chat/heart-beat")
5037 stevensc 40
      if (data.success) handleEntities(data.data)
41
      return data.data
689 steven 42
    } catch (error) {
976 stevensc 43
      console.log('>>: chat error > ', error)
689 steven 44
    }
5037 stevensc 45
  }
1 www 46
 
47
  const handleUpdateOnline = (entity) => {
48
    const existingChatId = activeChats.findIndex(
49
      (activeChat) => activeChat.id === entity.id
5037 stevensc 50
    )
1 www 51
    if (existingChatId >= 0) {
52
      if (activeChats[existingChatId].online !== entity.online) {
5037 stevensc 53
        const newActiveChats = [...activeChats]
54
        newActiveChats[existingChatId].online = entity.online
55
        setActiveChats(newActiveChats)
1 www 56
      }
57
    }
5037 stevensc 58
  }
1 www 59
 
60
  const handleNewMessage = async (unseeEntity) => {
3596 stevensc 61
    await axios.post(unseeEntity.url_mark_received)
1 www 62
    if (!activeChats.some((activeChat) => activeChat.id === unseeEntity.id)) {
5037 stevensc 63
      setActiveChats([...activeChats, { ...unseeEntity, minimized: false }])
64
      playNotifyAudio()
1 www 65
    } else {
66
      const existingChatId = activeChats.findIndex(
67
        (activeChat) => activeChat.id === unseeEntity.id
5037 stevensc 68
      )
1 www 69
      if (!activeChats[existingChatId].unsee_messages) {
5037 stevensc 70
        const newActiveChats = [...activeChats]
71
        newActiveChats[existingChatId].unsee_messages = true
72
        setActiveChats(newActiveChats)
73
        playNotifyAudio(newActiveChats[existingChatId].minimized)
1 www 74
      }
75
    }
5037 stevensc 76
  }
1 www 77
 
3122 stevensc 78
  const handleCloseConversation = async (entity) => {
3127 stevensc 79
    const { data } = await axios.post(entity.url_close)
80
    if (!data.success) console.log('Error in entity close')
81
    setActiveChats(activeChats.filter(prevActiveChats => prevActiveChats.id !== entity.id))
3122 stevensc 82
  }
83
 
84
  const handleOpenConversation = async (entity, minimized = false) => {
3129 stevensc 85
    if (activeChats.length < 3 && !activeChats.some((el) => el.id === entity.id)) {
5037 stevensc 86
      setActiveChats([...activeChats, { ...entity, minimized: minimized }])
3127 stevensc 87
    }
3129 stevensc 88
    if (activeChats.length >= 3 && !activeChats.some((el) => el.id === entity.id)) {
89
      await handleCloseConversation(activeChats[0])
5037 stevensc 90
      setActiveChats(prevActiveChats => [...prevActiveChats, { ...entity, minimized: minimized }])
986 stevensc 91
    }
5037 stevensc 92
  }
1 www 93
 
3122 stevensc 94
  const handleReadConversation = async (entity) => {
95
    try {
96
      const { data } = await axios.post(entity.url_mark_seen)
97
      if (!data.success) console.log('Ha ocurrido un error')
98
      setActiveChats(prevActiveChats => [...prevActiveChats].map(chat => {
99
        if (entity.id === chat.id) return { ...chat, not_seen_messages: false }
5037 stevensc 100
        return chat
3122 stevensc 101
      }))
102
    } catch (error) {
103
      console.log(`Error: ${error}`)
1 www 104
    }
5037 stevensc 105
  }
1 www 106
 
3123 stevensc 107
  const handleMinimizeConversation = (entity, minimized = null) => {
108
    return setActiveChats(prevActiveChats => [...prevActiveChats].map(chat => {
3130 stevensc 109
      if (entity.id === chat.id) return { ...chat, minimized: minimized ?? !chat.minimized }
3123 stevensc 110
      return chat
111
    }))
112
  }
1 www 113
 
3119 stevensc 114
  const handleNotSeenMessage = (entity) => {
3111 stevensc 115
    const index = activeChats.findIndex(chat => chat.id === entity.id)
116
 
117
    if (index !== -1) {
118
      setActiveChats(prev => [...prev].map(chat => {
119
        if (chat.id === entity.id) {
120
          return {
121
            ...chat,
122
            not_seen_messages: entity.not_seen_messages
123
          }
124
        }
5037 stevensc 125
        return chat
3111 stevensc 126
      }))
127
    }
3119 stevensc 128
 
129
  }
130
 
1 www 131
  const playNotifyAudio = (minimized = true) => {
132
    if (!isMuted && minimized) {
5037 stevensc 133
      notifyAudio.play()
1 www 134
    }
5037 stevensc 135
  }
1 www 136
 
137
  const handleMute = () => {
5037 stevensc 138
    setIsMuted(!isMuted)
1 www 139
    if (isMuted) {
5037 stevensc 140
      notifyAudio.play()
1 www 141
    }
5037 stevensc 142
  }
1 www 143
 
144
  useEffect(() => {
2548 stevensc 145
    if (!loading) {
2973 stevensc 146
      const fetchData = async () => {
2966 stevensc 147
        setLoading(true)
5037 stevensc 148
        const entities = await heartBeat() || []
2966 stevensc 149
        setLoading(false)
2973 stevensc 150
 
151
        return entities
2966 stevensc 152
      }
153
 
2989 stevensc 154
      setTimeout(() => {
155
        fetchData()
156
      }, "2000")
691 steven 157
    }
5037 stevensc 158
  }, [loading])
978 stevensc 159
 
1 www 160
  useEffect(() => {
4868 stevensc 161
    if (pendingConversation) {
162
      const pendingChat = contacts.find(contact => contact.url_send === pendingConversation)
4872 stevensc 163
 
164
      if (pendingChat) {
165
        handleOpenConversation(pendingChat)
166
        setPendingConversation('')
167
      }
4880 stevensc 168
 
4868 stevensc 169
    }
170
  }, [pendingConversation, contacts])
171
 
172
  useEffect(() => {
5037 stevensc 173
    emojione.imageType = "png"
174
    emojione.sprites = false
175
    emojione.ascii = true
5043 stevensc 176
    emojione.imagePathPNG = emojiOnePath
5037 stevensc 177
  }, [])
1 www 178
 
5033 stevensc 179
  if (window.innerWidth < 1000 || window.location.pathname === '/chat') return null
5037 stevensc 180
 
5033 stevensc 181
  return (
5027 stevensc 182
    <>
5033 stevensc 183
      <div className="chat-helper">
5028 stevensc 184
        <div className="subpanel_title" onClick={(e) => (e.currentTarget === e.target) && setIsChatOpen(!isChatOpen)}>
185
          <a href="/chat" className="text-chat-title">
186
            Chat
187
            <FiMaximize2 className="ml-3" />
188
          </a>
189
          <div className="subpanel_title-icons">
190
            <i
191
              className={`icon ${isMuted ? "icon-volume-off" : "icon-volume-2"} text-20`}
192
              onClick={handleMute}
193
            />
194
            <i
195
              className={`fa ${isChatOpen ? "fa-angle-down" : "fa-angle-up"} text-20`}
196
              onClick={() => setIsChatOpen(!isChatOpen)}
197
            />
198
          </div>
199
        </div>
5035 stevensc 200
        {defaultNetwork !== 'y' &&
5047 stevensc 201
          <button className="action-btn" onClick={() => setShowModal(true)}>
5035 stevensc 202
            <AddIcon />
5047 stevensc 203
            Iniciar conversación
204
          </button>
5035 stevensc 205
        }
5048 stevensc 206
        {isChatOpen &&
207
          <ContactsFilters
208
            dataset={contacts}
209
            selectConversation={handleOpenConversation}
210
          />
211
        }
212
 
1 www 213
      </div>
5048 stevensc 214
 
3133 stevensc 215
      <div className="active_chats-list">
1 www 216
        {activeChats.map((entity, index) => (
217
          <PersonalChat
3131 stevensc 218
            index={index}
1 www 219
            key={entity.id}
220
            entity={entity}
3106 stevensc 221
            not_seen_messages={entity.not_seen_messages}
3131 stevensc 222
            minimized={entity.minimized}
3122 stevensc 223
            onClose={handleCloseConversation}
3123 stevensc 224
            onMinimize={handleMinimizeConversation}
3122 stevensc 225
            onRead={handleReadConversation}
5043 stevensc 226
            timezones={timezones}
1 www 227
          />
228
        ))}
229
      </div>
5048 stevensc 230
 
5033 stevensc 231
      <ContactsModal
4867 stevensc 232
        show={showModal}
4869 stevensc 233
        setConversation={(url) => setPendingConversation(url)}
4866 stevensc 234
      />
1335 stevensc 235
      <NotificationAlert />
5027 stevensc 236
    </>
237
  )
4858 stevensc 238
}
1 www 239
 
5037 stevensc 240
export default Chat