@meshagent/meshagent-react 0.5.1 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [0.5.2]
2
+ - Stability
3
+
1
4
  ## [0.5.1]
2
5
  - Stability
3
6
 
@@ -24,6 +24,7 @@ export interface UseMessageChatResult {
24
24
  sendMessage: (message: ChatMessage) => void;
25
25
  selectAttachments: (files: File[]) => void;
26
26
  attachments: FileUpload[];
27
+ setAttachments: (attachments: FileUpload[]) => void;
27
28
  }
28
29
  export declare function fileToAsyncIterable(file: File): AsyncIterable<Uint8Array>;
29
30
  export declare function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }: UseMessageChatProps): UseMessageChatResult;
package/dist/cjs/chat.js CHANGED
@@ -128,27 +128,28 @@ function useChat({ room, path, participants, participantNames, initialMessage, i
128
128
  const [attachments, setAttachments] = (0, react_1.useState)([]);
129
129
  (0, document_connection_scope_1.useDocumentChanged)({
130
130
  document,
131
- onChanged: (doc) => {
132
- setMessages(mapMessages(doc));
133
- },
131
+ onChanged: (doc) => setMessages(mapMessages(doc)),
134
132
  });
135
133
  const selectAttachments = (0, react_1.useCallback)((files) => {
136
134
  const attachmentsToUpload = files.map((file) => new file_upload_1.MeshagentFileUpload(room, `uploaded-files/${file.name}`, fileToAsyncIterable(file), file.size));
137
135
  setAttachments(attachmentsToUpload);
138
- }, [room, document]);
136
+ }, [room]);
139
137
  const sendMessage = (0, react_1.useCallback)((message) => {
140
138
  const children = document?.root.getChildren() || [];
141
139
  const thread = children.find((c) => c.tagName === "messages");
142
140
  if (!thread) {
143
141
  return;
144
142
  }
145
- thread.createChildElement("message", {
143
+ const m = thread.createChildElement("message", {
146
144
  id: message.id,
147
145
  text: message.text,
148
146
  created_at: new Date().toISOString(),
149
147
  author_name: room.localParticipant.getAttribute("name"),
150
148
  author_ref: null,
151
149
  });
150
+ for (const path of message.attachments) {
151
+ m.createChildElement("file", { path });
152
+ }
152
153
  for (const participant of getOnlineParticipants(room, document)) {
153
154
  room.messaging.sendMessage({
154
155
  to: participant,
@@ -156,11 +157,11 @@ function useChat({ room, path, participants, participantNames, initialMessage, i
156
157
  message: {
157
158
  path,
158
159
  text: message.text,
159
- attachments: message.attachments,
160
+ attachments: message.attachments.map(path => ({ path })),
160
161
  },
161
162
  });
162
163
  }
163
- }, [document]);
164
+ }, [document, attachments]);
164
165
  (0, react_1.useEffect)(() => {
165
166
  if (document) {
166
167
  ensureParticipants(document, room.localParticipant, includeLocalParticipant ?? true, participants ?? [], participantNames ?? []);
@@ -174,5 +175,6 @@ function useChat({ room, path, participants, participantNames, initialMessage, i
174
175
  sendMessage,
175
176
  selectAttachments,
176
177
  attachments,
178
+ setAttachments,
177
179
  };
178
180
  }
@@ -60,9 +60,7 @@ function useDocumentConnection({ room, path }) {
60
60
  function useDocumentChanged({ document, onChanged }) {
61
61
  (0, react_1.useEffect)(() => {
62
62
  if (document) {
63
- const s = document.listen(() => {
64
- setTimeout(() => onChanged(document), 40);
65
- });
63
+ const s = document.listen(() => onChanged(document));
66
64
  onChanged(document);
67
65
  return () => s.unsubscribe();
68
66
  }
@@ -3,3 +3,4 @@ export * from './client-toolkits';
3
3
  export * from './document-connection-scope';
4
4
  export * from './file-upload';
5
5
  export * from './room-connection-scope';
6
+ export * from './wail-for-participant';
package/dist/cjs/index.js CHANGED
@@ -19,3 +19,4 @@ __exportStar(require("./client-toolkits"), exports);
19
19
  __exportStar(require("./document-connection-scope"), exports);
20
20
  __exportStar(require("./file-upload"), exports);
21
21
  __exportStar(require("./room-connection-scope"), exports);
22
+ __exportStar(require("./wail-for-participant"), exports);
@@ -0,0 +1,3 @@
1
+ import { Participant } from '@meshagent/meshagent';
2
+ import { UseRoomConnectionResult } from './room-connection-scope';
3
+ export declare function useWaitForAgentParticipant(connection: UseRoomConnectionResult | null): Participant | null;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useWaitForAgentParticipant = useWaitForAgentParticipant;
4
+ const react_1 = require("react");
5
+ function useWaitForAgentParticipant(connection) {
6
+ const [participant, setParticipant] = (0, react_1.useState)(null);
7
+ (0, react_1.useEffect)(() => {
8
+ if (connection == null || !connection.ready) {
9
+ return;
10
+ }
11
+ function onChange() {
12
+ const participants = Array.from(connection.client.messaging.remoteParticipants);
13
+ const agentParticipant = participants.find(p => p.role === 'agent');
14
+ if (agentParticipant) {
15
+ setParticipant(agentParticipant);
16
+ }
17
+ }
18
+ connection.client.messaging.on('change', onChange);
19
+ onChange();
20
+ return () => connection.client.messaging.off('change', onChange);
21
+ }, [connection]);
22
+ return participant;
23
+ }
@@ -24,6 +24,7 @@ export interface UseMessageChatResult {
24
24
  sendMessage: (message: ChatMessage) => void;
25
25
  selectAttachments: (files: File[]) => void;
26
26
  attachments: FileUpload[];
27
+ setAttachments: (attachments: FileUpload[]) => void;
27
28
  }
28
29
  export declare function fileToAsyncIterable(file: File): AsyncIterable<Uint8Array>;
29
30
  export declare function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }: UseMessageChatProps): UseMessageChatResult;
package/dist/esm/chat.js CHANGED
@@ -122,27 +122,28 @@ export function useChat({ room, path, participants, participantNames, initialMes
122
122
  const [attachments, setAttachments] = useState([]);
123
123
  useDocumentChanged({
124
124
  document,
125
- onChanged: (doc) => {
126
- setMessages(mapMessages(doc));
127
- },
125
+ onChanged: (doc) => setMessages(mapMessages(doc)),
128
126
  });
129
127
  const selectAttachments = useCallback((files) => {
130
128
  const attachmentsToUpload = files.map((file) => new MeshagentFileUpload(room, `uploaded-files/${file.name}`, fileToAsyncIterable(file), file.size));
131
129
  setAttachments(attachmentsToUpload);
132
- }, [room, document]);
130
+ }, [room]);
133
131
  const sendMessage = useCallback((message) => {
134
132
  const children = document?.root.getChildren() || [];
135
133
  const thread = children.find((c) => c.tagName === "messages");
136
134
  if (!thread) {
137
135
  return;
138
136
  }
139
- thread.createChildElement("message", {
137
+ const m = thread.createChildElement("message", {
140
138
  id: message.id,
141
139
  text: message.text,
142
140
  created_at: new Date().toISOString(),
143
141
  author_name: room.localParticipant.getAttribute("name"),
144
142
  author_ref: null,
145
143
  });
144
+ for (const path of message.attachments) {
145
+ m.createChildElement("file", { path });
146
+ }
146
147
  for (const participant of getOnlineParticipants(room, document)) {
147
148
  room.messaging.sendMessage({
148
149
  to: participant,
@@ -150,11 +151,11 @@ export function useChat({ room, path, participants, participantNames, initialMes
150
151
  message: {
151
152
  path,
152
153
  text: message.text,
153
- attachments: message.attachments,
154
+ attachments: message.attachments.map(path => ({ path })),
154
155
  },
155
156
  });
156
157
  }
157
- }, [document]);
158
+ }, [document, attachments]);
158
159
  useEffect(() => {
159
160
  if (document) {
160
161
  ensureParticipants(document, room.localParticipant, includeLocalParticipant ?? true, participants ?? [], participantNames ?? []);
@@ -168,5 +169,6 @@ export function useChat({ room, path, participants, participantNames, initialMes
168
169
  sendMessage,
169
170
  selectAttachments,
170
171
  attachments,
172
+ setAttachments,
171
173
  };
172
174
  }
@@ -56,9 +56,7 @@ export function useDocumentConnection({ room, path }) {
56
56
  export function useDocumentChanged({ document, onChanged }) {
57
57
  useEffect(() => {
58
58
  if (document) {
59
- const s = document.listen(() => {
60
- setTimeout(() => onChanged(document), 40);
61
- });
59
+ const s = document.listen(() => onChanged(document));
62
60
  onChanged(document);
63
61
  return () => s.unsubscribe();
64
62
  }
@@ -3,3 +3,4 @@ export * from './client-toolkits';
3
3
  export * from './document-connection-scope';
4
4
  export * from './file-upload';
5
5
  export * from './room-connection-scope';
6
+ export * from './wail-for-participant';
package/dist/esm/index.js CHANGED
@@ -3,3 +3,4 @@ export * from './client-toolkits';
3
3
  export * from './document-connection-scope';
4
4
  export * from './file-upload';
5
5
  export * from './room-connection-scope';
6
+ export * from './wail-for-participant';
@@ -0,0 +1,3 @@
1
+ import { Participant } from '@meshagent/meshagent';
2
+ import { UseRoomConnectionResult } from './room-connection-scope';
3
+ export declare function useWaitForAgentParticipant(connection: UseRoomConnectionResult | null): Participant | null;
@@ -0,0 +1,20 @@
1
+ import { useState, useEffect } from 'react';
2
+ export function useWaitForAgentParticipant(connection) {
3
+ const [participant, setParticipant] = useState(null);
4
+ useEffect(() => {
5
+ if (connection == null || !connection.ready) {
6
+ return;
7
+ }
8
+ function onChange() {
9
+ const participants = Array.from(connection.client.messaging.remoteParticipants);
10
+ const agentParticipant = participants.find(p => p.role === 'agent');
11
+ if (agentParticipant) {
12
+ setParticipant(agentParticipant);
13
+ }
14
+ }
15
+ connection.client.messaging.on('change', onChange);
16
+ onChange();
17
+ return () => connection.client.messaging.off('change', onChange);
18
+ }, [connection]);
19
+ return participant;
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshagent/meshagent-react",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Meshagent React Client",
5
5
  "homepage": "https://github.com/meshagent/meshagent-react",
6
6
  "scripts": {
@@ -31,9 +31,10 @@
31
31
  "@types/react-dom": "^19.1.6"
32
32
  },
33
33
  "dependencies": {
34
- "@meshagent/meshagent": "^0.5.1",
34
+ "react": "^19.1.0",
35
+ "@meshagent/meshagent": "^0.5.2",
35
36
  "react-dom": "^19.1.0",
36
37
  "typescript": "^5.8.3",
37
38
  "uuid": "^11.1.0"
38
39
  }
39
- }
40
+ }