Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

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