react-peer-chat 0.2.5 → 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 CHANGED
@@ -1,14 +1,13 @@
1
- import React, { CSSProperties, DetailedHTMLProps, HTMLAttributes, ReactNode, RefObject } from 'react';
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
- notification?: boolean;
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,13 +19,13 @@ type DialogOptions = {
20
19
  type Props = {
21
20
  name?: string;
22
21
  peerId: string;
23
- remotePeerId: string;
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?: ReactNode | ((childrenOptions: ChildrenOptions) => ReactNode);
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, send = false) {
18
+ function addMessage(message, sendToRemotePeer = false) {
19
+ var _a, _b;
20
20
  setMessages(old => old.concat(message));
21
- if (send)
22
- connRef.current?.send({ type: 'message', message });
23
- else if (!dialogRef.current?.open)
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 (text)
62
- handleConnection(peer.connect(remotePeerId, { metadata: name }), true);
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
- call = peer.call(remotePeerId, stream);
75
- call.on('stream', handleRemoteStream);
76
- call.on('close', call.removeAllListeners);
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
- localStream.current?.getTracks().forEach(track => track.stop());
89
- connRef.current?.removeAllListeners();
90
- connRef.current?.close();
91
- call?.removeAllListeners();
92
- call?.close();
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?.show();
106
+ (_a = dialogRef.current) === null || _a === void 0 ? void 0 : _a.show();
100
107
  else
101
- dialogRef.current?.close();
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({ notification, messages, addMessage, dialogRef, audio, setAudio }) : React.createElement(React.Fragment, null,
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 ? 'rpc-dialog' : ''} rpc-position-${dialogOptions?.position || 'center'}`, style: dialogOptions?.style },
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?.value;
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 ||= getStorage('mode') !== 'online';
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) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
4
4
  "description": "An easy to use react component for impleting peer-to-peer chatting.",
5
5
  "main": "./build/index.js",
6
6
  "type": "module",