@aslaluroba/help-center-react 3.0.0 → 3.0.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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "dist/index.js",
4
4
  "module": "dist/index.esm.js",
5
5
  "types": "dist/index.d.ts",
6
- "version": "3.0.0",
6
+ "version": "3.0.2",
7
7
  "description": "BabylAI Help Center Widget for React and Next.js",
8
8
  "private": false,
9
9
  "exports": {
@@ -69,7 +69,7 @@ export class ClientAblyService {
69
69
  throw new Error('Chat client not initialized');
70
70
  }
71
71
 
72
- const roomName = `session:${tenantId}-${sessionId}`;
72
+ const roomName = `session:${tenantId}:${sessionId}`;
73
73
 
74
74
  // Set up raw channel subscription for server messages
75
75
  if (this.client) {
@@ -80,11 +80,12 @@ export class ClientAblyService {
80
80
  try {
81
81
  const messageContent =
82
82
  typeof message.data === 'string' ? message.data : message.data?.content || message.data?.message;
83
- const senderType = 3; // Assistant
83
+ const senderType = message.data?.senderType || 3; // Assistant
84
84
  const needsAgent = message.data?.needsAgent || false;
85
85
 
86
86
  onMessageReceived(messageContent, senderType, needsAgent);
87
87
  } catch (error) {
88
+ // Handle error silently
88
89
  console.error('[AblyService] Error processing ReceiveMessage:', error);
89
90
  }
90
91
  });
@@ -117,7 +118,6 @@ export class ClientAblyService {
117
118
  this.isConnected = false;
118
119
  this.sessionId = null;
119
120
  } catch (error) {
120
- console.error('[AblyService] Error stopping Ably connection:', error);
121
121
  // Reset state even if there's an error
122
122
  this.isConnected = false;
123
123
  this.sessionId = null;
@@ -134,4 +134,23 @@ export class ClientAblyService {
134
134
  static getConnectionState(): string {
135
135
  return this.client?.connection.state || 'disconnected';
136
136
  }
137
+
138
+ // Method to manually send a message (if needed for debugging or direct messaging)
139
+ static async sendMessage(messageContent: string, senderType: number = 1) {
140
+ if (!this.channel || !this.isConnected) {
141
+ throw new Error('Connection not active');
142
+ }
143
+
144
+ try {
145
+ await this.channel.publish('message', {
146
+ text: messageContent,
147
+ metadata: {
148
+ senderType,
149
+ sentAt: new Date().toISOString(),
150
+ },
151
+ });
152
+ } catch (error) {
153
+ throw error;
154
+ }
155
+ }
137
156
  }
@@ -5,6 +5,7 @@ import React, { useEffect, useRef, useState, useCallback, useMemo } from 'react'
5
5
  import LoadingGif from './../../../assets/animatedLogo.gif';
6
6
  import Seperator from './../../../assets/icons/seperator.svg';
7
7
  import LogoIcon from './../../../assets/logo.svg';
8
+ import HumanIcon from './../../../assets/icons/user.svg';
8
9
 
9
10
  interface ChatWindowProps {
10
11
  onSendMessage: (message: string) => void;
@@ -41,10 +42,18 @@ const MessageComponent = React.memo(
41
42
  </div>
42
43
  )}
43
44
  <div className={`babylai-mb-4 babylai-flex ${textDirection}`}>
44
- {isFirstInSequence && message.senderType === 3 && (
45
+ {isFirstInSequence && (message.senderType === 3 || message.senderType === 2) && (
45
46
  <div className='babylai-flex-shrink-0 babylai-me-3'>
46
- <div className='babylai-w-8 babylai-h-8 babylai-rounded-full babylai-bg-purple-500 babylai-flex babylai-items-center babylai-justify-center'>
47
- <LogoIcon className='babylai-w-4 babylai-h-4 babylai-text-white' />
47
+ <div
48
+ className={`babylai-w-8 babylai-h-8 babylai-rounded-full babylai-flex babylai-items-center babylai-justify-center ${
49
+ message?.senderType === 3 ? 'babylai-bg-primary' : 'babylai-bg-black-white-50'
50
+ }`}
51
+ >
52
+ {message?.senderType === 3 ? (
53
+ <LogoIcon className='babylai-w-4 babylai-h-4 babylai-text-white' />
54
+ ) : (
55
+ <HumanIcon className='babylai-w-4 babylai-h-4 babylai-text-primary' />
56
+ )}
48
57
  </div>
49
58
  </div>
50
59
  )}
@@ -78,7 +78,7 @@ export function HelpCenter({
78
78
  setAssistantStatus('idle');
79
79
  };
80
80
 
81
- const handleEndChat = async (option?: Option) => {
81
+ const handleEndChat = async () => {
82
82
  if (!sessionId || !selectedOption) return;
83
83
 
84
84
  try {
@@ -98,9 +98,7 @@ export function HelpCenter({
98
98
  setMessages([]);
99
99
 
100
100
  setIsReviewDialogOpen(true);
101
- if (option) {
102
- handleStartChat(option);
103
- }
101
+
104
102
  } catch (error) {
105
103
  console.error('Error ending chat:', error);
106
104
  setError('Failed to end chat session');
@@ -121,8 +119,7 @@ export function HelpCenter({
121
119
  const response = await apiRequest(`Client/ClientChatSession/${reviewSessionId}/review`, 'POST', payload);
122
120
  if (!response.ok) throw new Error('Failed to send chat review');
123
121
 
124
- setIsReviewDialogOpen(false);
125
- setReviewSessionId(null);
122
+ handleCloseChatReview()
126
123
  } catch (error) {
127
124
  console.error('Error sending chat review:', error);
128
125
  setError('Failed to send chat review');
@@ -139,9 +136,6 @@ export function HelpCenter({
139
136
  };
140
137
 
141
138
  const startNewChatSession = async (option: Option) => {
142
- if (isAblyConnected || sessionId) {
143
- handleEndChat();
144
- }
145
139
 
146
140
  try {
147
141
  setStatus('loading');
@@ -346,7 +340,7 @@ export function HelpCenter({
346
340
  showHelpScreen={showHelpScreen}
347
341
  />
348
342
  )}
349
- {isOpen && !!isReviewDialogOpen && (
343
+ {isOpen && !!isReviewDialogOpen && reviewSessionId && (
350
344
  <ReviewDialog handleSubmit={handleSendChatReview} onClose={handleCloseChatReview} />
351
345
  )}
352
346
  </div>
@@ -121,61 +121,28 @@ export function HelpPopup({
121
121
  setEndChatConfirmation(false);
122
122
  }, []);
123
123
 
124
- const handleEndAndStartNewChat = useCallback(async () => {
124
+ const handleEndAndStartNewChat = async () => {
125
125
  if (tempSelectedOption) {
126
126
  setStartNewChatConfirmation(false);
127
127
 
128
- try {
129
- // First end the current chat and wait for it to complete
130
- await onEndChat();
131
-
132
- // Wait for sessionId to be cleared (indicating the session is fully closed)
133
- // We'll use a polling mechanism to wait for the state to update
134
- const maxAttempts = 50; // 5 seconds max wait time
135
- let attempts = 0;
136
-
137
- while (sessionId && attempts < maxAttempts) {
138
- await new Promise((resolve) => setTimeout(resolve, 100)); // Wait 100ms
139
- attempts++;
140
- }
141
-
142
- // Only start new chat after current session is fully closed
143
- if (!sessionId) {
144
- setShowChat(true);
145
- onStartChat(tempSelectedOption);
146
- setSelectedOption(tempSelectedOption);
147
- } else {
148
- console.warn('Session did not close properly, but proceeding with new chat');
128
+ await handleEndChat()
129
+ .then(() => {
149
130
  setShowChat(true);
150
131
  onStartChat(tempSelectedOption);
151
132
  setSelectedOption(tempSelectedOption);
152
- }
153
- } catch (error) {
154
- console.error('Error ending current chat:', error);
155
- // Even if ending fails, try to start new chat
156
- setShowChat(true);
157
- onStartChat(tempSelectedOption);
158
- setSelectedOption(tempSelectedOption);
159
- } finally {
160
- setTempSelectedOption(null);
161
- }
133
+ })
134
+ .finally(() => {
135
+ setTempSelectedOption(null);
136
+ })
162
137
  }
163
- }, [
164
- onEndChat,
165
- onStartChat,
166
- setSelectedOption,
167
- setTempSelectedOption,
168
- tempSelectedOption,
169
- setStartNewChatConfirmation,
170
- sessionId,
171
- ]);
138
+ };
172
139
 
173
- const handleEndChat = useCallback(() => {
140
+ const handleEndChat = async () => {
174
141
  setEndChatConfirmation(false);
175
- onEndChat();
142
+ await onEndChat();
176
143
  setShowChat(false);
177
144
  setSelectedOption(null);
178
- }, [selectedOption, onEndChat, setSelectedOption]);
145
+ };
179
146
 
180
147
  const handleShowActiveChat = useCallback(() => {
181
148
  setShowChat(true);