react-peer-chat 0.2.2 → 0.2.4
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/icons.js +5 -5
- package/build/index.js +19 -46
- package/build/storage.js +2 -2
- package/package.json +1 -1
package/build/icons.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
export function BiSolidMessageDetail(props) {
|
|
3
|
-
return React.createElement("span",
|
|
3
|
+
return React.createElement("span", { ...props },
|
|
4
4
|
React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'rpc-scale-down' },
|
|
5
5
|
React.createElement("path", { d: "M20 2H4c-1.103 0-2 .894-2 1.992v12.016C2 17.106 2.897 18 4 18h3v4l6.351-4H20c1.103 0 2-.894 2-1.992V3.992A1.998 1.998 0 0 0 20 2zm-6 11H7v-2h7v2zm3-4H7V7h10v2z" })));
|
|
6
6
|
}
|
|
7
7
|
export function BiSolidMessageX(props) {
|
|
8
|
-
return React.createElement("span",
|
|
8
|
+
return React.createElement("span", { ...props },
|
|
9
9
|
React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'rpc-scale-down' },
|
|
10
10
|
React.createElement("path", { d: "M20 2H4c-1.103 0-2 .894-2 1.992v12.016C2 17.106 2.897 18 4 18h3v4l6.351-4H20c1.103 0 2-.894 2-1.992V3.992A1.998 1.998 0 0 0 20 2zm-3.293 11.293-1.414 1.414L12 11.414l-3.293 3.293-1.414-1.414L10.586 10 7.293 6.707l1.414-1.414L12 8.586l3.293-3.293 1.414 1.414L13.414 10l3.293 3.293z" })));
|
|
11
11
|
}
|
|
12
12
|
export function GrSend(props) {
|
|
13
|
-
return React.createElement("span",
|
|
13
|
+
return React.createElement("span", { ...props },
|
|
14
14
|
React.createElement("svg", { fill: "currentColor", width: '1.5rem', height: '1.5rem', className: 'rpc-scale-down rpc-invert' },
|
|
15
15
|
React.createElement("path", { fill: "none", stroke: "#000", strokeWidth: 2, d: "M22,3 L2,11 L20.5,19 L22,3 Z M10,20.5 L13,16 M15.5,9.5 L9,14 L9.85884537,20.0119176 C9.93680292,20.5576204 10.0751625,20.5490248 10.1651297,20.009222 L11,15 L15.5,9.5 Z" })));
|
|
16
16
|
}
|
|
17
17
|
export function BsFillMicFill(props) {
|
|
18
|
-
return React.createElement("span",
|
|
18
|
+
return React.createElement("span", { ...props },
|
|
19
19
|
React.createElement("svg", { fill: "currentColor", width: '1rem', height: '1rem' },
|
|
20
20
|
React.createElement("path", { d: "M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0V3z" }),
|
|
21
21
|
React.createElement("path", { d: "M3.5 6.5A.5.5 0 0 1 4 7v1a4 4 0 0 0 8 0V7a.5.5 0 0 1 1 0v1a5 5 0 0 1-4.5 4.975V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 .5-.5z" })));
|
|
22
22
|
}
|
|
23
23
|
export function BsFillMicMuteFill(props) {
|
|
24
|
-
return React.createElement("span",
|
|
24
|
+
return React.createElement("span", { ...props },
|
|
25
25
|
React.createElement("svg", { fill: "currentColor", width: '1rem', height: '1rem' },
|
|
26
26
|
React.createElement("path", { d: "M13 8c0 .564-.094 1.107-.266 1.613l-.814-.814A4.02 4.02 0 0 0 12 8V7a.5.5 0 0 1 1 0v1zm-5 4c.818 0 1.578-.245 2.212-.667l.718.719a4.973 4.973 0 0 1-2.43.923V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 1 0v1a4 4 0 0 0 4 4zm3-9v4.879L5.158 2.037A3.001 3.001 0 0 1 11 3z" }),
|
|
27
27
|
React.createElement("path", { d: "M9.486 10.607 5 6.12V8a3 3 0 0 0 4.486 2.607zm-7.84-9.253 12 12 .708-.708-12-12-.708.708z" })));
|
package/build/index.js
CHANGED
|
@@ -1,28 +1,7 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
1
|
import React, { useEffect, useRef, useState } from 'react';
|
|
22
|
-
import useStorage, { removeStorage } from './storage';
|
|
23
|
-
import { BiSolidMessageDetail, BiSolidMessageX, BsFillMicFill, BsFillMicMuteFill, GrSend } from './icons';
|
|
24
|
-
export default function Chat(
|
|
25
|
-
var { name, peerId, remotePeerId, peerOptions, text = true, voice = true, dialogOptions, onError = () => console.error("Can not access microphone!"), children } = _a, props = __rest(_a, ["name", "peerId", "remotePeerId", "peerOptions", "text", "voice", "dialogOptions", "onError", "children"]);
|
|
2
|
+
import useStorage, { removeStorage } from './storage.js';
|
|
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 }) {
|
|
26
5
|
const [peer, setPeer] = useState();
|
|
27
6
|
const [opponentName, setOpponentName] = useState();
|
|
28
7
|
const [notification, setNotification] = useState(false);
|
|
@@ -38,11 +17,10 @@ export default function Chat(_a) {
|
|
|
38
17
|
remotePeerId = `rpc-${remotePeerId}`;
|
|
39
18
|
const handleRemoteStream = (remoteStream) => streamRef.current.srcObject = remoteStream;
|
|
40
19
|
function addMessage(message, send = false) {
|
|
41
|
-
var _a, _b;
|
|
42
20
|
setMessages(old => old.concat(message));
|
|
43
21
|
if (send)
|
|
44
|
-
|
|
45
|
-
else if (!
|
|
22
|
+
connRef.current?.send({ type: 'message', message });
|
|
23
|
+
else if (!dialogRef.current?.open)
|
|
46
24
|
setNotification(true);
|
|
47
25
|
}
|
|
48
26
|
function handleConnection(conn, setName = false) {
|
|
@@ -69,12 +47,10 @@ export default function Chat(_a) {
|
|
|
69
47
|
setPeer(undefined);
|
|
70
48
|
return;
|
|
71
49
|
}
|
|
72
|
-
(function loadPeer() {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
setPeer(peer);
|
|
77
|
-
});
|
|
50
|
+
(async function loadPeer() {
|
|
51
|
+
const { Peer } = await import('peerjs');
|
|
52
|
+
const peer = new Peer(`rpc-${peerId}`, peerOptions);
|
|
53
|
+
setPeer(peer);
|
|
78
54
|
})();
|
|
79
55
|
}, [audio]);
|
|
80
56
|
useEffect(() => {
|
|
@@ -109,29 +85,27 @@ export default function Chat(_a) {
|
|
|
109
85
|
});
|
|
110
86
|
peer.on('connection', handleConnection);
|
|
111
87
|
return () => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
(
|
|
116
|
-
call
|
|
117
|
-
call === null || call === void 0 ? void 0 : call.close();
|
|
88
|
+
localStream.current?.getTracks().forEach(track => track.stop());
|
|
89
|
+
connRef.current?.removeAllListeners();
|
|
90
|
+
connRef.current?.close();
|
|
91
|
+
call?.removeAllListeners();
|
|
92
|
+
call?.close();
|
|
118
93
|
peer.removeAllListeners();
|
|
119
94
|
peer.destroy();
|
|
120
95
|
};
|
|
121
96
|
}, [peer]);
|
|
122
97
|
useEffect(() => {
|
|
123
|
-
var _a, _b;
|
|
124
98
|
if (dialog)
|
|
125
|
-
|
|
99
|
+
dialogRef.current?.show();
|
|
126
100
|
else
|
|
127
|
-
|
|
101
|
+
dialogRef.current?.close();
|
|
128
102
|
}, [dialog]);
|
|
129
103
|
useEffect(() => {
|
|
130
104
|
const container = containerRef.current;
|
|
131
105
|
if (container)
|
|
132
106
|
container.scrollTop = container.scrollHeight;
|
|
133
107
|
}, [dialog, opponentName, messages]);
|
|
134
|
-
return React.createElement("div",
|
|
108
|
+
return React.createElement("div", { className: 'rpc-main', ...props },
|
|
135
109
|
typeof children === 'function' ? children({ notification, messages, addMessage, dialogRef, audio, setAudio }) : React.createElement(React.Fragment, null,
|
|
136
110
|
text && React.createElement("div", null,
|
|
137
111
|
dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) })
|
|
@@ -141,7 +115,7 @@ export default function Chat(_a) {
|
|
|
141
115
|
setDialog(true);
|
|
142
116
|
} }),
|
|
143
117
|
notification && React.createElement("span", { className: 'rpc-badge' })),
|
|
144
|
-
React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'dialog' : ''} position-${
|
|
118
|
+
React.createElement("dialog", { ref: dialogRef, className: `${dialog ? 'dialog' : ''} position-${dialogOptions?.position || 'center'}`, style: dialogOptions?.style },
|
|
145
119
|
React.createElement("div", { className: 'rpc-heading' }, "Chat"),
|
|
146
120
|
React.createElement("hr", null),
|
|
147
121
|
React.createElement("div", null,
|
|
@@ -152,9 +126,8 @@ export default function Chat(_a) {
|
|
|
152
126
|
React.createElement("span", null, text)))),
|
|
153
127
|
React.createElement("hr", null),
|
|
154
128
|
React.createElement("form", { className: 'rpc-input-container', onSubmit: e => {
|
|
155
|
-
var _a;
|
|
156
129
|
e.preventDefault();
|
|
157
|
-
const text =
|
|
130
|
+
const text = inputRef.current?.value;
|
|
158
131
|
if (text) {
|
|
159
132
|
inputRef.current.value = '';
|
|
160
133
|
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 {
|
|
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 ||= getStorage('mode') !== 'online';
|
|
27
27
|
const [storedValue, setStoredValue] = useState(save ? getStorage(key, initialValue, local) : initialValue);
|
|
28
28
|
const setValue = (value) => {
|
|
29
29
|
setStoredValue((old) => {
|