wealth-alpha-chat-widget 2.1.4 → 2.1.6

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.mjs CHANGED
@@ -342,6 +342,7 @@ function useAuth(opts) {
342
342
  const [error, setError] = useState(null);
343
343
  const [tick, setTick] = useState(0);
344
344
  const onErrorRef = useRef(onError);
345
+ const hadUserRef = useRef(false);
345
346
  useEffect(() => {
346
347
  onErrorRef.current = onError;
347
348
  }, [onError]);
@@ -354,12 +355,15 @@ function useAuth(opts) {
354
355
  if (!session?.token) {
355
356
  setUser(null);
356
357
  setLoading(false);
358
+ hadUserRef.current = false;
357
359
  return;
358
360
  }
359
361
  const controller = new AbortController();
360
- setLoading(true);
362
+ const isInitial = !hadUserRef.current;
363
+ if (isInitial) setLoading(true);
361
364
  setError(null);
362
365
  checkAuth(apiBase, authCheck, controller.signal).then((u) => {
366
+ hadUserRef.current = true;
363
367
  setUser(u);
364
368
  setError(null);
365
369
  }).catch((err) => {
@@ -367,12 +371,13 @@ function useAuth(opts) {
367
371
  if (err instanceof AuthExpiredError) {
368
372
  clearSession();
369
373
  setUser(null);
374
+ hadUserRef.current = false;
370
375
  } else {
371
376
  setError(err);
372
377
  onErrorRef.current?.(err);
373
378
  }
374
379
  }).finally(() => {
375
- if (!controller.signal.aborted) setLoading(false);
380
+ if (!controller.signal.aborted && isInitial) setLoading(false);
376
381
  });
377
382
  return () => controller.abort();
378
383
  }, [apiBase, authCheck, enabled, tick]);
@@ -1316,7 +1321,7 @@ function WealthChat(props) {
1316
1321
  ttlSeconds: sessionTTL,
1317
1322
  onExpire: onSessionExpire
1318
1323
  });
1319
- const { isLoggedIn, user, loading: authLoading } = useAuth({
1324
+ const { isLoggedIn, user, loading: authLoading, refresh: refreshAuth } = useAuth({
1320
1325
  apiBase,
1321
1326
  authCheck,
1322
1327
  enabled: mounted && open,
@@ -1373,6 +1378,62 @@ function WealthChat(props) {
1373
1378
  });
1374
1379
  onLogin?.();
1375
1380
  }, [isLoggedIn, open, chatState.messages.length, welcomeMessage, session, user, appendBotResponse, onLogin, welcomeShownRef]);
1381
+ useEffect(() => {
1382
+ if (!mounted || !open) return;
1383
+ let last = 0;
1384
+ const THROTTLE_MS = 3e3;
1385
+ const trigger = () => {
1386
+ const now = Date.now();
1387
+ if (now - last < THROTTLE_MS) return;
1388
+ last = now;
1389
+ refreshAuth();
1390
+ };
1391
+ const onVisible = () => {
1392
+ if (typeof document !== "undefined" && document.visibilityState === "visible") trigger();
1393
+ };
1394
+ window.addEventListener("focus", trigger);
1395
+ window.addEventListener("wealthalpha:refresh-auth", trigger);
1396
+ document.addEventListener("visibilitychange", onVisible);
1397
+ return () => {
1398
+ window.removeEventListener("focus", trigger);
1399
+ window.removeEventListener("wealthalpha:refresh-auth", trigger);
1400
+ document.removeEventListener("visibilitychange", onVisible);
1401
+ };
1402
+ }, [mounted, open, refreshAuth]);
1403
+ const planRef = useRef({ known: false });
1404
+ useEffect(() => {
1405
+ if (!user) {
1406
+ planRef.current = { known: false };
1407
+ return;
1408
+ }
1409
+ const prev = planRef.current;
1410
+ planRef.current = { known: true, plan: user.plan };
1411
+ if (!prev.known || prev.plan === user.plan) return;
1412
+ if (chatState.messages.length === 0) return;
1413
+ const hasUserActivity = chatState.messages.some((m) => m.role === "user");
1414
+ if (!hasUserActivity) {
1415
+ clearChat();
1416
+ welcomeShownRef.shown = false;
1417
+ setHistory([]);
1418
+ return;
1419
+ }
1420
+ deactivatePriorChips();
1421
+ appendBotResponse({
1422
+ message: user.welcomeMessage ?? buildWelcome(user.name, user.plan),
1423
+ chips: user.rootChips ?? [],
1424
+ sessionId: session?.sessionId ?? "",
1425
+ endOfFlow: false
1426
+ });
1427
+ }, [
1428
+ user,
1429
+ chatState.messages,
1430
+ clearChat,
1431
+ setHistory,
1432
+ deactivatePriorChips,
1433
+ appendBotResponse,
1434
+ session,
1435
+ welcomeShownRef
1436
+ ]);
1376
1437
  const handleChipClick = useCallback(
1377
1438
  async (chip) => {
1378
1439
  touch();