Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useState, useEffect } from "react";
3
import {axios} from "../../utils";
4
import Contacts from "./contacts/Contacts";
5
import Groups from "./groups/Groups";
6
import PersonalChat from "./personal-chat/PersonalChat";
7
 
8
const notifyAudio = new Audio("/audio/chat.mp3");
9
 
10
const Chat = (props) => {
11
  // states
12
  const [contacts, setContacts] = useState([]);
13
  const [groups, setGroups] = useState([]);
14
  const [activeChats, setActiveChats] = useState([]);
15
  const [isChatOpen, setIsChatOpen] = useState(false);
16
  const [isMuted, setIsMuted] = useState(false);
17
  const [activeTab, setActiveTab] = useState("user");
18
 
19
  // Time intervals
20
  let heartBeatInterval;
21
 
22
  const heartBeat = async () => {
23
    let entities = [];
24
    await axios.get("/chat/heart-beat").then((response) => {
25
      const resData = response.data;
26
      if (resData.success) {
27
        entities = resData.data;
28
      }
29
    });
30
    let newUserContacts = [];
31
    let newGroups = [];
32
    entities.map((entity) => {
33
      if (entity.is_open) {
34
        handleOpenConversation(entity, true);
35
      }
36
      if (entity.not_received_messages) {
37
        handleNewMessage(entity);
38
      }
39
      switch (entity.type) {
40
        case "user":
41
          newUserContacts = [...newUserContacts, entity];
42
          handleUpdateOnline(entity);
43
          break;
44
        case "group":
45
          newGroups = [...newGroups, entity];
46
          break;
47
        default:
48
          break;
49
      }
50
    });
51
    setContacts(newUserContacts);
52
    setGroups(newGroups);
53
  };
54
 
55
  const handleUpdateOnline = (entity) => {
56
    const existingChatId = activeChats.findIndex(
57
      (activeChat) => activeChat.id === entity.id
58
    );
59
    if (existingChatId >= 0) {
60
      if (activeChats[existingChatId].online !== entity.online) {
61
        const newActiveChats = [...activeChats];
62
        newActiveChats[existingChatId].online = entity.online;
63
        setActiveChats(newActiveChats);
64
      }
65
    }
66
  };
67
 
68
  const handleNewMessage = async (unseeEntity) => {
69
    await axios.post(unseeEntity.url_mark_received).then((response) => {
70
       ('')
71
    });
72
    if (!activeChats.some((activeChat) => activeChat.id === unseeEntity.id)) {
73
      setActiveChats([...activeChats, { ...unseeEntity, minimized: false }]);
74
      playNotifyAudio();
75
    } else {
76
      const existingChatId = activeChats.findIndex(
77
        (activeChat) => activeChat.id === unseeEntity.id
78
      );
79
      if (!activeChats[existingChatId].unsee_messages) {
80
        const newActiveChats = [...activeChats];
81
        newActiveChats[existingChatId].unsee_messages = true;
82
        setActiveChats(newActiveChats);
83
        playNotifyAudio(newActiveChats[existingChatId].minimized);
84
      }
85
    }
86
  };
87
 
88
  const handleOpenConversation = (entity, minimized = false) => {
89
    if (activeChats.length <= 3 && !activeChats.some((el) => el.id === entity.id)) {
90
      setActiveChats([...activeChats, { ...entity, minimized: minimized }]);
91
    }
92
  };
93
 
94
  const handleReadChat = (index) => {
95
    if (activeChats[index].unsee_messages) {
96
      const newActiveChats = [...activeChats];
97
      newActiveChats[index].unsee_messages = false;
98
      setActiveChats(newActiveChats);
99
    }
100
  };
101
 
102
  const handleMinimizeChat = (chatIndex, minimize) => {
103
    const newActiveChats = [...activeChats];
104
    switch (minimize) {
105
      case false:
106
        newActiveChats[chatIndex].minimized = false;
107
        break;
108
      default:
109
        newActiveChats[chatIndex].minimized =
110
          !newActiveChats[chatIndex].minimized;
111
        break;
112
    }
113
    setActiveChats(newActiveChats);
114
  };
115
 
116
  const handleCloseChat = (entityId, url_close) => {
117
    let newActiveChats = [];
118
    axios.post(url_close).then((response) => {
119
      const resData = response.data;
120
    });
121
    newActiveChats = activeChats.filter(
122
      (activeChat) => activeChat.id !== entityId
123
    );
124
    setActiveChats(newActiveChats);
125
  };
126
 
127
  const playNotifyAudio = (minimized = true) => {
128
    if (!isMuted && minimized) {
129
      notifyAudio.play();
130
    }
131
  };
132
 
133
  const handleMute = () => {
134
    setIsMuted(!isMuted);
135
    if (isMuted) {
136
      notifyAudio.play();
137
    }
138
  };
139
 
140
  const handleChangeTab = (tab) => {
141
    setActiveTab(tab);
142
  };
143
 
144
  useEffect(() => {
145
    clearInterval(heartBeatInterval);
146
    heartBeatInterval = setInterval(() => {
147
      heartBeat();
148
    }, 1000);
149
    return () => {
150
      clearInterval(heartBeatInterval);
151
    };
152
  });
153
 
154
  useEffect(() => {
155
    emojione.imageType = "png";
156
    emojione.sprites = false;
157
    emojione.ascii = true;
158
    emojione.imagePathPNG = props.emojiOnePath;
159
  }, []);
160
 
161
  return window.innerWidth > 1000 ? (
162
    <React.Fragment>
163
      <div id="drupalchat-wrapper">
164
        <div id="drupalchat">
165
          <div className="item-list" id="chatbox_chatlist">
166
            <ul id="mainpanel">
167
              <li id="chatpanel" className="first last">
168
                <div className="subpanel" style={{ display: "block" }}>
169
                  <div
170
                    className="subpanel_title"
171
                    onClick={(e) => {
172
                      if (e.currentTarget === e.target)
173
                        setIsChatOpen(!isChatOpen);
174
                    }}
175
                  >
176
                    <div
177
                      style={{ width: "89%", height: "100%" }}
178
                      id="minmaxchatlist"
179
                      onClick={() => {
180
                        setIsChatOpen(!isChatOpen);
181
                      }}
182
                    >
183
                      <a
184
                        href="/chat"
185
                        className="text-white"
186
                      >
187
                        Chat
188
                      </a>
189
                    </div>
190
                    <span
191
                      className="min localhost-icon-minus-1"
192
                      id="mute-sound"
193
                    >
194
                      <i
195
                        className={`icon ${
196
                          isMuted ? "icon-volume-off" : "icon-volume-2"
197
                        } text-20`}
198
                        aria-hidden="true"
199
                        onClick={handleMute}
200
                      ></i>
201
                    </span>
202
                    {/* <span
203
                      className="min localhost-icon-minus-1"
204
                      id="new-chat-group"
205
                      style={{ marginRight: "5%" }}
206
                      title="Crear grupo"
207
                    >
208
                      <i className="fa fa-edit"></i>
209
                    </span> */}
210
                  </div>
211
                  <div
212
                    id="showhidechatlist"
213
                    style={{ display: isChatOpen ? "block" : "none" }}
214
                  >
215
                    <div
216
                      className="drupalchat_search_main chatboxinput"
217
                      style={{ background: "#f9f9f9" }}
218
                    >
219
                      <div
220
                        className="drupalchat_search"
221
                        style={{ height: "auto" }}
222
                      >
223
                        <input
224
                          className="drupalchat_searchinput live-search-box"
225
                          id="live-search-box"
226
                          placeholder="Buscar"
227
                          size="24"
228
                          type="text"
229
                        />
230
                        <button
231
                          className="searchbutton"
232
                          id="searchbutton"
233
                          title=""
234
                          style={{
235
                            height: "30px",
236
                            border: "none",
237
                            margin: "0px",
238
                            paddingRight: "13px",
239
                            verticalAlign: "middle",
240
                          }}
241
                          type="submit"
242
                        ></button>
243
                        <div
244
                          id="search_result"
245
                          style={{
246
                            textAlign: "center",
247
                            fontSize: "11px",
248
                            display: "none",
249
                          }}
250
                        >
251
                          Sin resultados
252
                        </div>
253
                      </div>
254
                    </div>
255
                    <div
256
                      className="drupalchat_search_main chatboxinput"
257
                      style={{ background: "#f9f9f9" }}
258
                    >
259
                      <div
260
                        style={{
261
                          width: "50%",
262
                          float: "left",
263
                          display: "inline-block",
264
                          padding: "5px",
265
                          textAlign: "center",
266
                          fontSize: "14px",
267
                        }}
268
                      >
269
                        <a
270
                          href="#"
271
                          className="blue-color chat-contacts"
272
                          onClick={(e) => {
273
                            e.preventDefault();
274
                            handleChangeTab("user");
275
                          }}
276
                        >
277
                          Contactos
278
                        </a>
279
                      </div>
280
                      <div
281
                        style={{
282
                          width: "50%",
283
                          display: "inline-block",
284
                          padding: "5px",
285
                          textAlign: "center",
286
                          fontSize: "14px",
287
                        }}
288
                      >
289
                        <a
290
                          href="#"
291
                          className="chat-groups"
292
                          onClick={(e) => {
293
                            e.preventDefault();
294
                            handleChangeTab("group");
295
                          }}
296
                        >
297
                          Grupos
298
                        </a>
299
                      </div>
300
                    </div>
301
                    <div
302
                      className="contact-list chatboxcontent"
303
                      style={{
304
                        display: activeTab === "user" ? "block" : "none",
305
                      }}
306
                    >
307
                      <Contacts
308
                        contacts={contacts}
309
                        onOpenConversation={handleOpenConversation}
310
                      />
311
                    </div>
312
                    <div
313
                      className="group-list chatboxcontent"
314
                      style={{
315
                        display: activeTab === "group" ? "block" : "none",
316
                      }}
317
                    >
318
                      <ul id="group-list-ul" className="live-search-list-group">
319
                        <Groups
320
                          groups={groups}
321
                          onOpenConversation={handleOpenConversation}
322
                        />
323
                      </ul>
324
                    </div>
325
                    <div
326
                      className="group-contacts-list chatboxcontent"
327
                      style={{ display: "none" }}
328
                    >
329
                      <div style={{ textAlign: "center", fontSize: "13px" }}>
330
                        Integrantes del grupo
331
                      </div>
332
                      <ul
333
                        id="contact-group-list-ul"
334
                        className="live-search-list"
335
                      ></ul>
336
                    </div>
337
                  </div>
338
                </div>
339
              </li>
340
            </ul>
341
          </div>
342
        </div>
343
      </div>
344
      <div style={{ display: "flex" }}>
345
        {activeChats.map((entity, index) => (
346
          <PersonalChat
347
            key={entity.id}
348
            entity={entity}
349
            index={index}
350
            onClose={handleCloseChat}
351
            onMinimize={handleMinimizeChat}
352
            onRead={handleReadChat}
353
          />
354
        ))}
355
      </div>
356
    </React.Fragment>
357
  ) : (
358
    ""
359
  );
360
};
361
 
362
export default Chat;