react-peer-chat 0.2.5 → 0.3.1
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 +8 -8
- package/build/index.js +35 -28
- package/build/storage.js +2 -2
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
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
|
};
|
|
@@ -20,14 +19,15 @@ 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?:
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
children?: (childrenOptions: ChildrenOptions) => ReactNode;
|
|
29
|
+
props: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
30
|
+
};
|
|
31
|
+
export default function Chat({ name, peerId, remotePeerId, peerOptions, text, voice, dialogOptions, onError, children, props }: Props): React.JSX.Element;
|
|
32
32
|
export declare const clearChat: () => void;
|
|
33
33
|
export {};
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React, { useEffect, useRef, useState } from 'react';
|
|
2
2
|
import useStorage, { removeStorage } from './storage.js';
|
|
3
3
|
import { BiSolidMessageDetail, BiSolidMessageX, BsFillMicFill, BsFillMicMuteFill, GrSend } from './icons.js';
|
|
4
|
-
export default function Chat({ name, peerId, remotePeerId, peerOptions, text = true, voice = true, dialogOptions, onError = () => console.error("Can not access microphone!"), children,
|
|
4
|
+
export default function Chat({ name, peerId, remotePeerId, peerOptions, text = true, voice = true, dialogOptions, onError = () => console.error("Can not access microphone!"), children, props }) {
|
|
5
5
|
const [peer, setPeer] = useState();
|
|
6
6
|
const [opponentName, setOpponentName] = useState();
|
|
7
7
|
const [notification, setNotification] = useState(false);
|
|
@@ -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,16 +113,15 @@ 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
|
-
dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) })
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'rpc-dialog' : ''} rpc-position-${dialogOptions?.position || 'center'}`, style: dialogOptions?.style },
|
|
118
|
+
dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) }) : React.createElement("div", { className: 'rpc-notification' },
|
|
119
|
+
React.createElement(BiSolidMessageDetail, { onClick: () => {
|
|
120
|
+
setNotification(false);
|
|
121
|
+
setDialog(true);
|
|
122
|
+
} }),
|
|
123
|
+
notification && React.createElement("span", { className: 'rpc-badge' })),
|
|
124
|
+
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
125
|
React.createElement("div", { className: 'rpc-heading' }, "Chat"),
|
|
120
126
|
React.createElement("hr", null),
|
|
121
127
|
React.createElement("div", null,
|
|
@@ -126,8 +132,9 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
126
132
|
React.createElement("span", null, text)))),
|
|
127
133
|
React.createElement("hr", null),
|
|
128
134
|
React.createElement("form", { className: 'rpc-input-container', onSubmit: e => {
|
|
135
|
+
var _a;
|
|
129
136
|
e.preventDefault();
|
|
130
|
-
const text = inputRef.current
|
|
137
|
+
const text = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.value;
|
|
131
138
|
if (text) {
|
|
132
139
|
inputRef.current.value = '';
|
|
133
140
|
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) => {
|