Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useCallback, useEffect, useState } from 'react'
2
import { axios } from '../../utils'
3
import { Tab, Tabs } from '@mui/material'
4
import styled from 'styled-components'
5
import SearchIcon from '@mui/icons-material/Search'
6
 
7
import Contacts from './Contacts'
8
import ContactsModal from '../modals/ContactsModal'
9
import CreateGroupModal from '../modals/CreateGroupModal'
10
 
11
const notifyAudio = new Audio('/audio/chat.mp3')
12
 
13
const StyledTab = styled(Tab)`
14
  flex-grow: 1;
15
  color: var(--subtitle-color) !important;
16
  font-weight: bold !important;
17
  padding: 0.5rem 1rem !important;
18
  min-height: auto !important;
19
  &.Mui-selected {
20
    color: var(--font-color) !important;
21
  }
22
`
23
 
24
const ChatContacts = ({ selectedConversation, changeConversation }) => {
25
  const [showContactModal, setShowContactModal] = useState(false)
26
  const [showGroupModal, setShowGroupModal] = useState(false)
27
  const [conversations, setConversations] = useState([])
28
  const [search, setSearch] = useState('')
29
  const [tab, setTab] = useState('user')
30
 
31
  const options = [
32
    {
33
      label: 'Iniciar conversación',
1707 stevensc 34
      action: () => setShowContactModal(!showContactModal)
5 stevensc 35
    },
1707 stevensc 36
    { label: 'Crear grupo', action: () => setShowGroupModal(!showGroupModal) }
5 stevensc 37
  ]
38
 
39
  const onCreateConversation = (url) => {
40
    const conversation = conversations.find((user) => user.url_send === url)
41
    changeConversation(conversation)
42
  }
43
 
44
  const heartBeat = async () => {
45
    axios.get('/chat/heart-beat').then((response) => {
46
      const { data: entities, success } = response.data
47
 
48
      if (!success) {
49
        return
50
      }
51
 
52
      entities.map(
53
        (entity) =>
54
          entity.not_received_messages &&
55
          playNewMessage(entity.url_mark_received)
56
      )
57
 
58
      setConversations(entities)
59
    })
60
  }
61
 
62
  const playNewMessage = async (url) => {
63
    notifyAudio.play()
64
    const { data } = await axios.post(url)
65
    return data.success
66
  }
67
 
68
  const getConversations = useCallback(
69
    () => conversations.filter((conversation) => conversation.type === tab),
70
    [tab, conversations]
71
  )
72
 
73
  const filterConversations = useCallback(
74
    (conversations) =>
75
      conversations.filter((conversation) =>
1716 stevensc 76
        conversation?.name?.toLowerCase().includes(search.toLowerCase())
5 stevensc 77
      ),
78
 
79
    [search]
80
  )
81
 
82
  useEffect(() => {
1707 stevensc 83
    const heartBeatInterval = setInterval(() => heartBeat(), 3000)
5 stevensc 84
 
85
    return () => {
86
      clearInterval(heartBeatInterval)
87
    }
88
  }, [])
89
 
1707 stevensc 90
  useEffect(() => {
91
    if (!selectedConversation) return
92
 
1708 stevensc 93
    const block = !conversations.some((c) => {
1707 stevensc 94
      return JSON.stringify(c) === JSON.stringify(selectedConversation)
95
    })
96
 
97
    if (block) {
98
      changeConversation(null)
99
    }
100
  }, [conversations, selectedConversation])
101
 
5 stevensc 102
  return (
103
    <>
104
      <Contacts>
105
        <Contacts.Header options={options}>
106
          <h1>Chat</h1>
107
        </Contacts.Header>
108
        <Tabs
109
          value={tab}
110
          onChange={(e, newValue) => setTab(newValue)}
111
          sx={{
112
            width: '100%',
113
            minHeight: 'auto',
114
            '& .MuiTabs-indicator': {
1707 stevensc 115
              backgroundColor: 'var(--font-color)'
116
            }
5 stevensc 117
          }}
118
        >
1707 stevensc 119
          <StyledTab label='Personas' value='user' disableRipple />
120
          <StyledTab label='Grupos' value='group' disableRipple />
5 stevensc 121
        </Tabs>
122
 
1707 stevensc 123
        <div className='contact__search show'>
5 stevensc 124
          <SearchIcon />
125
          <input
1707 stevensc 126
            type='text'
127
            placeholder='Buscar'
5 stevensc 128
            onChange={(e) => setSearch(e.target.value)}
129
          />
130
        </div>
131
 
132
        <Contacts.List
133
          contacts={filterConversations(getConversations())}
134
          onChange={changeConversation}
135
          currentConversation={selectedConversation}
136
        />
137
      </Contacts>
138
      <ContactsModal
139
        show={showContactModal}
140
        onClose={() => setShowContactModal(false)}
141
        onComplete={onCreateConversation}
142
      />
143
      <CreateGroupModal
144
        isOpen={showGroupModal}
145
        onClose={() => setShowGroupModal(false)}
146
      />
147
    </>
148
  )
149
}
150
 
151
export default ChatContacts