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.cjs +64 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +64 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
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,62 @@ 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
|
+
const hasUserActivity = chatState.messages.some((m) => m.role === "user");
|
|
1420
|
+
if (!hasUserActivity) {
|
|
1421
|
+
clearChat();
|
|
1422
|
+
welcomeShownRef.shown = false;
|
|
1423
|
+
setHistory([]);
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
deactivatePriorChips();
|
|
1427
|
+
appendBotResponse({
|
|
1428
|
+
message: user.welcomeMessage ?? buildWelcome(user.name, user.plan),
|
|
1429
|
+
chips: user.rootChips ?? [],
|
|
1430
|
+
sessionId: session?.sessionId ?? "",
|
|
1431
|
+
endOfFlow: false
|
|
1432
|
+
});
|
|
1433
|
+
}, [
|
|
1434
|
+
user,
|
|
1435
|
+
chatState.messages,
|
|
1436
|
+
clearChat,
|
|
1437
|
+
setHistory,
|
|
1438
|
+
deactivatePriorChips,
|
|
1439
|
+
appendBotResponse,
|
|
1440
|
+
session,
|
|
1441
|
+
welcomeShownRef
|
|
1442
|
+
]);
|
|
1382
1443
|
const handleChipClick = react.useCallback(
|
|
1383
1444
|
async (chip) => {
|
|
1384
1445
|
touch();
|