@churchapps/apphelper 0.4.50 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/FormCardPayment.d.ts +1 -1
- package/dist/components/FormCardPayment.d.ts.map +1 -1
- package/dist/components/FormCardPayment.js +27 -10
- package/dist/components/FormCardPayment.js.map +1 -1
- package/dist/components/Loading.js +36 -36
- package/dist/components/notes/Notes.js +27 -27
- package/dist/helpers/index.d.ts +1 -1
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +1 -1
- package/dist/helpers/index.js.map +1 -1
- package/dist/public/css/cropper.css +309 -309
- package/dist/public/css/styles.css +111 -111
- package/package.json +72 -73
- package/public/css/cropper.css +309 -309
- package/public/css/styles.css +111 -111
- package/public/locales/de.json +269 -269
- package/public/locales/en.json +276 -276
- package/public/locales/es.json +272 -272
- package/public/locales/fr.json +269 -269
- package/public/locales/hi.json +269 -269
- package/public/locales/it.json +269 -269
- package/public/locales/ko.json +269 -269
- package/public/locales/no.json +269 -269
- package/public/locales/pt.json +269 -269
- package/public/locales/ru.json +269 -269
- package/public/locales/tl.json +269 -269
- package/public/locales/zh.json +269 -269
- package/src/components/DisplayBox.tsx +83 -83
- package/src/components/ErrorMessages.tsx +28 -28
- package/src/components/ExportLink.tsx +81 -81
- package/src/components/FloatingSupport.tsx +18 -18
- package/src/components/FormCardPayment.tsx +184 -169
- package/src/components/FormSubmissionEdit.tsx +168 -168
- package/src/components/HelpIcon.tsx +12 -12
- package/src/components/ImageEditor.tsx +161 -161
- package/src/components/InputBox.tsx +96 -96
- package/src/components/Loading.tsx +77 -77
- package/src/components/PageHeader.tsx +110 -110
- package/src/components/PersonAvatar.tsx +77 -77
- package/src/components/QuestionEdit.tsx +99 -99
- package/src/components/SmallButton.tsx +42 -42
- package/src/components/SupportModal.tsx +32 -32
- package/src/components/TabPanel.tsx +28 -28
- package/src/components/gallery/GalleryModal.tsx +173 -173
- package/src/components/gallery/StockPhotos.tsx +95 -95
- package/src/components/gallery/index.ts +1 -1
- package/src/components/header/Banner.tsx +11 -11
- package/src/components/header/PrimaryMenu.tsx +100 -100
- package/src/components/header/SecondaryMenu.tsx +23 -23
- package/src/components/header/SecondaryMenuAlt.tsx +40 -40
- package/src/components/header/SiteHeader.tsx +207 -207
- package/src/components/header/SupportDrawer.tsx +111 -111
- package/src/components/header/index.tsx +2 -2
- package/src/components/index.tsx +20 -20
- package/src/components/notes/AddNote.tsx +180 -180
- package/src/components/notes/Note.tsx +68 -68
- package/src/components/notes/Notes.tsx +208 -208
- package/src/components/notes/index.ts +3 -3
- package/src/components/wrapper/AppList.tsx +19 -19
- package/src/components/wrapper/ChurchList.tsx +154 -154
- package/src/components/wrapper/NavItem.tsx +47 -47
- package/src/components/wrapper/NewPrivateMessage.tsx +253 -253
- package/src/components/wrapper/Notifications.tsx +223 -223
- package/src/components/wrapper/PrivateMessageDetails.tsx +112 -112
- package/src/components/wrapper/PrivateMessages.tsx +576 -576
- package/src/components/wrapper/UserMenu.tsx +383 -383
- package/src/components/wrapper/index.tsx +8 -8
- package/src/helpers/AnalyticsHelper.ts +32 -32
- package/src/helpers/AppearanceHelper.ts +73 -73
- package/src/helpers/ArrayHelper.ts +87 -87
- package/src/helpers/CurrencyHelper.ts +10 -10
- package/src/helpers/DateHelper.ts +104 -104
- package/src/helpers/ErrorHelper.ts +43 -43
- package/src/helpers/EventHelper.ts +49 -49
- package/src/helpers/FileHelper.ts +31 -31
- package/src/helpers/Locale.ts +457 -457
- package/src/helpers/NotificationService.ts +296 -296
- package/src/helpers/PersonHelper.ts +62 -62
- package/src/helpers/SlugHelper.ts +37 -37
- package/src/helpers/SocketHelper.ts +296 -296
- package/src/helpers/UniqueIdHelper.ts +36 -36
- package/src/helpers/UserHelper.ts +104 -104
- package/src/helpers/createEmotionCache.ts +17 -17
- package/src/helpers/index.ts +58 -50
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useMountedState.ts +16 -16
- package/src/hooks/useNotifications.ts +93 -93
- package/src/index.ts +2 -2
- package/src/types/interface-extensions.d.ts +12 -0
- package/tsconfig.json +31 -31
- package/dist/public/locales/de.json +0 -270
- package/dist/public/locales/en.json +0 -277
- package/dist/public/locales/es.json +0 -272
- package/dist/public/locales/fr.json +0 -270
- package/dist/public/locales/hi.json +0 -270
- package/dist/public/locales/it.json +0 -270
- package/dist/public/locales/ko.json +0 -270
- package/dist/public/locales/no.json +0 -270
- package/dist/public/locales/pt.json +0 -270
- package/dist/public/locales/ru.json +0 -270
- package/dist/public/locales/tl.json +0 -270
- package/dist/public/locales/zh.json +0 -270
|
@@ -1,296 +1,296 @@
|
|
|
1
|
-
import { ConnectionInterface, SocketActionHandlerInterface, SocketPayloadInterface, ApiHelper, ArrayHelper, CommonEnvironmentHelper } from "@churchapps/helpers";
|
|
2
|
-
|
|
3
|
-
export class SocketHelper {
|
|
4
|
-
static socket: WebSocket;
|
|
5
|
-
static socketId: string;
|
|
6
|
-
static actionHandlers: SocketActionHandlerInterface[] = [];
|
|
7
|
-
private static personIdChurchId: { personId: string, churchId: string } = { personId: "", churchId: "" };
|
|
8
|
-
private static isCleanedUp: boolean = false;
|
|
9
|
-
|
|
10
|
-
static setPersonChurch = (pc: { personId: string, churchId: string }) => {
|
|
11
|
-
console.log('👤 SocketHelper: Setting person/church context:', pc);
|
|
12
|
-
|
|
13
|
-
if (pc?.personId && pc.personId && pc.churchId !== this.personIdChurchId.churchId && pc.personId !== this.personIdChurchId.personId) {
|
|
14
|
-
this.personIdChurchId = pc;
|
|
15
|
-
console.log('🔗 SocketHelper: Person/church context updated, creating alert connection');
|
|
16
|
-
this.createAlertConnection();
|
|
17
|
-
} else {
|
|
18
|
-
console.log('⚠️ SocketHelper: Person/church context unchanged or invalid');
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static createAlertConnection = () => {
|
|
23
|
-
console.log('🔗 SocketHelper: Attempting to create alert connection...');
|
|
24
|
-
console.log('🆔 Socket ID:', SocketHelper.socketId);
|
|
25
|
-
console.log('👤 Person ID:', SocketHelper.personIdChurchId.personId);
|
|
26
|
-
console.log('⛪ Church ID:', SocketHelper.personIdChurchId.churchId);
|
|
27
|
-
|
|
28
|
-
if (SocketHelper.personIdChurchId.personId && SocketHelper.socketId) {
|
|
29
|
-
const connection: ConnectionInterface = {
|
|
30
|
-
conversationId: "alerts",
|
|
31
|
-
churchId: SocketHelper.personIdChurchId.churchId,
|
|
32
|
-
displayName: "Test",
|
|
33
|
-
socketId: SocketHelper.socketId,
|
|
34
|
-
personId: SocketHelper.personIdChurchId.personId
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
console.log('🔗 SocketHelper: Creating alert connection with data:', connection);
|
|
38
|
-
|
|
39
|
-
ApiHelper.postAnonymous("/connections", [connection], "MessagingApi").then((response: any) => {
|
|
40
|
-
console.log('✅ SocketHelper: Alert connection created successfully:', response);
|
|
41
|
-
}).catch((error: any) => {
|
|
42
|
-
console.error("❌ Failed to create alert connection:", error);
|
|
43
|
-
console.error("❌ Error details:", {
|
|
44
|
-
status: error.status,
|
|
45
|
-
message: error.message,
|
|
46
|
-
response: error.response
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
} else {
|
|
50
|
-
console.warn('⚠️ SocketHelper: Cannot create alert connection - missing data:', {
|
|
51
|
-
hasPersonId: !!SocketHelper.personIdChurchId.personId,
|
|
52
|
-
hasSocketId: !!SocketHelper.socketId,
|
|
53
|
-
personId: SocketHelper.personIdChurchId.personId,
|
|
54
|
-
socketId: SocketHelper.socketId
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
static init = async () => {
|
|
60
|
-
console.log('🔌 SocketHelper: Starting initialization...');
|
|
61
|
-
SocketHelper.cleanup();
|
|
62
|
-
SocketHelper.isCleanedUp = false;
|
|
63
|
-
|
|
64
|
-
if (SocketHelper.socket && SocketHelper.socket.readyState !== SocketHelper.socket.CLOSED) {
|
|
65
|
-
console.log('🔌 SocketHelper: Closing existing socket connection');
|
|
66
|
-
try {
|
|
67
|
-
SocketHelper.socket.close();
|
|
68
|
-
} catch (e) {
|
|
69
|
-
console.error("❌ Error closing existing socket:", e);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
console.log('🔌 SocketHelper: Connecting to:', CommonEnvironmentHelper.MessagingApiSocket);
|
|
74
|
-
|
|
75
|
-
await new Promise((resolve, reject) => {
|
|
76
|
-
let hasReceivedSocketId = false;
|
|
77
|
-
let messageCount = 0;
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
SocketHelper.socket = new WebSocket(CommonEnvironmentHelper.MessagingApiSocket);
|
|
81
|
-
|
|
82
|
-
SocketHelper.socket.onmessage = (event) => {
|
|
83
|
-
console.log("🔔 SocketHelper: onmessage event triggered");
|
|
84
|
-
if (SocketHelper.isCleanedUp) return;
|
|
85
|
-
|
|
86
|
-
messageCount++;
|
|
87
|
-
console.log(`📨 SocketHelper: Raw message #${messageCount} received:`, event.data);
|
|
88
|
-
console.log(`📨 SocketHelper: Message timestamp:`, new Date().toISOString());
|
|
89
|
-
console.log(`📨 SocketHelper: Message size:`, event.data.length, 'characters');
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
const payload = JSON.parse(event.data);
|
|
93
|
-
console.log(`📨 SocketHelper: Parsed message #${messageCount}:`, payload);
|
|
94
|
-
console.log(`📨 SocketHelper: Message action:`, payload.action);
|
|
95
|
-
console.log(`📨 SocketHelper: Message data type:`, typeof payload.data);
|
|
96
|
-
|
|
97
|
-
if (payload.action === 'socketId') {
|
|
98
|
-
hasReceivedSocketId = true;
|
|
99
|
-
console.log('🆔 SocketHelper: Socket ID received!', payload.data);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Log all message types we receive
|
|
103
|
-
switch (payload.action) {
|
|
104
|
-
case 'socketId':
|
|
105
|
-
console.log('🆔 Message type: Socket ID assignment');
|
|
106
|
-
break;
|
|
107
|
-
case 'privateMessage':
|
|
108
|
-
console.log('💬 Message type: Private message notification');
|
|
109
|
-
break;
|
|
110
|
-
case 'notification':
|
|
111
|
-
console.log('🔔 Message type: General notification');
|
|
112
|
-
break;
|
|
113
|
-
case 'message':
|
|
114
|
-
console.log('📩 Message type: Message update');
|
|
115
|
-
break;
|
|
116
|
-
case 'reconnect':
|
|
117
|
-
console.log('🔄 Message type: Reconnection signal');
|
|
118
|
-
break;
|
|
119
|
-
default:
|
|
120
|
-
console.log('❓ Message type: Unknown action -', payload.action);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
SocketHelper.handleMessage(payload);
|
|
124
|
-
} catch (error) {
|
|
125
|
-
console.error("❌ Error parsing socket message:", error);
|
|
126
|
-
console.error("❌ Raw message was:", event.data);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
SocketHelper.socket.onopen = async (e) => {
|
|
131
|
-
console.log('✅ SocketHelper: WebSocket connection opened');
|
|
132
|
-
console.log('🔌 SocketHelper: Connection URL:', SocketHelper.socket.url);
|
|
133
|
-
console.log('🔌 SocketHelper: Connection protocol:', SocketHelper.socket.protocol);
|
|
134
|
-
|
|
135
|
-
// Send the getId request
|
|
136
|
-
console.log('🔌 SocketHelper: Sending getId request...');
|
|
137
|
-
SocketHelper.socket.send("getId");
|
|
138
|
-
console.log('🔌 SocketHelper: getId request sent');
|
|
139
|
-
|
|
140
|
-
// Wait longer to see if we get a response
|
|
141
|
-
setTimeout(() => {
|
|
142
|
-
if (!hasReceivedSocketId) {
|
|
143
|
-
console.warn('⚠️ SocketHelper: No socket ID received after 3 seconds');
|
|
144
|
-
console.log('🔍 SocketHelper: Socket state:', SocketHelper.socket.readyState);
|
|
145
|
-
console.log('🔍 SocketHelper: Messages received so far:', messageCount);
|
|
146
|
-
}
|
|
147
|
-
resolve(null);
|
|
148
|
-
}, 3000);
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
SocketHelper.socket.onclose = async (e) => {
|
|
152
|
-
console.log('🔒 SocketHelper: WebSocket connection closed', {
|
|
153
|
-
code: e.code,
|
|
154
|
-
reason: e.reason,
|
|
155
|
-
wasClean: e.wasClean,
|
|
156
|
-
timestamp: new Date().toISOString(),
|
|
157
|
-
totalMessagesReceived: messageCount
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
// Log common close codes for debugging
|
|
161
|
-
if (e.code === 1005) console.log('🔍 Close code 1005: No status received (normal for some servers)');
|
|
162
|
-
else if (e.code === 1006) console.log('🔍 Close code 1006: Abnormal closure');
|
|
163
|
-
else if (e.code === 1000) console.log('🔍 Close code 1000: Normal closure');
|
|
164
|
-
else console.log('🔍 Close code:', e.code);
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
SocketHelper.socket.onerror = (error) => {
|
|
168
|
-
console.error('❌ SocketHelper: WebSocket connection error:', error);
|
|
169
|
-
console.error('❌ SocketHelper: Messages received before error:', messageCount);
|
|
170
|
-
reject(error);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
} catch (error) {
|
|
174
|
-
console.error('❌ SocketHelper: Error initializing socket:', error);
|
|
175
|
-
reject(error);
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
static addHandler = (action: string, id: string, handleMessage: (data: any) => void) => {
|
|
181
|
-
const existing = ArrayHelper.getOne(SocketHelper.actionHandlers, "id", id);
|
|
182
|
-
if (existing !== null) {
|
|
183
|
-
existing.handleMessage = handleMessage;
|
|
184
|
-
} else {
|
|
185
|
-
SocketHelper.actionHandlers.push({ action, id, handleMessage });
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
static removeHandler = (id: string) => {
|
|
190
|
-
SocketHelper.actionHandlers = SocketHelper.actionHandlers.filter(handler => handler.id !== id);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
static removeHandlersByAction = (action: string) => {
|
|
194
|
-
SocketHelper.actionHandlers = SocketHelper.actionHandlers.filter(handler => handler.action !== action);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
static clearAllHandlers = () => {
|
|
198
|
-
SocketHelper.actionHandlers = [];
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
static handleMessage = (payload: SocketPayloadInterface) => {
|
|
202
|
-
if (SocketHelper.isCleanedUp) return;
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
console.log('🔄 SocketHelper: Processing message with action:', payload.action);
|
|
206
|
-
|
|
207
|
-
if (payload.action === "socketId") {
|
|
208
|
-
console.log('🆔 SocketHelper: Received socket ID:', payload.data);
|
|
209
|
-
SocketHelper.socketId = payload.data;
|
|
210
|
-
SocketHelper.createAlertConnection();
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
const matchingHandlers = ArrayHelper.getAll(SocketHelper.actionHandlers, "action", payload.action);
|
|
214
|
-
console.log(`📬 SocketHelper: Found ${matchingHandlers.length} handlers for action: ${payload.action}`);
|
|
215
|
-
|
|
216
|
-
matchingHandlers.forEach((handler) => {
|
|
217
|
-
try {
|
|
218
|
-
console.log(`🏃 SocketHelper: Executing handler ${handler.id} for action: ${payload.action}`);
|
|
219
|
-
handler.handleMessage(payload.data);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
console.error(`❌ Error in handler ${handler.id}:`, error);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
} catch (error) {
|
|
226
|
-
console.error("❌ Error handling socket message:", error);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
static cleanup = () => {
|
|
231
|
-
SocketHelper.isCleanedUp = true;
|
|
232
|
-
|
|
233
|
-
// Close socket connection
|
|
234
|
-
if (SocketHelper.socket && SocketHelper.socket.readyState !== SocketHelper.socket.CLOSED) {
|
|
235
|
-
try {
|
|
236
|
-
SocketHelper.socket.close();
|
|
237
|
-
} catch (error) {
|
|
238
|
-
console.error("Error closing socket:", error);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Clear references
|
|
243
|
-
SocketHelper.socket = null;
|
|
244
|
-
SocketHelper.socketId = null;
|
|
245
|
-
SocketHelper.actionHandlers = [];
|
|
246
|
-
SocketHelper.personIdChurchId = { personId: "", churchId: "" };
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
static disconnect = () => {
|
|
250
|
-
SocketHelper.cleanup();
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
static isConnected = (): boolean => {
|
|
254
|
-
return SocketHelper.socket && SocketHelper.socket.readyState === SocketHelper.socket.OPEN;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
static getConnectionState = (): string => {
|
|
258
|
-
if (!SocketHelper.socket) return "UNINITIALIZED";
|
|
259
|
-
|
|
260
|
-
switch (SocketHelper.socket.readyState) {
|
|
261
|
-
case SocketHelper.socket.CONNECTING:
|
|
262
|
-
return "CONNECTING";
|
|
263
|
-
case SocketHelper.socket.OPEN:
|
|
264
|
-
return "OPEN";
|
|
265
|
-
case SocketHelper.socket.CLOSING:
|
|
266
|
-
return "CLOSING";
|
|
267
|
-
case SocketHelper.socket.CLOSED:
|
|
268
|
-
return "CLOSED";
|
|
269
|
-
default:
|
|
270
|
-
return "UNKNOWN";
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// Global cleanup on window unload
|
|
275
|
-
static setupGlobalCleanup = () => {
|
|
276
|
-
if (typeof window !== "undefined") {
|
|
277
|
-
const cleanup = () => {
|
|
278
|
-
SocketHelper.cleanup();
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
window.addEventListener("beforeunload", cleanup);
|
|
282
|
-
window.addEventListener("unload", cleanup);
|
|
283
|
-
|
|
284
|
-
// Also cleanup on page visibility change (when tab is closed)
|
|
285
|
-
document.addEventListener("visibilitychange", () => {
|
|
286
|
-
if (document.visibilityState === "hidden") {
|
|
287
|
-
// Optional: cleanup when tab becomes hidden
|
|
288
|
-
// SocketHelper.cleanup();
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
return cleanup;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
}
|
|
1
|
+
import { ConnectionInterface, SocketActionHandlerInterface, SocketPayloadInterface, ApiHelper, ArrayHelper, CommonEnvironmentHelper } from "@churchapps/helpers";
|
|
2
|
+
|
|
3
|
+
export class SocketHelper {
|
|
4
|
+
static socket: WebSocket;
|
|
5
|
+
static socketId: string;
|
|
6
|
+
static actionHandlers: SocketActionHandlerInterface[] = [];
|
|
7
|
+
private static personIdChurchId: { personId: string, churchId: string } = { personId: "", churchId: "" };
|
|
8
|
+
private static isCleanedUp: boolean = false;
|
|
9
|
+
|
|
10
|
+
static setPersonChurch = (pc: { personId: string, churchId: string }) => {
|
|
11
|
+
console.log('👤 SocketHelper: Setting person/church context:', pc);
|
|
12
|
+
|
|
13
|
+
if (pc?.personId && pc.personId && pc.churchId !== this.personIdChurchId.churchId && pc.personId !== this.personIdChurchId.personId) {
|
|
14
|
+
this.personIdChurchId = pc;
|
|
15
|
+
console.log('🔗 SocketHelper: Person/church context updated, creating alert connection');
|
|
16
|
+
this.createAlertConnection();
|
|
17
|
+
} else {
|
|
18
|
+
console.log('⚠️ SocketHelper: Person/church context unchanged or invalid');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static createAlertConnection = () => {
|
|
23
|
+
console.log('🔗 SocketHelper: Attempting to create alert connection...');
|
|
24
|
+
console.log('🆔 Socket ID:', SocketHelper.socketId);
|
|
25
|
+
console.log('👤 Person ID:', SocketHelper.personIdChurchId.personId);
|
|
26
|
+
console.log('⛪ Church ID:', SocketHelper.personIdChurchId.churchId);
|
|
27
|
+
|
|
28
|
+
if (SocketHelper.personIdChurchId.personId && SocketHelper.socketId) {
|
|
29
|
+
const connection: ConnectionInterface = {
|
|
30
|
+
conversationId: "alerts",
|
|
31
|
+
churchId: SocketHelper.personIdChurchId.churchId,
|
|
32
|
+
displayName: "Test",
|
|
33
|
+
socketId: SocketHelper.socketId,
|
|
34
|
+
personId: SocketHelper.personIdChurchId.personId
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
console.log('🔗 SocketHelper: Creating alert connection with data:', connection);
|
|
38
|
+
|
|
39
|
+
ApiHelper.postAnonymous("/connections", [connection], "MessagingApi").then((response: any) => {
|
|
40
|
+
console.log('✅ SocketHelper: Alert connection created successfully:', response);
|
|
41
|
+
}).catch((error: any) => {
|
|
42
|
+
console.error("❌ Failed to create alert connection:", error);
|
|
43
|
+
console.error("❌ Error details:", {
|
|
44
|
+
status: error.status,
|
|
45
|
+
message: error.message,
|
|
46
|
+
response: error.response
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
console.warn('⚠️ SocketHelper: Cannot create alert connection - missing data:', {
|
|
51
|
+
hasPersonId: !!SocketHelper.personIdChurchId.personId,
|
|
52
|
+
hasSocketId: !!SocketHelper.socketId,
|
|
53
|
+
personId: SocketHelper.personIdChurchId.personId,
|
|
54
|
+
socketId: SocketHelper.socketId
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static init = async () => {
|
|
60
|
+
console.log('🔌 SocketHelper: Starting initialization...');
|
|
61
|
+
SocketHelper.cleanup();
|
|
62
|
+
SocketHelper.isCleanedUp = false;
|
|
63
|
+
|
|
64
|
+
if (SocketHelper.socket && SocketHelper.socket.readyState !== SocketHelper.socket.CLOSED) {
|
|
65
|
+
console.log('🔌 SocketHelper: Closing existing socket connection');
|
|
66
|
+
try {
|
|
67
|
+
SocketHelper.socket.close();
|
|
68
|
+
} catch (e) {
|
|
69
|
+
console.error("❌ Error closing existing socket:", e);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
console.log('🔌 SocketHelper: Connecting to:', CommonEnvironmentHelper.MessagingApiSocket);
|
|
74
|
+
|
|
75
|
+
await new Promise((resolve, reject) => {
|
|
76
|
+
let hasReceivedSocketId = false;
|
|
77
|
+
let messageCount = 0;
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
SocketHelper.socket = new WebSocket(CommonEnvironmentHelper.MessagingApiSocket);
|
|
81
|
+
|
|
82
|
+
SocketHelper.socket.onmessage = (event) => {
|
|
83
|
+
console.log("🔔 SocketHelper: onmessage event triggered");
|
|
84
|
+
if (SocketHelper.isCleanedUp) return;
|
|
85
|
+
|
|
86
|
+
messageCount++;
|
|
87
|
+
console.log(`📨 SocketHelper: Raw message #${messageCount} received:`, event.data);
|
|
88
|
+
console.log(`📨 SocketHelper: Message timestamp:`, new Date().toISOString());
|
|
89
|
+
console.log(`📨 SocketHelper: Message size:`, event.data.length, 'characters');
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const payload = JSON.parse(event.data);
|
|
93
|
+
console.log(`📨 SocketHelper: Parsed message #${messageCount}:`, payload);
|
|
94
|
+
console.log(`📨 SocketHelper: Message action:`, payload.action);
|
|
95
|
+
console.log(`📨 SocketHelper: Message data type:`, typeof payload.data);
|
|
96
|
+
|
|
97
|
+
if (payload.action === 'socketId') {
|
|
98
|
+
hasReceivedSocketId = true;
|
|
99
|
+
console.log('🆔 SocketHelper: Socket ID received!', payload.data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Log all message types we receive
|
|
103
|
+
switch (payload.action) {
|
|
104
|
+
case 'socketId':
|
|
105
|
+
console.log('🆔 Message type: Socket ID assignment');
|
|
106
|
+
break;
|
|
107
|
+
case 'privateMessage':
|
|
108
|
+
console.log('💬 Message type: Private message notification');
|
|
109
|
+
break;
|
|
110
|
+
case 'notification':
|
|
111
|
+
console.log('🔔 Message type: General notification');
|
|
112
|
+
break;
|
|
113
|
+
case 'message':
|
|
114
|
+
console.log('📩 Message type: Message update');
|
|
115
|
+
break;
|
|
116
|
+
case 'reconnect':
|
|
117
|
+
console.log('🔄 Message type: Reconnection signal');
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
console.log('❓ Message type: Unknown action -', payload.action);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
SocketHelper.handleMessage(payload);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error("❌ Error parsing socket message:", error);
|
|
126
|
+
console.error("❌ Raw message was:", event.data);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
SocketHelper.socket.onopen = async (e) => {
|
|
131
|
+
console.log('✅ SocketHelper: WebSocket connection opened');
|
|
132
|
+
console.log('🔌 SocketHelper: Connection URL:', SocketHelper.socket.url);
|
|
133
|
+
console.log('🔌 SocketHelper: Connection protocol:', SocketHelper.socket.protocol);
|
|
134
|
+
|
|
135
|
+
// Send the getId request
|
|
136
|
+
console.log('🔌 SocketHelper: Sending getId request...');
|
|
137
|
+
SocketHelper.socket.send("getId");
|
|
138
|
+
console.log('🔌 SocketHelper: getId request sent');
|
|
139
|
+
|
|
140
|
+
// Wait longer to see if we get a response
|
|
141
|
+
setTimeout(() => {
|
|
142
|
+
if (!hasReceivedSocketId) {
|
|
143
|
+
console.warn('⚠️ SocketHelper: No socket ID received after 3 seconds');
|
|
144
|
+
console.log('🔍 SocketHelper: Socket state:', SocketHelper.socket.readyState);
|
|
145
|
+
console.log('🔍 SocketHelper: Messages received so far:', messageCount);
|
|
146
|
+
}
|
|
147
|
+
resolve(null);
|
|
148
|
+
}, 3000);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
SocketHelper.socket.onclose = async (e) => {
|
|
152
|
+
console.log('🔒 SocketHelper: WebSocket connection closed', {
|
|
153
|
+
code: e.code,
|
|
154
|
+
reason: e.reason,
|
|
155
|
+
wasClean: e.wasClean,
|
|
156
|
+
timestamp: new Date().toISOString(),
|
|
157
|
+
totalMessagesReceived: messageCount
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// Log common close codes for debugging
|
|
161
|
+
if (e.code === 1005) console.log('🔍 Close code 1005: No status received (normal for some servers)');
|
|
162
|
+
else if (e.code === 1006) console.log('🔍 Close code 1006: Abnormal closure');
|
|
163
|
+
else if (e.code === 1000) console.log('🔍 Close code 1000: Normal closure');
|
|
164
|
+
else console.log('🔍 Close code:', e.code);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
SocketHelper.socket.onerror = (error) => {
|
|
168
|
+
console.error('❌ SocketHelper: WebSocket connection error:', error);
|
|
169
|
+
console.error('❌ SocketHelper: Messages received before error:', messageCount);
|
|
170
|
+
reject(error);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
} catch (error) {
|
|
174
|
+
console.error('❌ SocketHelper: Error initializing socket:', error);
|
|
175
|
+
reject(error);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static addHandler = (action: string, id: string, handleMessage: (data: any) => void) => {
|
|
181
|
+
const existing = ArrayHelper.getOne(SocketHelper.actionHandlers, "id", id);
|
|
182
|
+
if (existing !== null) {
|
|
183
|
+
existing.handleMessage = handleMessage;
|
|
184
|
+
} else {
|
|
185
|
+
SocketHelper.actionHandlers.push({ action, id, handleMessage });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static removeHandler = (id: string) => {
|
|
190
|
+
SocketHelper.actionHandlers = SocketHelper.actionHandlers.filter(handler => handler.id !== id);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
static removeHandlersByAction = (action: string) => {
|
|
194
|
+
SocketHelper.actionHandlers = SocketHelper.actionHandlers.filter(handler => handler.action !== action);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static clearAllHandlers = () => {
|
|
198
|
+
SocketHelper.actionHandlers = [];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
static handleMessage = (payload: SocketPayloadInterface) => {
|
|
202
|
+
if (SocketHelper.isCleanedUp) return;
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
console.log('🔄 SocketHelper: Processing message with action:', payload.action);
|
|
206
|
+
|
|
207
|
+
if (payload.action === "socketId") {
|
|
208
|
+
console.log('🆔 SocketHelper: Received socket ID:', payload.data);
|
|
209
|
+
SocketHelper.socketId = payload.data;
|
|
210
|
+
SocketHelper.createAlertConnection();
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
const matchingHandlers = ArrayHelper.getAll(SocketHelper.actionHandlers, "action", payload.action);
|
|
214
|
+
console.log(`📬 SocketHelper: Found ${matchingHandlers.length} handlers for action: ${payload.action}`);
|
|
215
|
+
|
|
216
|
+
matchingHandlers.forEach((handler) => {
|
|
217
|
+
try {
|
|
218
|
+
console.log(`🏃 SocketHelper: Executing handler ${handler.id} for action: ${payload.action}`);
|
|
219
|
+
handler.handleMessage(payload.data);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.error(`❌ Error in handler ${handler.id}:`, error);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.error("❌ Error handling socket message:", error);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static cleanup = () => {
|
|
231
|
+
SocketHelper.isCleanedUp = true;
|
|
232
|
+
|
|
233
|
+
// Close socket connection
|
|
234
|
+
if (SocketHelper.socket && SocketHelper.socket.readyState !== SocketHelper.socket.CLOSED) {
|
|
235
|
+
try {
|
|
236
|
+
SocketHelper.socket.close();
|
|
237
|
+
} catch (error) {
|
|
238
|
+
console.error("Error closing socket:", error);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Clear references
|
|
243
|
+
SocketHelper.socket = null;
|
|
244
|
+
SocketHelper.socketId = null;
|
|
245
|
+
SocketHelper.actionHandlers = [];
|
|
246
|
+
SocketHelper.personIdChurchId = { personId: "", churchId: "" };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static disconnect = () => {
|
|
250
|
+
SocketHelper.cleanup();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static isConnected = (): boolean => {
|
|
254
|
+
return SocketHelper.socket && SocketHelper.socket.readyState === SocketHelper.socket.OPEN;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
static getConnectionState = (): string => {
|
|
258
|
+
if (!SocketHelper.socket) return "UNINITIALIZED";
|
|
259
|
+
|
|
260
|
+
switch (SocketHelper.socket.readyState) {
|
|
261
|
+
case SocketHelper.socket.CONNECTING:
|
|
262
|
+
return "CONNECTING";
|
|
263
|
+
case SocketHelper.socket.OPEN:
|
|
264
|
+
return "OPEN";
|
|
265
|
+
case SocketHelper.socket.CLOSING:
|
|
266
|
+
return "CLOSING";
|
|
267
|
+
case SocketHelper.socket.CLOSED:
|
|
268
|
+
return "CLOSED";
|
|
269
|
+
default:
|
|
270
|
+
return "UNKNOWN";
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Global cleanup on window unload
|
|
275
|
+
static setupGlobalCleanup = () => {
|
|
276
|
+
if (typeof window !== "undefined") {
|
|
277
|
+
const cleanup = () => {
|
|
278
|
+
SocketHelper.cleanup();
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
window.addEventListener("beforeunload", cleanup);
|
|
282
|
+
window.addEventListener("unload", cleanup);
|
|
283
|
+
|
|
284
|
+
// Also cleanup on page visibility change (when tab is closed)
|
|
285
|
+
document.addEventListener("visibilitychange", () => {
|
|
286
|
+
if (document.visibilityState === "hidden") {
|
|
287
|
+
// Optional: cleanup when tab becomes hidden
|
|
288
|
+
// SocketHelper.cleanup();
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return cleanup;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
}
|