wealth-alpha-chat-widget 2.1.3 → 2.1.5

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.cjs CHANGED
@@ -348,6 +348,7 @@ function useAuth(opts) {
348
348
  const [error, setError] = react.useState(null);
349
349
  const [tick, setTick] = react.useState(0);
350
350
  const onErrorRef = react.useRef(onError);
351
+ const hadUserRef = react.useRef(false);
351
352
  react.useEffect(() => {
352
353
  onErrorRef.current = onError;
353
354
  }, [onError]);
@@ -360,12 +361,15 @@ function useAuth(opts) {
360
361
  if (!session?.token) {
361
362
  setUser(null);
362
363
  setLoading(false);
364
+ hadUserRef.current = false;
363
365
  return;
364
366
  }
365
367
  const controller = new AbortController();
366
- setLoading(true);
368
+ const isInitial = !hadUserRef.current;
369
+ if (isInitial) setLoading(true);
367
370
  setError(null);
368
371
  checkAuth(apiBase, authCheck, controller.signal).then((u) => {
372
+ hadUserRef.current = true;
369
373
  setUser(u);
370
374
  setError(null);
371
375
  }).catch((err) => {
@@ -373,12 +377,13 @@ function useAuth(opts) {
373
377
  if (err instanceof AuthExpiredError) {
374
378
  clearSession();
375
379
  setUser(null);
380
+ hadUserRef.current = false;
376
381
  } else {
377
382
  setError(err);
378
383
  onErrorRef.current?.(err);
379
384
  }
380
385
  }).finally(() => {
381
- if (!controller.signal.aborted) setLoading(false);
386
+ if (!controller.signal.aborted && isInitial) setLoading(false);
382
387
  });
383
388
  return () => controller.abort();
384
389
  }, [apiBase, authCheck, enabled, tick]);
@@ -1322,7 +1327,7 @@ function WealthChat(props) {
1322
1327
  ttlSeconds: sessionTTL,
1323
1328
  onExpire: onSessionExpire
1324
1329
  });
1325
- const { isLoggedIn, user, loading: authLoading } = useAuth({
1330
+ const { isLoggedIn, user, loading: authLoading, refresh: refreshAuth } = useAuth({
1326
1331
  apiBase,
1327
1332
  authCheck,
1328
1333
  enabled: mounted && open,
@@ -1379,6 +1384,46 @@ function WealthChat(props) {
1379
1384
  });
1380
1385
  onLogin?.();
1381
1386
  }, [isLoggedIn, open, chatState.messages.length, welcomeMessage, session, user, appendBotResponse, onLogin, welcomeShownRef]);
1387
+ react.useEffect(() => {
1388
+ if (!mounted || !open) return;
1389
+ let last = 0;
1390
+ const THROTTLE_MS = 3e3;
1391
+ const trigger = () => {
1392
+ const now = Date.now();
1393
+ if (now - last < THROTTLE_MS) return;
1394
+ last = now;
1395
+ refreshAuth();
1396
+ };
1397
+ const onVisible = () => {
1398
+ if (typeof document !== "undefined" && document.visibilityState === "visible") trigger();
1399
+ };
1400
+ window.addEventListener("focus", trigger);
1401
+ window.addEventListener("wealthalpha:refresh-auth", trigger);
1402
+ document.addEventListener("visibilitychange", onVisible);
1403
+ return () => {
1404
+ window.removeEventListener("focus", trigger);
1405
+ window.removeEventListener("wealthalpha:refresh-auth", trigger);
1406
+ document.removeEventListener("visibilitychange", onVisible);
1407
+ };
1408
+ }, [mounted, open, refreshAuth]);
1409
+ const planRef = react.useRef({ known: false });
1410
+ react.useEffect(() => {
1411
+ if (!user) {
1412
+ planRef.current = { known: false };
1413
+ return;
1414
+ }
1415
+ const prev = planRef.current;
1416
+ planRef.current = { known: true, plan: user.plan };
1417
+ if (!prev.known || prev.plan === user.plan) return;
1418
+ if (chatState.messages.length === 0) return;
1419
+ deactivatePriorChips();
1420
+ appendBotResponse({
1421
+ message: user.welcomeMessage ?? buildWelcome(user.name, user.plan),
1422
+ chips: user.rootChips ?? [],
1423
+ sessionId: session?.sessionId ?? "",
1424
+ endOfFlow: false
1425
+ });
1426
+ }, [user, chatState.messages.length, deactivatePriorChips, appendBotResponse, session]);
1382
1427
  const handleChipClick = react.useCallback(
1383
1428
  async (chip) => {
1384
1429
  touch();