react-peer-chat 0.3.0 → 0.3.2

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
@@ -5,7 +5,7 @@ type Message = {
5
5
  text: string;
6
6
  };
7
7
  type ChildrenOptions = {
8
- opponentName?: string;
8
+ remotePeer?: string;
9
9
  messages?: Message[];
10
10
  addMessage?: (message: Message, sendToRemotePeer?: boolean) => void;
11
11
  audio?: boolean;
@@ -26,7 +26,8 @@ type Props = {
26
26
  dialogOptions?: DialogOptions;
27
27
  onError?: () => void;
28
28
  children?: (childrenOptions: ChildrenOptions) => ReactNode;
29
- } & DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
30
- export default function Chat({ name, peerId, remotePeerId, peerOptions, text, voice, dialogOptions, onError, children, ...props }: Props): React.JSX.Element;
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;
31
32
  export declare const clearChat: () => void;
32
33
  export {};
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, setName = false) {
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.send({ type: 'messages', messages });
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 === 'messages') {
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 }), true);
63
+ handleConnection(peer.connect(remotePeerId, { metadata: name }));
65
64
  }
66
65
  if (audio) {
67
66
  const getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
@@ -111,24 +110,23 @@ 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, opponentName, messages]);
113
+ }, [dialog, remotePeer, messages]);
115
114
  return React.createElement("div", { className: 'rpc-main', ...props },
116
- typeof children === 'function' ? children({ opponentName, messages, addMessage, audio, setAudio }) : React.createElement(React.Fragment, null,
115
+ typeof children === 'function' ? children({ remotePeer, messages, addMessage, audio, setAudio }) : React.createElement(React.Fragment, null,
117
116
  text && React.createElement("div", null,
118
- dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) })
119
- : React.createElement("div", { className: 'rpc-notification' },
120
- React.createElement(BiSolidMessageDetail, { onClick: () => {
121
- setNotification(false);
122
- setDialog(true);
123
- } }),
124
- notification && React.createElement("span", { className: 'rpc-badge' })),
117
+ dialog ? React.createElement(BiSolidMessageX, { onClick: () => setDialog(false) }) : React.createElement("div", { className: 'rpc-notification' },
118
+ React.createElement(BiSolidMessageDetail, { onClick: () => {
119
+ setNotification(false);
120
+ setDialog(true);
121
+ } }),
122
+ notification && React.createElement("span", { className: 'rpc-badge' })),
125
123
  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 },
126
124
  React.createElement("div", { className: 'rpc-heading' }, "Chat"),
127
125
  React.createElement("hr", null),
128
126
  React.createElement("div", null,
129
- React.createElement("div", { ref: containerRef, className: 'rpc-message-container' }, opponentName && messages.map(({ id, text }, i) => React.createElement("div", { key: i },
127
+ React.createElement("div", { ref: containerRef, className: 'rpc-message-container' }, messages.map(({ id, text }, i) => React.createElement("div", { key: i },
130
128
  React.createElement("strong", null,
131
- id === peerId ? 'You' : opponentName,
129
+ id === peerId ? 'You' : remotePeer,
132
130
  ": "),
133
131
  React.createElement("span", null, text)))),
134
132
  React.createElement("hr", null),
@@ -147,4 +145,7 @@ export default function Chat({ name, peerId, remotePeerId, peerOptions, text = t
147
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) }))),
148
146
  voice && audio && React.createElement("audio", { ref: streamRef, autoPlay: true, style: { display: 'none' } }));
149
147
  }
150
- export const clearChat = () => removeStorage('rpc-messages');
148
+ export const clearChat = () => {
149
+ removeStorage('rpc-remote-peer');
150
+ removeStorage('rpc-messages');
151
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-peer-chat",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
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",