@aslaluroba/help-center-react 2.1.1 → 2.1.3

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/dist/index.js CHANGED
@@ -1185,11 +1185,50 @@ function HelpPopup({ onClose, helpScreen, status, error, onStartChat, onSendMess
1185
1185
  const handleEndAndStartNewChat = React.useCallback(async () => {
1186
1186
  if (tempSelectedOption) {
1187
1187
  setStartNewChatConfirmation(false);
1188
- setShowChat(true);
1189
- onEndChat(tempSelectedOption);
1190
- setTempSelectedOption(null);
1188
+ try {
1189
+ // First end the current chat and wait for it to complete
1190
+ await onEndChat();
1191
+ // Wait for sessionId to be cleared (indicating the session is fully closed)
1192
+ // We'll use a polling mechanism to wait for the state to update
1193
+ const maxAttempts = 50; // 5 seconds max wait time
1194
+ let attempts = 0;
1195
+ while (sessionId && attempts < maxAttempts) {
1196
+ await new Promise((resolve) => setTimeout(resolve, 100)); // Wait 100ms
1197
+ attempts++;
1198
+ }
1199
+ // Only start new chat after current session is fully closed
1200
+ if (!sessionId) {
1201
+ setShowChat(true);
1202
+ onStartChat(tempSelectedOption);
1203
+ setSelectedOption(tempSelectedOption);
1204
+ }
1205
+ else {
1206
+ console.warn('Session did not close properly, but proceeding with new chat');
1207
+ setShowChat(true);
1208
+ onStartChat(tempSelectedOption);
1209
+ setSelectedOption(tempSelectedOption);
1210
+ }
1211
+ }
1212
+ catch (error) {
1213
+ console.error('Error ending current chat:', error);
1214
+ // Even if ending fails, try to start new chat
1215
+ setShowChat(true);
1216
+ onStartChat(tempSelectedOption);
1217
+ setSelectedOption(tempSelectedOption);
1218
+ }
1219
+ finally {
1220
+ setTempSelectedOption(null);
1221
+ }
1191
1222
  }
1192
- }, [onEndChat, setTempSelectedOption, tempSelectedOption, setStartNewChatConfirmation]);
1223
+ }, [
1224
+ onEndChat,
1225
+ onStartChat,
1226
+ setSelectedOption,
1227
+ setTempSelectedOption,
1228
+ tempSelectedOption,
1229
+ setStartNewChatConfirmation,
1230
+ sessionId,
1231
+ ]);
1193
1232
  const handleEndChat = React.useCallback(() => {
1194
1233
  setEndChatConfirmation(false);
1195
1234
  onEndChat();
@@ -1278,6 +1317,7 @@ function HelpCenter({ helpScreenId, user, showArrow = true, language = 'en', mes
1278
1317
  const [error, setError] = React.useState('');
1279
1318
  const [selectedOption, setSelectedOption] = React.useState(null);
1280
1319
  const [sessionId, setSessionId] = React.useState(null);
1320
+ const [reviewSessionId, setReviewSessionId] = React.useState(null);
1281
1321
  const [isSignalRConnected, setIsSignalRConnected] = React.useState(false);
1282
1322
  const [isChatClosed, setIsChatClosed] = React.useState(false);
1283
1323
  const [messages, setMessages] = React.useState([]);
@@ -1318,6 +1358,12 @@ function HelpCenter({ helpScreenId, user, showArrow = true, language = 'en', mes
1318
1358
  const response = await apiRequest(`Client/ClientChatSession/${sessionId}/close`, 'POST');
1319
1359
  if (!response.ok)
1320
1360
  throw new Error('Failed to close chat session');
1361
+ // Store sessionId for review before clearing the main sessionId
1362
+ setReviewSessionId(sessionId);
1363
+ // Clear the sessionId after successfully closing the session
1364
+ setSessionId(null);
1365
+ setSelectedOption(null);
1366
+ setMessages([]);
1321
1367
  setIsReviewDialogOpen(true);
1322
1368
  if (option) {
1323
1369
  handleStartChat(option);
@@ -1327,17 +1373,22 @@ function HelpCenter({ helpScreenId, user, showArrow = true, language = 'en', mes
1327
1373
  console.error('Error ending chat:', error);
1328
1374
  setError('Failed to end chat session');
1329
1375
  setAssistantStatus('idle');
1376
+ // Even if there's an error, clear the session state to prevent stuck state
1377
+ setReviewSessionId(sessionId); // Store for review even if there's an error
1378
+ setSessionId(null);
1379
+ setSelectedOption(null);
1330
1380
  }
1331
1381
  };
1332
1382
  const handleSendChatReview = async ({ comment, rating }) => {
1333
- if (!sessionId)
1383
+ if (!reviewSessionId)
1334
1384
  return;
1335
1385
  const payload = { rating, comment };
1336
1386
  try {
1337
- const response = await apiRequest(`Client/ClientChatSession/${sessionId}/review`, 'POST', payload);
1387
+ const response = await apiRequest(`Client/ClientChatSession/${reviewSessionId}/review`, 'POST', payload);
1338
1388
  if (!response.ok)
1339
1389
  throw new Error('Failed to send chat review');
1340
1390
  setIsReviewDialogOpen(false);
1391
+ setReviewSessionId(null); // Clear review session ID after review is sent
1341
1392
  }
1342
1393
  catch (error) {
1343
1394
  console.error('Error sending chat review:', error);
@@ -1346,6 +1397,7 @@ function HelpCenter({ helpScreenId, user, showArrow = true, language = 'en', mes
1346
1397
  };
1347
1398
  const handleCloseChatReview = () => {
1348
1399
  setIsReviewDialogOpen(false);
1400
+ setReviewSessionId(null); // Clear review session ID when review is closed
1349
1401
  };
1350
1402
  const handleStartChat = async (option) => {
1351
1403
  await startNewChatSession(option);