ajaxter-chat 3.0.16 → 3.1.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/BlockList/index.d.ts +1 -0
- package/dist/components/BlockList/index.d.ts.map +1 -0
- package/dist/components/BlockList/index.js +55 -28
- package/dist/components/BlockList/index.js.map +1 -0
- package/dist/components/CallScreen/index.d.ts +3 -0
- package/dist/components/CallScreen/index.d.ts.map +1 -0
- package/dist/components/CallScreen/index.js +107 -29
- package/dist/components/CallScreen/index.js.map +1 -0
- package/dist/components/ChatScreen/index.d.ts +1 -0
- package/dist/components/ChatScreen/index.d.ts.map +1 -0
- package/dist/components/ChatScreen/index.js +493 -294
- package/dist/components/ChatScreen/index.js.map +1 -0
- package/dist/components/ChatWidget.d.ts +1 -0
- package/dist/components/ChatWidget.d.ts.map +1 -0
- package/dist/components/ChatWidget.js +359 -250
- package/dist/components/ChatWidget.js.map +1 -0
- package/dist/components/EmojiPicker/index.d.ts +1 -0
- package/dist/components/EmojiPicker/index.d.ts.map +1 -0
- package/dist/components/EmojiPicker/index.js +19 -7
- package/dist/components/EmojiPicker/index.js.map +1 -0
- package/dist/components/ErrorBoundary/index.d.ts +20 -0
- package/dist/components/ErrorBoundary/index.d.ts.map +1 -0
- package/dist/components/ErrorBoundary/index.js +76 -0
- package/dist/components/ErrorBoundary/index.js.map +1 -0
- package/dist/components/HomeScreen/index.d.ts +1 -0
- package/dist/components/HomeScreen/index.d.ts.map +1 -0
- package/dist/components/HomeScreen/index.js +236 -158
- package/dist/components/HomeScreen/index.js.map +1 -0
- package/dist/components/MaintenanceView/index.d.ts +1 -0
- package/dist/components/MaintenanceView/index.d.ts.map +1 -0
- package/dist/components/MaintenanceView/index.js +28 -12
- package/dist/components/MaintenanceView/index.js.map +1 -0
- package/dist/components/MiniCallBar/index.d.ts +15 -0
- package/dist/components/MiniCallBar/index.d.ts.map +1 -0
- package/dist/components/MiniCallBar/index.js +116 -0
- package/dist/components/MiniCallBar/index.js.map +1 -0
- package/dist/components/PermissionsGateScreen/index.d.ts +1 -0
- package/dist/components/PermissionsGateScreen/index.d.ts.map +1 -0
- package/dist/components/PermissionsGateScreen/index.js +82 -28
- package/dist/components/PermissionsGateScreen/index.js.map +1 -0
- package/dist/components/RecentChatsScreen/index.d.ts +1 -0
- package/dist/components/RecentChatsScreen/index.d.ts.map +1 -0
- package/dist/components/RecentChatsScreen/index.js +79 -19
- package/dist/components/RecentChatsScreen/index.js.map +1 -0
- package/dist/components/SlideNavMenu.d.ts +1 -0
- package/dist/components/SlideNavMenu.d.ts.map +1 -0
- package/dist/components/SlideNavMenu.js +82 -63
- package/dist/components/SlideNavMenu.js.map +1 -0
- package/dist/components/Tabs/BottomTabs.d.ts +1 -0
- package/dist/components/Tabs/BottomTabs.d.ts.map +1 -0
- package/dist/components/Tabs/BottomTabs.js +34 -19
- package/dist/components/Tabs/BottomTabs.js.map +1 -0
- package/dist/components/TicketDetailScreen/index.d.ts +1 -0
- package/dist/components/TicketDetailScreen/index.d.ts.map +1 -0
- package/dist/components/TicketDetailScreen/index.js +66 -27
- package/dist/components/TicketDetailScreen/index.js.map +1 -0
- package/dist/components/TicketFormScreen/index.d.ts +1 -0
- package/dist/components/TicketFormScreen/index.d.ts.map +1 -0
- package/dist/components/TicketFormScreen/index.js +99 -49
- package/dist/components/TicketFormScreen/index.js.map +1 -0
- package/dist/components/TicketScreen/index.d.ts +1 -0
- package/dist/components/TicketScreen/index.d.ts.map +1 -0
- package/dist/components/TicketScreen/index.js +95 -26
- package/dist/components/TicketScreen/index.js.map +1 -0
- package/dist/components/UserListScreen/index.d.ts +1 -0
- package/dist/components/UserListScreen/index.d.ts.map +1 -0
- package/dist/components/UserListScreen/index.js +127 -53
- package/dist/components/UserListScreen/index.js.map +1 -0
- package/dist/components/ViewerBlockedScreen/index.d.ts +1 -0
- package/dist/components/ViewerBlockedScreen/index.d.ts.map +1 -0
- package/dist/components/ViewerBlockedScreen/index.js +113 -61
- package/dist/components/ViewerBlockedScreen/index.js.map +1 -0
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +7 -2
- package/dist/config/index.js.map +1 -0
- package/dist/hooks/useChat.d.ts +9 -1
- package/dist/hooks/useChat.d.ts.map +1 -0
- package/dist/hooks/useChat.js +60 -18
- package/dist/hooks/useChat.js.map +1 -0
- package/dist/hooks/useRemoteConfig.d.ts +1 -0
- package/dist/hooks/useRemoteConfig.d.ts.map +1 -0
- package/dist/hooks/useRemoteConfig.js +12 -8
- package/dist/hooks/useRemoteConfig.js.map +1 -0
- package/dist/hooks/useSocket.d.ts +40 -0
- package/dist/hooks/useSocket.d.ts.map +1 -0
- package/dist/hooks/useSocket.js +190 -0
- package/dist/hooks/useSocket.js.map +1 -0
- package/dist/hooks/useWebRTC.d.ts +10 -2
- package/dist/hooks/useWebRTC.d.ts.map +1 -0
- package/dist/hooks/useWebRTC.js +101 -69
- package/dist/hooks/useWebRTC.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +67 -21
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -1
- package/dist/types/index.js.map +1 -0
- package/dist/utils/chat.d.ts +1 -0
- package/dist/utils/chat.d.ts.map +1 -0
- package/dist/utils/chat.js +17 -7
- package/dist/utils/chat.js.map +1 -0
- package/dist/utils/fileName.d.ts +1 -0
- package/dist/utils/fileName.d.ts.map +1 -0
- package/dist/utils/fileName.js +5 -1
- package/dist/utils/fileName.js.map +1 -0
- package/dist/utils/messageSound.d.ts +1 -0
- package/dist/utils/messageSound.d.ts.map +1 -0
- package/dist/utils/messageSound.js +9 -3
- package/dist/utils/messageSound.js.map +1 -0
- package/dist/utils/presenceStatus.d.ts +1 -0
- package/dist/utils/presenceStatus.d.ts.map +1 -0
- package/dist/utils/presenceStatus.js +11 -4
- package/dist/utils/presenceStatus.js.map +1 -0
- package/dist/utils/privacyConsent.d.ts +1 -0
- package/dist/utils/privacyConsent.d.ts.map +1 -0
- package/dist/utils/privacyConsent.js +9 -3
- package/dist/utils/privacyConsent.js.map +1 -0
- package/dist/utils/reenableRequest.d.ts +1 -0
- package/dist/utils/reenableRequest.d.ts.map +1 -0
- package/dist/utils/reenableRequest.js +5 -1
- package/dist/utils/reenableRequest.js.map +1 -0
- package/dist/utils/theme.d.ts +1 -0
- package/dist/utils/theme.d.ts.map +1 -0
- package/dist/utils/theme.js +10 -4
- package/dist/utils/theme.js.map +1 -0
- package/dist/utils/widgetPermissions.d.ts +1 -0
- package/dist/utils/widgetPermissions.d.ts.map +1 -0
- package/dist/utils/widgetPermissions.js +13 -5
- package/dist/utils/widgetPermissions.js.map +1 -0
- package/dist/utils/widgetSession.d.ts +1 -0
- package/dist/utils/widgetSession.d.ts.map +1 -0
- package/dist/utils/widgetSession.js +9 -3
- package/dist/utils/widgetSession.js.map +1 -0
- package/package.json +3 -3
- package/src/components/CallScreen/index.tsx +23 -1
- package/src/components/ChatScreen/index.tsx +2 -1
- package/src/components/ChatWidget.tsx +314 -263
- package/src/components/ErrorBoundary/index.tsx +62 -0
- package/src/components/HomeScreen/index.tsx +0 -3
- package/src/components/MiniCallBar/index.tsx +150 -0
- package/src/hooks/useChat.ts +59 -12
- package/src/hooks/useSocket.ts +228 -0
- package/src/hooks/useWebRTC.ts +99 -64
- package/src/index.ts +7 -2
package/dist/hooks/useChat.js
CHANGED
|
@@ -1,30 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const [
|
|
7
|
-
const
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useChat = useChat;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useChat(initialMessages = [], opts = {}) {
|
|
6
|
+
const [messages, setMessages] = (0, react_1.useState)(initialMessages);
|
|
7
|
+
const [activeUser, setActiveUser] = (0, react_1.useState)(null);
|
|
8
|
+
const [isPaused, setIsPaused] = (0, react_1.useState)(false);
|
|
9
|
+
const [isReported, setIsReported] = (0, react_1.useState)(false);
|
|
10
|
+
const activeUserRef = (0, react_1.useRef)(null);
|
|
11
|
+
const isPausedRef = (0, react_1.useRef)(false);
|
|
12
|
+
const { onEmitMessage, onEmitPause, onEmitReport } = opts;
|
|
13
|
+
const selectUser = (0, react_1.useCallback)((user, history = []) => {
|
|
8
14
|
setActiveUser(user);
|
|
15
|
+
activeUserRef.current = user;
|
|
9
16
|
setMessages(history);
|
|
10
17
|
setIsPaused(false);
|
|
18
|
+
isPausedRef.current = false;
|
|
11
19
|
setIsReported(false);
|
|
12
|
-
// TODO: socket.emit('join', { roomId: user.uid });
|
|
13
|
-
// TODO: socket.on('message', msg => setMessages(prev => [...prev, msg]));
|
|
14
20
|
}, []);
|
|
15
|
-
const sendMessage = useCallback((text, type = 'text', extra = {}) => {
|
|
16
|
-
|
|
21
|
+
const sendMessage = (0, react_1.useCallback)((text, type = 'text', extra = {}) => {
|
|
22
|
+
const user = activeUserRef.current;
|
|
23
|
+
if (!user || isPausedRef.current)
|
|
17
24
|
return;
|
|
18
|
-
const msg = Object.assign({ id: `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`, senderId: 'me', receiverId:
|
|
25
|
+
const msg = Object.assign({ id: `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`, senderId: 'me', receiverId: user.uid, text, timestamp: new Date().toISOString(), type, status: 'sent' }, extra);
|
|
19
26
|
setMessages(prev => [...prev, msg]);
|
|
20
|
-
|
|
21
|
-
}, [
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
onEmitMessage === null || onEmitMessage === void 0 ? void 0 : onEmitMessage(msg);
|
|
28
|
+
}, [onEmitMessage]);
|
|
29
|
+
const receiveMessage = (0, react_1.useCallback)((msg) => {
|
|
30
|
+
setMessages(prev => {
|
|
31
|
+
if (prev.some(m => m.id === msg.id))
|
|
32
|
+
return prev;
|
|
33
|
+
return [...prev, msg];
|
|
34
|
+
});
|
|
35
|
+
}, []);
|
|
36
|
+
const updateMessageStatus = (0, react_1.useCallback)((messageId, status) => {
|
|
37
|
+
setMessages(prev => prev.map(m => m.id === messageId ? Object.assign(Object.assign({}, m), { status }) : m));
|
|
38
|
+
}, []);
|
|
39
|
+
const togglePause = (0, react_1.useCallback)(() => {
|
|
40
|
+
const user = activeUserRef.current;
|
|
41
|
+
if (!user)
|
|
42
|
+
return;
|
|
43
|
+
const next = !isPausedRef.current;
|
|
44
|
+
setIsPaused(next);
|
|
45
|
+
isPausedRef.current = next;
|
|
46
|
+
const roomId = [user.uid, 'me'].sort().join('_');
|
|
47
|
+
onEmitPause === null || onEmitPause === void 0 ? void 0 : onEmitPause(roomId, user.uid, next);
|
|
48
|
+
}, [onEmitPause]);
|
|
49
|
+
const reportChat = (0, react_1.useCallback)(() => {
|
|
50
|
+
const user = activeUserRef.current;
|
|
51
|
+
setIsReported(true);
|
|
52
|
+
if (user) {
|
|
53
|
+
const roomId = [user.uid, 'me'].sort().join('_');
|
|
54
|
+
onEmitReport === null || onEmitReport === void 0 ? void 0 : onEmitReport(roomId);
|
|
55
|
+
}
|
|
56
|
+
}, [onEmitReport]);
|
|
57
|
+
const clearChat = (0, react_1.useCallback)(() => {
|
|
58
|
+
setMessages([]);
|
|
59
|
+
setActiveUser(null);
|
|
60
|
+
activeUserRef.current = null;
|
|
61
|
+
setIsPaused(false);
|
|
62
|
+
isPausedRef.current = false;
|
|
63
|
+
setIsReported(false);
|
|
64
|
+
}, []);
|
|
25
65
|
return {
|
|
26
66
|
messages, activeUser, isPaused, isReported,
|
|
27
|
-
selectUser, sendMessage,
|
|
67
|
+
selectUser, sendMessage, receiveMessage, updateMessageStatus,
|
|
68
|
+
togglePause, reportChat, clearChat,
|
|
28
69
|
setMessages,
|
|
29
70
|
};
|
|
30
71
|
}
|
|
72
|
+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KAAK,CAAC,CAAC;QACnB,WAAW,CAAC,OAAO,GAAG,KAAK,CAAC;QAC5B,aAAa,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;QACL,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU;QAC1C,UAAU,EAAE,WAAW,EAAE,cAAc,EAAE,mBAAmB;QAC5D,WAAW,EAAE,UAAU,EAAE,SAAS;QAClC,WAAW;KACZ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRemoteConfig.d.ts","sourceRoot":"","sources":["../../src/hooks/useRemoteConfig.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAG1C,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;;;;EAqB/D"}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useRemoteConfig = useRemoteConfig;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const config_1 = require("../config");
|
|
6
|
+
function useRemoteConfig(apiKey, widgetId) {
|
|
7
|
+
const [data, setData] = (0, react_1.useState)(null);
|
|
8
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
9
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
10
|
+
(0, react_1.useEffect)(() => {
|
|
8
11
|
let cancelled = false;
|
|
9
12
|
setLoading(true);
|
|
10
|
-
fetchRemoteChatData(apiKey, widgetId)
|
|
13
|
+
(0, config_1.fetchRemoteChatData)(apiKey, widgetId)
|
|
11
14
|
.then(d => { if (!cancelled) {
|
|
12
15
|
setData(d);
|
|
13
16
|
setError(null);
|
|
@@ -24,3 +27,4 @@ export function useRemoteConfig(apiKey, widgetId) {
|
|
|
24
27
|
}, [apiKey, widgetId]);
|
|
25
28
|
return { data, loading, error };
|
|
26
29
|
}
|
|
30
|
+
//# sourceMappingURL=useRemoteConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRemoteConfig.js","sourceRoot":"","sources":["../../src/hooks/useRemoteConfig.ts"],"names":[],"mappings":";;AAIA,0CAqBC;AAzBD,iCAA4C;AAE5C,sCAAgD;AAEhD,SAAgB,eAAe,CAAC,MAAc,EAAE,QAAgB;IAC9D,MAAM,CAAC,IAAI,EAAK,OAAO,CAAC,GAAM,IAAA,gBAAQ,EAAwB,IAAI,CAAC,CAAC;IACpE,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAC,IAAI,CAAC,CAAC;IAC7C,MAAM,CAAC,KAAK,EAAI,QAAQ,CAAC,GAAK,IAAA,gBAAQ,EAAgB,IAAI,CAAC,CAAC;IAE5D,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,SAAS,GAAG,KAAK,CAAC;QACtB,UAAU,CAAC,IAAI,CAAC,CAAC;QACjB,IAAA,4BAAmB,EAAC,MAAM,EAAE,QAAQ,CAAC;aAClC,IAAI,CAAC,CAAC,CAAE,EAAE,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;YAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAAC,CAAC,CAAC,CAAC,CAAC;aAClF,KAAK,CAAC,CAAC,CAAC,EAAE;YACT,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,MAAM,GAAG,GAAG,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;gBACvD,QAAQ,CAAC,GAAG,IAAI,2CAA2C,CAAC,CAAC;gBAC7D,UAAU,CAAC,KAAK,CAAC,CAAC;YACpB,CAAC;QACH,CAAC,CAAC,CAAC;QACL,OAAO,GAAG,EAAE,GAAG,SAAS,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACrC,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IAEvB,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ChatMessage } from '../types';
|
|
2
|
+
export type SocketStatus = 'connecting' | 'connected' | 'disconnected' | 'error';
|
|
3
|
+
export interface SocketMessageAck {
|
|
4
|
+
messageId: string;
|
|
5
|
+
status: 'delivered' | 'read';
|
|
6
|
+
}
|
|
7
|
+
export interface UseSocketOptions {
|
|
8
|
+
widgetId: string;
|
|
9
|
+
viewerUid: string;
|
|
10
|
+
/** WebSocket server URL — defaults to http://localhost:3005 */
|
|
11
|
+
serverUrl?: string;
|
|
12
|
+
onMessage?: (msg: ChatMessage) => void;
|
|
13
|
+
onMessageAck?: (ack: SocketMessageAck) => void;
|
|
14
|
+
onChatPaused?: (roomId: string, paused: boolean) => void;
|
|
15
|
+
onCallOffer?: (offer: RTCSessionDescriptionInit, fromUid: string, callId: string) => void;
|
|
16
|
+
onCallAnswer?: (answer: RTCSessionDescriptionInit, fromUid: string, callId: string) => void;
|
|
17
|
+
onIceCandidate?: (candidate: RTCIceCandidateInit, fromUid: string) => void;
|
|
18
|
+
onCallEnd?: (callId: string) => void;
|
|
19
|
+
onError?: (code: string, message: string) => void;
|
|
20
|
+
onUserStatus?: (uid: string, status: 'online' | 'away' | 'offline') => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function useSocket(opts: UseSocketOptions): {
|
|
23
|
+
status: SocketStatus;
|
|
24
|
+
joinRoom: (roomId: string) => void;
|
|
25
|
+
leaveRoom: (roomId: string) => void;
|
|
26
|
+
emitMessage: (msg: ChatMessage) => void;
|
|
27
|
+
emitTypingStart: (roomId: string) => void;
|
|
28
|
+
emitTypingStop: (roomId: string) => void;
|
|
29
|
+
emitPauseToggle: (roomId: string, targetUserId: string, paused: boolean) => void;
|
|
30
|
+
emitReport: (roomId: string, reason?: string) => void;
|
|
31
|
+
emitBlock: (blockedUid: string) => void;
|
|
32
|
+
emitUnblock: (blockedUid: string) => void;
|
|
33
|
+
emitTransfer: (fromRoomId: string, toUserId: string, note?: string) => void;
|
|
34
|
+
emitCallOffer: (offer: RTCSessionDescriptionInit, toUid: string, callId: string) => void;
|
|
35
|
+
emitCallAnswer: (answer: RTCSessionDescriptionInit, toUid: string, callId: string) => void;
|
|
36
|
+
emitIceCandidate: (candidate: RTCIceCandidateInit, toUid: string) => void;
|
|
37
|
+
emitCallEnd: (callId: string, toUid?: string) => void;
|
|
38
|
+
emitAddParticipant: (roomId: string, uid: string) => void;
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=useSocket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSocket.d.ts","sourceRoot":"","sources":["../../src/hooks/useSocket.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,WAAW,GAAG,cAAc,GAAG,OAAO,CAAC;AAEjF,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,WAAW,GAAG,MAAM,CAAC;CAC9B;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,IAAI,CAAC;IACvC,YAAY,CAAC,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,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true });
|
|
4
|
+
exports.useSocket = useSocket;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const SOCKET_URL = 'http://localhost:3005';
|
|
7
|
+
const RECONNECT_DELAY = 3000;
|
|
8
|
+
const MAX_RECONNECT = 5;
|
|
9
|
+
function useSocket(opts) {
|
|
10
|
+
const { widgetId, viewerUid, serverUrl = SOCKET_URL, onMessage, onMessageAck, onChatPaused, onCallOffer, onCallAnswer, onIceCandidate, onCallEnd, onError, onUserStatus, } = opts;
|
|
11
|
+
const [status, setStatus] = (0, react_1.useState)('disconnected');
|
|
12
|
+
const wsRef = (0, react_1.useRef)(null);
|
|
13
|
+
const reconnectCount = (0, react_1.useRef)(0);
|
|
14
|
+
const reconnectTimer = (0, react_1.useRef)(null);
|
|
15
|
+
const currentRoomRef = (0, react_1.useRef)(null);
|
|
16
|
+
const mountedRef = (0, react_1.useRef)(true);
|
|
17
|
+
const send = (0, react_1.useCallback)((event, payload) => {
|
|
18
|
+
var _a;
|
|
19
|
+
if (((_a = wsRef.current) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
20
|
+
wsRef.current.send(JSON.stringify({ event, payload }));
|
|
21
|
+
}
|
|
22
|
+
}, []);
|
|
23
|
+
const connect = (0, react_1.useCallback)(() => {
|
|
24
|
+
if (!mountedRef.current)
|
|
25
|
+
return;
|
|
26
|
+
if (wsRef.current) {
|
|
27
|
+
wsRef.current.onclose = null;
|
|
28
|
+
wsRef.current.close();
|
|
29
|
+
}
|
|
30
|
+
setStatus('connecting');
|
|
31
|
+
const url = `${serverUrl}?widgetId=${encodeURIComponent(widgetId)}&uid=${encodeURIComponent(viewerUid)}`;
|
|
32
|
+
let ws;
|
|
33
|
+
try {
|
|
34
|
+
ws = new WebSocket(url);
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
setStatus('error');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
wsRef.current = ws;
|
|
41
|
+
ws.onopen = () => {
|
|
42
|
+
if (!mountedRef.current) {
|
|
43
|
+
ws.close();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
reconnectCount.current = 0;
|
|
47
|
+
setStatus('connected');
|
|
48
|
+
// Re-join current room if we were in one
|
|
49
|
+
if (currentRoomRef.current) {
|
|
50
|
+
send('join', { roomId: currentRoomRef.current, widgetId });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
ws.onmessage = (event) => {
|
|
54
|
+
let parsed;
|
|
55
|
+
try {
|
|
56
|
+
parsed = JSON.parse(event.data);
|
|
57
|
+
}
|
|
58
|
+
catch (_a) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const { event: ev, payload } = parsed;
|
|
62
|
+
switch (ev) {
|
|
63
|
+
case 'message':
|
|
64
|
+
onMessage === null || onMessage === void 0 ? void 0 : onMessage(payload);
|
|
65
|
+
break;
|
|
66
|
+
case 'message:ack':
|
|
67
|
+
onMessageAck === null || onMessageAck === void 0 ? void 0 : onMessageAck(payload);
|
|
68
|
+
break;
|
|
69
|
+
case 'chat:paused': {
|
|
70
|
+
const p = payload;
|
|
71
|
+
onChatPaused === null || onChatPaused === void 0 ? void 0 : onChatPaused(p.roomId, p.paused);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case 'call-offer': {
|
|
75
|
+
const p = payload;
|
|
76
|
+
onCallOffer === null || onCallOffer === void 0 ? void 0 : onCallOffer(p.offer, p.from, p.callId);
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case 'call-answer': {
|
|
80
|
+
const p = payload;
|
|
81
|
+
onCallAnswer === null || onCallAnswer === void 0 ? void 0 : onCallAnswer(p.answer, p.from, p.callId);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case 'ice-candidate': {
|
|
85
|
+
const p = payload;
|
|
86
|
+
onIceCandidate === null || onIceCandidate === void 0 ? void 0 : onIceCandidate(p.candidate, p.from);
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case 'call-end': {
|
|
90
|
+
const p = payload;
|
|
91
|
+
onCallEnd === null || onCallEnd === void 0 ? void 0 : onCallEnd(p.callId);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case 'user:status': {
|
|
95
|
+
const p = payload;
|
|
96
|
+
onUserStatus === null || onUserStatus === void 0 ? void 0 : onUserStatus(p.uid, p.status);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case 'error': {
|
|
100
|
+
const p = payload;
|
|
101
|
+
onError === null || onError === void 0 ? void 0 : onError(p.code, p.message);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
ws.onerror = () => {
|
|
107
|
+
if (!mountedRef.current)
|
|
108
|
+
return;
|
|
109
|
+
setStatus('error');
|
|
110
|
+
};
|
|
111
|
+
ws.onclose = () => {
|
|
112
|
+
if (!mountedRef.current)
|
|
113
|
+
return;
|
|
114
|
+
setStatus('disconnected');
|
|
115
|
+
if (reconnectCount.current < MAX_RECONNECT) {
|
|
116
|
+
reconnectCount.current++;
|
|
117
|
+
reconnectTimer.current = setTimeout(connect, RECONNECT_DELAY * reconnectCount.current);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}, [widgetId, viewerUid, serverUrl, send, onMessage, onMessageAck, onChatPaused, onCallOffer, onCallAnswer, onIceCandidate, onCallEnd, onError, onUserStatus]);
|
|
121
|
+
(0, react_1.useEffect)(() => {
|
|
122
|
+
mountedRef.current = true;
|
|
123
|
+
if (widgetId && viewerUid)
|
|
124
|
+
connect();
|
|
125
|
+
return () => {
|
|
126
|
+
var _a;
|
|
127
|
+
mountedRef.current = false;
|
|
128
|
+
if (reconnectTimer.current)
|
|
129
|
+
clearTimeout(reconnectTimer.current);
|
|
130
|
+
(_a = wsRef.current) === null || _a === void 0 ? void 0 : _a.close();
|
|
131
|
+
};
|
|
132
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
133
|
+
}, [widgetId, viewerUid, serverUrl]);
|
|
134
|
+
const joinRoom = (0, react_1.useCallback)((roomId) => {
|
|
135
|
+
currentRoomRef.current = roomId;
|
|
136
|
+
send('join', { roomId, widgetId });
|
|
137
|
+
}, [send, widgetId]);
|
|
138
|
+
const leaveRoom = (0, react_1.useCallback)((roomId) => {
|
|
139
|
+
currentRoomRef.current = null;
|
|
140
|
+
send('leave', { roomId });
|
|
141
|
+
}, [send]);
|
|
142
|
+
const emitMessage = (0, react_1.useCallback)((msg) => {
|
|
143
|
+
send('message', msg);
|
|
144
|
+
}, [send]);
|
|
145
|
+
const emitTypingStart = (0, react_1.useCallback)((roomId) => {
|
|
146
|
+
send('typing_start', { roomId, userId: viewerUid });
|
|
147
|
+
}, [send, viewerUid]);
|
|
148
|
+
const emitTypingStop = (0, react_1.useCallback)((roomId) => {
|
|
149
|
+
send('typing_stop', { roomId, userId: viewerUid });
|
|
150
|
+
}, [send, viewerUid]);
|
|
151
|
+
const emitPauseToggle = (0, react_1.useCallback)((roomId, targetUserId, paused) => {
|
|
152
|
+
send('chat:pause', { roomId, targetUserId, paused });
|
|
153
|
+
}, [send]);
|
|
154
|
+
const emitReport = (0, react_1.useCallback)((roomId, reason) => {
|
|
155
|
+
send('chat:report', { roomId, reporterId: viewerUid, reason });
|
|
156
|
+
}, [send, viewerUid]);
|
|
157
|
+
const emitBlock = (0, react_1.useCallback)((blockedUid) => {
|
|
158
|
+
send('user:block', { blockedUid });
|
|
159
|
+
}, [send]);
|
|
160
|
+
const emitUnblock = (0, react_1.useCallback)((blockedUid) => {
|
|
161
|
+
send('user:unblock', { blockedUid });
|
|
162
|
+
}, [send]);
|
|
163
|
+
const emitTransfer = (0, react_1.useCallback)((fromRoomId, toUserId, note) => {
|
|
164
|
+
send('transfer', { fromRoomId, toUserId, note });
|
|
165
|
+
}, [send]);
|
|
166
|
+
const emitCallOffer = (0, react_1.useCallback)((offer, toUid, callId) => {
|
|
167
|
+
send('call-offer', { offer, to: toUid, from: viewerUid, callId });
|
|
168
|
+
}, [send, viewerUid]);
|
|
169
|
+
const emitCallAnswer = (0, react_1.useCallback)((answer, toUid, callId) => {
|
|
170
|
+
send('call-answer', { answer, to: toUid, callId });
|
|
171
|
+
}, [send]);
|
|
172
|
+
const emitIceCandidate = (0, react_1.useCallback)((candidate, toUid) => {
|
|
173
|
+
send('ice-candidate', { candidate, to: toUid });
|
|
174
|
+
}, [send]);
|
|
175
|
+
const emitCallEnd = (0, react_1.useCallback)((callId, toUid) => {
|
|
176
|
+
send('call-end', { callId, to: toUid });
|
|
177
|
+
}, [send]);
|
|
178
|
+
const emitAddParticipant = (0, react_1.useCallback)((roomId, uid) => {
|
|
179
|
+
send('room:add-participant', { roomId, uid });
|
|
180
|
+
}, [send]);
|
|
181
|
+
return {
|
|
182
|
+
status,
|
|
183
|
+
joinRoom, leaveRoom,
|
|
184
|
+
emitMessage, emitTypingStart, emitTypingStop,
|
|
185
|
+
emitPauseToggle, emitReport, emitBlock, emitUnblock, emitTransfer,
|
|
186
|
+
emitCallOffer, emitCallAnswer, emitIceCandidate, emitCallEnd,
|
|
187
|
+
emitAddParticipant,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=useSocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSocket.js","sourceRoot":"","sources":["../../src/hooks/useSocket.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;AAgCb,8BAmMC;AAjOD,iCAAiE;AA0BjE,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAC3C,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB,SAAgB,SAAS,CAAC,IAAsB;IAC9C,MAAM,EACJ,QAAQ,EAAE,SAAS,EAAE,SAAS,GAAG,UAAU,EAC3C,SAAS,EAAE,YAAY,EAAE,YAAY,EACrC,WAAW,EAAE,YAAY,EAAE,cAAc,EAAE,SAAS,EACp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,EAAE,MAAc,EAAE,EAAE;QACtG,IAAI,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;IACrD,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,MAAM,gBAAgB,GAAG,IAAA,mBAAW,EAAC,CAAC,SAA8B,EAAE,KAAa,EAAE,EAAE;QACrF,IAAI,CAAC,eAAe,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IAClD,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,MAAM,WAAW,GAAG,IAAA,mBAAW,EAAC,CAAC,MAAc,EAAE,KAAc,EAAE,EAAE;QACjE,IAAI,CAAC,UAAU,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IAC1C,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,MAAM,kBAAkB,GAAG,IAAA,mBAAW,EAAC,CAAC,MAAc,EAAE,GAAW,EAAE,EAAE;QACrE,IAAI,CAAC,sBAAsB,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;IAChD,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,OAAO;QACL,MAAM;QACN,QAAQ,EAAE,SAAS;QACnB,WAAW,EAAE,eAAe,EAAE,cAAc;QAC5C,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY;QACjE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW;QAC5D,kBAAkB;KACnB,CAAC;AACJ,CAAC"}
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { CallSession, ChatUser } from '../types';
|
|
2
|
-
export
|
|
2
|
+
export interface UseWebRTCOptions {
|
|
3
|
+
onOfferReady?: (offer: RTCSessionDescriptionInit, toUid: string, callId: string) => void;
|
|
4
|
+
onAnswerReady?: (answer: RTCSessionDescriptionInit, toUid: string, callId: string) => void;
|
|
5
|
+
onIceCandidateReady?: (candidate: RTCIceCandidateInit, toUid: string) => void;
|
|
6
|
+
onCallEnded?: (callId: string, toUid?: string) => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function useWebRTC(opts?: UseWebRTCOptions): {
|
|
3
9
|
session: CallSession;
|
|
4
10
|
localVideoRef: import("react").MutableRefObject<HTMLVideoElement | null>;
|
|
5
11
|
remoteVideoRef: import("react").MutableRefObject<HTMLVideoElement | null>;
|
|
6
12
|
startCall: (peer: ChatUser, withVideo?: boolean) => Promise<void>;
|
|
7
|
-
acceptCall: (offer: RTCSessionDescriptionInit, peer: ChatUser, withVideo?: boolean) => Promise<void>;
|
|
13
|
+
acceptCall: (offer: RTCSessionDescriptionInit, peer: ChatUser, callId: string, withVideo?: boolean) => Promise<void>;
|
|
14
|
+
addIceCandidate: (candidate: RTCIceCandidateInit) => Promise<void>;
|
|
8
15
|
endCall: () => void;
|
|
9
16
|
toggleMute: () => void;
|
|
10
17
|
toggleCamera: () => void;
|
|
11
18
|
};
|
|
19
|
+
//# sourceMappingURL=useWebRTC.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWebRTC.d.ts","sourceRoot":"","sources":["../../src/hooks/useWebRTC.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAa,QAAQ,EAAE,MAAM,UAAU,CAAC;AAE5D,MAAM,WAAW,gBAAgB;IAC/B,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,yBAAyB,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IACzF,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,yBAAyB,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3F,mBAAmB,CAAC,EAAE,CAAC,SAAS,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9E,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;CACxD;AAOD,wBAAgB,SAAS,CAAC,IAAI,GAAE,gBAAqB;;;;sBA+CR,QAAQ;wBA8B1C,yBAAyB,QAC1B,QAAQ,UACN,MAAM;iCAyBsC,mBAAmB;;;;EA6C1E"}
|
package/dist/hooks/useWebRTC.js
CHANGED
|
@@ -1,112 +1,144 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useWebRTC = useWebRTC;
|
|
4
|
+
const react_1 = require("react");
|
|
2
5
|
const ICE_SERVERS = [
|
|
3
6
|
{ urls: 'stun:stun.l.google.com:19302' },
|
|
4
7
|
{ urls: 'stun:stun1.l.google.com:19302' },
|
|
5
8
|
];
|
|
6
|
-
|
|
7
|
-
const
|
|
9
|
+
function useWebRTC(opts = {}) {
|
|
10
|
+
const { onOfferReady, onAnswerReady, onIceCandidateReady, onCallEnded } = opts;
|
|
11
|
+
const [session, setSession] = (0, react_1.useState)({
|
|
8
12
|
state: 'idle',
|
|
9
13
|
peer: null,
|
|
10
14
|
startedAt: null,
|
|
11
15
|
isMuted: false,
|
|
12
16
|
isCameraOn: false,
|
|
13
17
|
});
|
|
14
|
-
const pcRef = useRef(null);
|
|
15
|
-
const localStream = useRef(null);
|
|
16
|
-
const localVideoRef = useRef(null);
|
|
17
|
-
const remoteVideoRef = useRef(null);
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const stream = await navigator.mediaDevices.getUserMedia({
|
|
23
|
-
audio: true,
|
|
24
|
-
video: withVideo,
|
|
25
|
-
});
|
|
26
|
-
localStream.current = stream;
|
|
27
|
-
if (localVideoRef.current)
|
|
28
|
-
localVideoRef.current.srcObject = stream;
|
|
18
|
+
const pcRef = (0, react_1.useRef)(null);
|
|
19
|
+
const localStream = (0, react_1.useRef)(null);
|
|
20
|
+
const localVideoRef = (0, react_1.useRef)(null);
|
|
21
|
+
const remoteVideoRef = (0, react_1.useRef)(null);
|
|
22
|
+
const currentCallId = (0, react_1.useRef)(null);
|
|
23
|
+
const peerUidRef = (0, react_1.useRef)(null);
|
|
24
|
+
const updateSession = (0, react_1.useCallback)((patch) => setSession(prev => (Object.assign(Object.assign({}, prev), patch))), []);
|
|
25
|
+
const buildPc = (0, react_1.useCallback)((peer) => {
|
|
29
26
|
const pc = new RTCPeerConnection({ iceServers: ICE_SERVERS });
|
|
30
27
|
pcRef.current = pc;
|
|
31
|
-
|
|
28
|
+
peerUidRef.current = peer.uid;
|
|
32
29
|
pc.ontrack = e => {
|
|
33
30
|
if (remoteVideoRef.current)
|
|
34
31
|
remoteVideoRef.current.srcObject = e.streams[0];
|
|
35
|
-
updateSession({ state: 'connected', startedAt: new Date()
|
|
32
|
+
updateSession({ state: 'connected', startedAt: new Date() });
|
|
36
33
|
};
|
|
37
34
|
pc.onicecandidate = e => {
|
|
38
|
-
if (e.candidate) {
|
|
39
|
-
|
|
40
|
-
console.log('[WebRTC] ICE candidate ready — send via signalling:', e.candidate);
|
|
35
|
+
if (e.candidate && peerUidRef.current) {
|
|
36
|
+
onIceCandidateReady === null || onIceCandidateReady === void 0 ? void 0 : onIceCandidateReady(e.candidate.toJSON(), peerUidRef.current);
|
|
41
37
|
}
|
|
42
38
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
pc.onconnectionstatechange = () => {
|
|
40
|
+
if (pc.connectionState === 'failed' || pc.connectionState === 'disconnected') {
|
|
41
|
+
updateSession({ state: 'ended' });
|
|
42
|
+
setTimeout(() => updateSession({ state: 'idle' }), 1800);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return pc;
|
|
46
|
+
}, [onIceCandidateReady, updateSession]);
|
|
47
|
+
const startCall = (0, react_1.useCallback)(async (peer, withVideo = false) => {
|
|
48
|
+
const callId = `call_${Date.now()}`;
|
|
49
|
+
currentCallId.current = callId;
|
|
50
|
+
updateSession({ state: 'calling', peer });
|
|
51
|
+
let stream;
|
|
52
|
+
try {
|
|
53
|
+
stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: withVideo });
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.error('[WebRTC] getUserMedia failed:', err);
|
|
57
|
+
updateSession({ state: 'idle' });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
localStream.current = stream;
|
|
61
|
+
if (localVideoRef.current)
|
|
62
|
+
localVideoRef.current.srcObject = stream;
|
|
63
|
+
const pc = buildPc(peer);
|
|
64
|
+
stream.getTracks().forEach(t => pc.addTrack(t, stream));
|
|
65
|
+
try {
|
|
66
|
+
const offer = await pc.createOffer();
|
|
67
|
+
await pc.setLocalDescription(offer);
|
|
68
|
+
onOfferReady === null || onOfferReady === void 0 ? void 0 : onOfferReady(offer, peer.uid, callId);
|
|
69
|
+
updateSession({ isCameraOn: withVideo });
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
console.error('[WebRTC] createOffer failed:', err);
|
|
73
|
+
}
|
|
74
|
+
}, [buildPc, onOfferReady, updateSession]);
|
|
75
|
+
const acceptCall = (0, react_1.useCallback)(async (offer, peer, callId, withVideo = false) => {
|
|
76
|
+
currentCallId.current = callId;
|
|
50
77
|
updateSession({ state: 'connected', peer, startedAt: new Date(), isCameraOn: withVideo });
|
|
51
|
-
|
|
78
|
+
let stream;
|
|
79
|
+
try {
|
|
80
|
+
stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: withVideo });
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
console.error('[WebRTC] getUserMedia failed:', err);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
52
86
|
localStream.current = stream;
|
|
53
87
|
if (localVideoRef.current)
|
|
54
88
|
localVideoRef.current.srcObject = stream;
|
|
55
|
-
const pc =
|
|
56
|
-
pcRef.current = pc;
|
|
89
|
+
const pc = buildPc(peer);
|
|
57
90
|
stream.getTracks().forEach(t => pc.addTrack(t, stream));
|
|
58
|
-
pc.ontrack = e => {
|
|
59
|
-
if (remoteVideoRef.current)
|
|
60
|
-
remoteVideoRef.current.srcObject = e.streams[0];
|
|
61
|
-
};
|
|
62
|
-
pc.onicecandidate = e => {
|
|
63
|
-
if (e.candidate) {
|
|
64
|
-
// TODO: socket.emit('ice-candidate', { candidate: e.candidate, to: peer.uid });
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
91
|
await pc.setRemoteDescription(offer);
|
|
68
92
|
const answer = await pc.createAnswer();
|
|
69
93
|
await pc.setLocalDescription(answer);
|
|
70
|
-
|
|
94
|
+
onAnswerReady === null || onAnswerReady === void 0 ? void 0 : onAnswerReady(answer, peer.uid, callId);
|
|
95
|
+
}, [buildPc, onAnswerReady, updateSession]);
|
|
96
|
+
const addIceCandidate = (0, react_1.useCallback)(async (candidate) => {
|
|
97
|
+
var _a;
|
|
98
|
+
try {
|
|
99
|
+
await ((_a = pcRef.current) === null || _a === void 0 ? void 0 : _a.addIceCandidate(new RTCIceCandidate(candidate)));
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
console.warn('[WebRTC] addIceCandidate failed:', err);
|
|
103
|
+
}
|
|
71
104
|
}, []);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
(_a =
|
|
76
|
-
(_b =
|
|
105
|
+
const endCall = (0, react_1.useCallback)(() => {
|
|
106
|
+
var _a, _b, _c;
|
|
107
|
+
const callId = currentCallId.current;
|
|
108
|
+
const peerUid = (_a = peerUidRef.current) !== null && _a !== void 0 ? _a : undefined;
|
|
109
|
+
(_b = localStream.current) === null || _b === void 0 ? void 0 : _b.getTracks().forEach(t => t.stop());
|
|
110
|
+
(_c = pcRef.current) === null || _c === void 0 ? void 0 : _c.close();
|
|
77
111
|
pcRef.current = null;
|
|
78
112
|
localStream.current = null;
|
|
113
|
+
currentCallId.current = null;
|
|
114
|
+
if (callId)
|
|
115
|
+
onCallEnded === null || onCallEnded === void 0 ? void 0 : onCallEnded(callId, peerUid);
|
|
79
116
|
updateSession({ state: 'ended', peer: null, startedAt: null });
|
|
80
117
|
setTimeout(() => updateSession({ state: 'idle' }), 1800);
|
|
81
|
-
}, []);
|
|
82
|
-
const toggleMute = useCallback(() => {
|
|
118
|
+
}, [onCallEnded, updateSession]);
|
|
119
|
+
const toggleMute = (0, react_1.useCallback)(() => {
|
|
83
120
|
if (!localStream.current)
|
|
84
121
|
return;
|
|
85
|
-
const
|
|
86
|
-
localStream.current.getAudioTracks().forEach(t => { t.enabled =
|
|
87
|
-
updateSession({ isMuted:
|
|
88
|
-
}, [session.isMuted]);
|
|
89
|
-
const toggleCamera = useCallback(() => {
|
|
122
|
+
const muted = !session.isMuted;
|
|
123
|
+
localStream.current.getAudioTracks().forEach(t => { t.enabled = !muted; });
|
|
124
|
+
updateSession({ isMuted: muted });
|
|
125
|
+
}, [session.isMuted, updateSession]);
|
|
126
|
+
const toggleCamera = (0, react_1.useCallback)(() => {
|
|
90
127
|
if (!localStream.current)
|
|
91
128
|
return;
|
|
92
|
-
const
|
|
93
|
-
localStream.current.getVideoTracks().forEach(t => { t.enabled =
|
|
94
|
-
updateSession({ isCameraOn:
|
|
95
|
-
}, [session.isCameraOn]);
|
|
96
|
-
|
|
97
|
-
useEffect(() => () => {
|
|
129
|
+
const on = !session.isCameraOn;
|
|
130
|
+
localStream.current.getVideoTracks().forEach(t => { t.enabled = on; });
|
|
131
|
+
updateSession({ isCameraOn: on });
|
|
132
|
+
}, [session.isCameraOn, updateSession]);
|
|
133
|
+
(0, react_1.useEffect)(() => () => {
|
|
98
134
|
var _a, _b;
|
|
99
135
|
(_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(t => t.stop());
|
|
100
136
|
(_b = pcRef.current) === null || _b === void 0 ? void 0 : _b.close();
|
|
101
137
|
}, []);
|
|
102
138
|
return {
|
|
103
|
-
session,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
startCall,
|
|
107
|
-
acceptCall,
|
|
108
|
-
endCall,
|
|
109
|
-
toggleMute,
|
|
110
|
-
toggleCamera,
|
|
139
|
+
session, localVideoRef, remoteVideoRef,
|
|
140
|
+
startCall, acceptCall, addIceCandidate, endCall,
|
|
141
|
+
toggleMute, toggleCamera,
|
|
111
142
|
};
|
|
112
143
|
}
|
|
144
|
+
//# sourceMappingURL=useWebRTC.js.map
|