-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.jsx
159 lines (141 loc) · 4.5 KB
/
socket.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import io from 'socket.io-client'
import { useContext, useEffect } from 'react'
import { homeContext } from '../pages/home/context/homecontext'
export const socket = io('https://chatappbackend.vedantyadu.online', {withCredentials: true, autoConnect: false})
export function useSocket() {
const {messages, setMessages, groups, setGroups, setCurGroup, members, setMembers, curGroup} = useContext(homeContext)
useEffect(() => {
socket.on('user-online', (data) => {
if (members?.[data.groupid]?.[data.userid]) {
const temp = Object.assign({}, members)
temp[data.groupid][data.userid].online = true
setMembers(temp)
}
})
socket.on('user-offline', (data) => {
if (members?.[data.groupid]?.[data.userid]) {
const temp = Object.assign({}, members)
temp[data.groupid][data.userid].online = false
setMembers(temp)
}
})
socket.on('new-message', (data) => {
if (messages[data.groupid]) {
let temp = Object.assign({}, messages)
temp[data.groupid].push(data.details)
setMessages(temp)
}
})
socket.on('new-group', (data) => {
if (groups) {
const temp = Object.assign({}, groups)
temp[data.groupid] = data.details
setGroups(temp)
setCurGroup(data.groupid)
}
})
socket.on('member-removed', (data) => {
if (members?.[data.groupid]) {
const temp = Object.assign({}, members)
temp[data.groupid][data.userid].current_member = false
setMembers(temp)
}
})
socket.on('member-added', (data) => {
if (members?.[data.groupid]) {
const temp = Object.assign({}, members)
temp[data.groupid][data.userid] = data.details
setMembers(temp)
}
})
socket.on('group-invite', (data) => {
if (groups) {
setGroups({...groups, [data.groupid]: data.details})
}
})
socket.on('group-remove', (data) => {
if (groups?.[data.groupid]) {
const temp = Object.assign({}, groups)
delete temp[data.groupid]
setCurGroup(Object.keys(temp)[0])
setGroups(temp)
}
if (messages?.[data.groupid]) {
const temp = Object.assign({}, messages)
delete temp[data.groupid]
setMessages(temp)
}
if (members?.[data.groupid]) {
const temp = Object.assign({}, members)
delete temp[data.groupid]
setMembers(temp)
}
})
socket.on('group-change', (data) => {
if (groups?.[data.groupid]) {
const temp = Object.assign({}, groups)
if (data.image) {
temp[data.groupid][image] = data.image
}
temp[data.groupid].name = data.name
temp[data.groupid].description = data.description
setGroups(temp)
}
})
socket.on('user-change', (data) => {
if (members?.[data.groupid]?.[data.userid]) {
const temp = Object.assign({}, members)
if (data.image) {
temp[data.groupid][data.userid].image = data.image
}
temp[data.groupid][data.userid].username = data.username
temp[data.groupid][data.userid].status = data.status
setMembers(temp)
}
})
socket.on('group-delete', (data) => {
if (groups?.[data.groupid]) {
const temp = Object.assign({}, groups)
delete temp[data.groupid]
setCurGroup(Object.keys(temp)[0])
setGroups(temp)
}
if (messages?.[data.groupid]) {
const temp = Object.assign({}, messages)
delete temp[data.groupid]
setMessages(temp)
}
if (members?.[data.groupid]) {
const temp = Object.assign({}, members)
delete temp[data.groupid]
setMembers(temp)
}
})
socket.on('user-left', (data) => {
const temp = Object.assign({}, members)
temp[data.groupid][data.userid].current_member = false
setMembers(temp)
})
socket.on('new-admin', (data) => {
console.log('new-admin')
const temp = Object.assign({}, groups)
temp[data.groupid].admin = true
setGroups(temp)
})
return () => {
socket.off('new-message')
socket.off('user-offline')
socket.off('user-online')
socket.off('new-group')
socket.off('member-removed')
socket.off('member-added')
socket.off('group-invite')
socket.off('group-remove')
socket.off('group-change')
socket.off('group-delete')
socket.off('user-change')
socket.off('user-left')
socket.off('new-admin')
}
})
}