@tagadapay/plugin-sdk 2.7.38 → 2.7.39
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
1
2
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
3
|
import { useTagadaContext } from '../providers/TagadaProvider';
|
|
3
4
|
/**
|
|
@@ -6,12 +7,14 @@ import { useTagadaContext } from '../providers/TagadaProvider';
|
|
|
6
7
|
*/
|
|
7
8
|
export function useCheckoutToken(options = {}) {
|
|
8
9
|
const { isSessionInitialized } = useTagadaContext();
|
|
9
|
-
const { checkoutToken: providedToken, autoLoadFromToken = true } = options;
|
|
10
|
+
const { checkoutToken: providedToken, autoLoadFromToken = true, autoLoadFromQueryCache = true } = options;
|
|
11
|
+
const queryClient = useQueryClient();
|
|
10
12
|
const [checkoutToken, setCheckoutToken] = useState(providedToken || null);
|
|
11
13
|
const [isLoading, _setIsLoading] = useState(false);
|
|
12
14
|
const [error, setError] = useState(null);
|
|
13
15
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
14
16
|
const hasAutoLoadedRef = useRef(false);
|
|
17
|
+
const hasAutoLoadedFromCacheRef = useRef(false);
|
|
15
18
|
const isSessionInitializedRef = useRef(isSessionInitialized);
|
|
16
19
|
// Keep ref in sync with state
|
|
17
20
|
useEffect(() => {
|
|
@@ -23,8 +26,36 @@ export function useCheckoutToken(options = {}) {
|
|
|
23
26
|
setCheckoutToken(providedToken);
|
|
24
27
|
setIsInitialized(false);
|
|
25
28
|
hasAutoLoadedRef.current = false;
|
|
29
|
+
hasAutoLoadedFromCacheRef.current = false;
|
|
26
30
|
}
|
|
27
31
|
}, [providedToken, checkoutToken]);
|
|
32
|
+
// Auto-load token from React Query cache if available (from useCheckoutQuery)
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (!providedToken && autoLoadFromQueryCache && !checkoutToken && !hasAutoLoadedFromCacheRef.current) {
|
|
35
|
+
try {
|
|
36
|
+
// Check React Query cache for checkout queries
|
|
37
|
+
// Query key format: ['checkout', checkoutToken, currency.code]
|
|
38
|
+
const queryCache = queryClient.getQueryCache();
|
|
39
|
+
const checkoutQueries = queryCache.findAll({ queryKey: ['checkout'] });
|
|
40
|
+
// Find the first query with a valid token in the query key
|
|
41
|
+
for (const query of checkoutQueries) {
|
|
42
|
+
const queryKey = query.queryKey;
|
|
43
|
+
if (Array.isArray(queryKey) && queryKey.length >= 2 && typeof queryKey[1] === 'string' && queryKey[1]) {
|
|
44
|
+
const cachedToken = queryKey[1];
|
|
45
|
+
if (cachedToken) {
|
|
46
|
+
setCheckoutToken(cachedToken);
|
|
47
|
+
hasAutoLoadedFromCacheRef.current = true;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
// Silently fail if query cache access fails
|
|
55
|
+
console.debug('Failed to load checkoutToken from query cache:', err);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}, [providedToken, autoLoadFromQueryCache, checkoutToken, queryClient]);
|
|
28
59
|
// Auto-load token from URL if no token provided
|
|
29
60
|
useEffect(() => {
|
|
30
61
|
if (!providedToken && autoLoadFromToken && !checkoutToken && !hasAutoLoadedRef.current) {
|
|
@@ -53,6 +84,7 @@ export function useCheckoutToken(options = {}) {
|
|
|
53
84
|
setIsInitialized(false);
|
|
54
85
|
setError(null);
|
|
55
86
|
hasAutoLoadedRef.current = false;
|
|
87
|
+
hasAutoLoadedFromCacheRef.current = false;
|
|
56
88
|
}, []);
|
|
57
89
|
return {
|
|
58
90
|
checkoutToken,
|