react-peer-chat 0.2.4 → 0.3.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/build/index.d.ts +6 -7
- package/build/index.js +28 -20
- package/build/storage.js +2 -2
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import React, { CSSProperties, DetailedHTMLProps, HTMLAttributes, ReactNode
|
|
1
|
+
import React, { CSSProperties, DetailedHTMLProps, HTMLAttributes, ReactNode } from 'react';
|
|
2
2
|
import { PeerOptions } from 'peerjs';
|
|
3
3
|
type Message = {
|
|
4
4
|
id: string;
|
|
5
5
|
text: string;
|
|
6
6
|
};
|
|
7
7
|
type ChildrenOptions = {
|
|
8
|
-
|
|
8
|
+
opponentName?: string;
|
|
9
9
|
messages?: Message[];
|
|
10
|
-
addMessage?: (message: Message) => void;
|
|
11
|
-
dialogRef?: RefObject<HTMLDialogElement>;
|
|
10
|
+
addMessage?: (message: Message, sendToRemotePeer?: boolean) => void;
|
|
12
11
|
audio?: boolean;
|
|
13
12
|
setAudio?: (audio: boolean) => void;
|
|
14
13
|
};
|
|
15
|
-
type DialogPosition = 'left' | 'center' | '
|
|
14
|
+
type DialogPosition = 'left' | 'center' | 'right';
|
|
16
15
|
type DialogOptions = {
|
|
17
16
|
position: DialogPosition;
|
|
18
17
|
style: CSSProperties;
|
|
@@ -20,13 +19,13 @@ type DialogOptions = {
|
|
|
20
19
|
type Props = {
|
|
21
20
|
name?: string;
|
|
22
21
|
peerId: string;
|
|
23
|
-
remotePeerId
|
|
22
|
+
remotePeerId?: string;
|
|
24
23
|
text?: boolean;
|
|
25
24
|
voice?: boolean;
|
|
26
25
|
peerOptions?: PeerOptions;
|
|
27
26
|
dialogOptions?: DialogOptions;
|
|
28
27
|
onError?: () => void;
|
|
29
|
-
children?:
|
|
28
|
+
children?: (childrenOptions: ChildrenOptions) => ReactNode;
|
|
30
29
|
} & DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
31
30
|
export default function Chat({ name, peerId, remotePeerId, peerOptions, text, voice, dialogOptions, onError, children, ...props }: Props): React.JSX.Element;
|
|
32
31
|
export declare const clearChat: () => void;
|
package/build/index.js
CHANGED
|
@@ -14,13 +14,13 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
14
14
|
const [audio, setAudio] = useStorage('rpc-audio', false, { local: true, save: true });
|
|
15
15
|
const streamRef = useRef(null);
|
|
16
16
|
const localStream = useRef();
|
|
17
|
-
remotePeerId = `rpc-${remotePeerId}`;
|
|
18
17
|
const handleRemoteStream = (remoteStream) => streamRef.current.srcObject = remoteStream;
|
|
19
|
-
function addMessage(message,
|
|
18
|
+
function addMessage(message, sendToRemotePeer = false) {
|
|
19
|
+
var _a, _b;
|
|
20
20
|
setMessages(old => old.concat(message));
|
|
21
|
-
if (
|
|
22
|
-
connRef.current
|
|
23
|
-
else if (!dialogRef.current
|
|
21
|
+
if (sendToRemotePeer)
|
|
22
|
+
(_a = connRef.current) === null || _a === void 0 ? void 0 : _a.send({ type: 'message', message });
|
|
23
|
+
else if (!((_b = dialogRef.current) === null || _b === void 0 ? void 0 : _b.open))
|
|
24
24
|
setNotification(true);
|
|
25
25
|
}
|
|
26
26
|
function handleConnection(conn, setName = false) {
|
|
@@ -58,8 +58,11 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
58
58
|
return;
|
|
59
59
|
let call;
|
|
60
60
|
peer.on('open', () => {
|
|
61
|
-
if (
|
|
62
|
-
|
|
61
|
+
if (remotePeerId) {
|
|
62
|
+
remotePeerId = `rpc-${remotePeerId}`;
|
|
63
|
+
if (text)
|
|
64
|
+
handleConnection(peer.connect(remotePeerId, { metadata: name }), true);
|
|
65
|
+
}
|
|
63
66
|
if (audio) {
|
|
64
67
|
const getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
65
68
|
getUserMedia({
|
|
@@ -71,9 +74,11 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
71
74
|
}
|
|
72
75
|
}, (stream) => {
|
|
73
76
|
localStream.current = stream;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
if (remotePeerId) {
|
|
78
|
+
call = peer.call(remotePeerId, stream);
|
|
79
|
+
call.on('stream', handleRemoteStream);
|
|
80
|
+
call.on('close', call.removeAllListeners);
|
|
81
|
+
}
|
|
77
82
|
peer.on('call', e => {
|
|
78
83
|
call = e;
|
|
79
84
|
call.answer(stream);
|
|
@@ -85,20 +90,22 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
85
90
|
});
|
|
86
91
|
peer.on('connection', handleConnection);
|
|
87
92
|
return () => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
connRef.current
|
|
91
|
-
|
|
92
|
-
call
|
|
93
|
+
var _a, _b, _c;
|
|
94
|
+
(_a = localStream.current) === null || _a === void 0 ? void 0 : _a.getTracks().forEach(track => track.stop());
|
|
95
|
+
(_b = connRef.current) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
|
|
96
|
+
(_c = connRef.current) === null || _c === void 0 ? void 0 : _c.close();
|
|
97
|
+
call === null || call === void 0 ? void 0 : call.removeAllListeners();
|
|
98
|
+
call === null || call === void 0 ? void 0 : call.close();
|
|
93
99
|
peer.removeAllListeners();
|
|
94
100
|
peer.destroy();
|
|
95
101
|
};
|
|
96
102
|
}, [peer]);
|
|
97
103
|
useEffect(() => {
|
|
104
|
+
var _a, _b;
|
|
98
105
|
if (dialog)
|
|
99
|
-
dialogRef.current
|
|
106
|
+
(_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.show();
|
|
100
107
|
else
|
|
101
|
-
dialogRef.current
|
|
108
|
+
(_b = dialogRef.current) === null || _b === void 0 ? void 0 : _b.close();
|
|
102
109
|
}, [dialog]);
|
|
103
110
|
useEffect(() => {
|
|
104
111
|
const container = containerRef.current;
|
|
@@ -106,7 +113,7 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
106
113
|
container.scrollTop = container.scrollHeight;
|
|
107
114
|
}, [dialog, opponentName, messages]);
|
|
108
115
|
return React.createElement("div", { className: 'rpc-main', ...props },
|
|
109
|
-
typeof children === 'function' ? children({
|
|
116
|
+
typeof children === 'function' ? children({ opponentName, messages, addMessage, audio, setAudio }) : React.createElement(React.Fragment, null,
|
|
110
117
|
text && React.createElement("div", null,
|
|
111
118
|
dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) })
|
|
112
119
|
: React.createElement("div", { className: 'rpc-notification' },
|
|
@@ -115,7 +122,7 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
115
122
|
setDialog(true);
|
|
116
123
|
} }),
|
|
117
124
|
notification && React.createElement("span", { className: 'rpc-badge' })),
|
|
118
|
-
React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'dialog' : ''} position-${dialogOptions
|
|
125
|
+
React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'rpc-dialog' : ''} rpc-position-${(dialogOptions === null || dialogOptions === void 0 ? void 0 : dialogOptions.position) || 'center'}`, style: dialogOptions === null || dialogOptions === void 0 ? void 0 : dialogOptions.style },
|
|
119
126
|
React.createElement("div", { className: 'rpc-heading' }, "Chat"),
|
|
120
127
|
React.createElement("hr", null),
|
|
121
128
|
React.createElement("div", null,
|
|
@@ -126,8 +133,9 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
126
133
|
React.createElement("span", null, text)))),
|
|
127
134
|
React.createElement("hr", null),
|
|
128
135
|
React.createElement("form", { className: 'rpc-input-container', onSubmit: e => {
|
|
136
|
+
var _a;
|
|
129
137
|
e.preventDefault();
|
|
130
|
-
const text = inputRef.current
|
|
138
|
+
const text = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.value;
|
|
131
139
|
if (text) {
|
|
132
140
|
inputRef.current.value = '';
|
|
133
141
|
addMessage({ id: peerId, text }, true);
|
package/build/storage.js
CHANGED
|
@@ -10,7 +10,7 @@ const getStorage = (key, fallbackValue, local = false) => {
|
|
|
10
10
|
throw new Error("Value doesn't exist");
|
|
11
11
|
value = JSON.parse(value);
|
|
12
12
|
}
|
|
13
|
-
catch {
|
|
13
|
+
catch (_a) {
|
|
14
14
|
if (fallbackValue !== undefined) {
|
|
15
15
|
value = fallbackValue;
|
|
16
16
|
setStorage(key, value, local);
|
|
@@ -23,7 +23,7 @@ const getStorage = (key, fallbackValue, local = false) => {
|
|
|
23
23
|
return value;
|
|
24
24
|
};
|
|
25
25
|
export default function useStorage(key, initialValue, { local = false, save = false } = {}) {
|
|
26
|
-
save
|
|
26
|
+
save || (save = getStorage('mode') !== 'online');
|
|
27
27
|
const [storedValue, setStoredValue] = useState(save ? getStorage(key, initialValue, local) : initialValue);
|
|
28
28
|
const setValue = (value) => {
|
|
29
29
|
setStoredValue((old) => {
|