react-peer-chat 0.3.1 → 0.3.3
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 +2 -2
- package/build/index.js +16 -14
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ type Message = {
|
|
|
5
5
|
text: string;
|
|
6
6
|
};
|
|
7
7
|
type ChildrenOptions = {
|
|
8
|
-
|
|
8
|
+
remotePeer?: string;
|
|
9
9
|
messages?: Message[];
|
|
10
10
|
addMessage?: (message: Message, sendToRemotePeer?: boolean) => void;
|
|
11
11
|
audio?: boolean;
|
|
@@ -26,7 +26,7 @@ type Props = {
|
|
|
26
26
|
dialogOptions?: DialogOptions;
|
|
27
27
|
onError?: () => void;
|
|
28
28
|
children?: (childrenOptions: ChildrenOptions) => ReactNode;
|
|
29
|
-
props
|
|
29
|
+
props?: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
30
30
|
};
|
|
31
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;
|
package/build/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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, props }) {
|
|
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
|
-
const [opponentName, setOpponentName] = useState();
|
|
7
6
|
const [notification, setNotification] = useState(false);
|
|
7
|
+
const [remotePeer, setRemotePeer] = useStorage('rpc-remote-peer', '', { save: true });
|
|
8
8
|
const [messages, setMessages] = useStorage('rpc-messages', [], { save: true });
|
|
9
9
|
const connRef = useRef();
|
|
10
10
|
const [dialog, setDialog] = useState(false);
|
|
@@ -23,16 +23,14 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
23
23
|
else if (!((_b = dialogRef.current) === null || _b === void 0 ? void 0 : _b.open))
|
|
24
24
|
setNotification(true);
|
|
25
25
|
}
|
|
26
|
-
function handleConnection(conn
|
|
27
|
-
if (setName)
|
|
28
|
-
setOpponentName(conn.metadata || 'Anonymous User');
|
|
26
|
+
function handleConnection(conn) {
|
|
29
27
|
connRef.current = conn;
|
|
30
28
|
conn.on('open', () => {
|
|
31
|
-
conn.
|
|
32
|
-
conn.on('data', ({ type, message, messages }) => {
|
|
29
|
+
conn.on('data', ({ type, message, remotePeer, messages }) => {
|
|
33
30
|
if (type === 'message')
|
|
34
31
|
addMessage(message);
|
|
35
|
-
else if (type === '
|
|
32
|
+
else if (type === 'init') {
|
|
33
|
+
setRemotePeer(remotePeer || 'Anonymous User');
|
|
36
34
|
setMessages(old => {
|
|
37
35
|
if (messages.length > old.length)
|
|
38
36
|
return messages;
|
|
@@ -40,6 +38,7 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
40
38
|
});
|
|
41
39
|
}
|
|
42
40
|
});
|
|
41
|
+
conn.send({ type: 'init', remotePeer: name, messages });
|
|
43
42
|
});
|
|
44
43
|
}
|
|
45
44
|
useEffect(() => {
|
|
@@ -61,7 +60,7 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
61
60
|
if (remotePeerId) {
|
|
62
61
|
remotePeerId = `rpc-${remotePeerId}`;
|
|
63
62
|
if (text)
|
|
64
|
-
handleConnection(peer.connect(remotePeerId, { metadata: name })
|
|
63
|
+
handleConnection(peer.connect(remotePeerId, { metadata: name }));
|
|
65
64
|
}
|
|
66
65
|
if (audio) {
|
|
67
66
|
const getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
|
|
@@ -111,9 +110,9 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
111
110
|
const container = containerRef.current;
|
|
112
111
|
if (container)
|
|
113
112
|
container.scrollTop = container.scrollHeight;
|
|
114
|
-
}, [dialog,
|
|
113
|
+
}, [dialog, remotePeer, messages]);
|
|
115
114
|
return React.createElement("div", { className: 'rpc-main', ...props },
|
|
116
|
-
typeof children === 'function' ? children({
|
|
115
|
+
typeof children === 'function' ? children({ remotePeer, messages, addMessage, audio, setAudio }) : React.createElement(React.Fragment, null,
|
|
117
116
|
text && React.createElement("div", null,
|
|
118
117
|
dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) }) : React.createElement("div", { className: 'rpc-notification' },
|
|
119
118
|
React.createElement(BiSolidMessageDetail, { onClick: () => {
|
|
@@ -125,9 +124,9 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
125
124
|
React.createElement("div", { className: 'rpc-heading' }, "Chat"),
|
|
126
125
|
React.createElement("hr", null),
|
|
127
126
|
React.createElement("div", null,
|
|
128
|
-
React.createElement("div", { ref: containerRef, className: 'rpc-message-container' },
|
|
127
|
+
React.createElement("div", { ref: containerRef, className: 'rpc-message-container' }, messages.map(({ id, text }, i) => React.createElement("div", { key: i },
|
|
129
128
|
React.createElement("strong", null,
|
|
130
|
-
id === peerId ? 'You' :
|
|
129
|
+
id === peerId ? 'You' : remotePeer,
|
|
131
130
|
": "),
|
|
132
131
|
React.createElement("span", null, text)))),
|
|
133
132
|
React.createElement("hr", null),
|
|
@@ -146,4 +145,7 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
|
|
|
146
145
|
voice && React.createElement("div", null, audio ? React.createElement(BsFillMicFill, { title: "Turn mic off", onClick: () => setAudio(false) }) : React.createElement(BsFillMicMuteFill, { title: "Turn mic on", onClick: () => setAudio(true) }))),
|
|
147
146
|
voice && audio && React.createElement("audio", { ref: streamRef, autoPlay: true, style: { display: 'none' } }));
|
|
148
147
|
}
|
|
149
|
-
export const clearChat = () =>
|
|
148
|
+
export const clearChat = () => {
|
|
149
|
+
removeStorage('rpc-remote-peer');
|
|
150
|
+
removeStorage('rpc-messages');
|
|
151
|
+
};
|