@tagadapay/plugin-sdk 2.7.39 → 2.7.41
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/v2/index.d.ts
CHANGED
|
@@ -32,3 +32,4 @@ export type { UseCustomerResult } from './react/hooks/useCustomer';
|
|
|
32
32
|
export type { UseCustomerInfosOptions, UseCustomerInfosResult } from './react/hooks/useCustomerInfos';
|
|
33
33
|
export type { UseCustomerOrdersOptions, UseCustomerOrdersResult } from './react/hooks/useCustomerOrders';
|
|
34
34
|
export type { UseCustomerSubscriptionsOptions, UseCustomerSubscriptionsResult } from './react/hooks/useCustomerSubscriptions';
|
|
35
|
+
export type { UseShippingRatesQueryOptions, UseShippingRatesQueryResult } from './react/hooks/useShippingRatesQuery';
|
|
@@ -16,10 +16,12 @@ export function useCheckoutToken(options = {}) {
|
|
|
16
16
|
const hasAutoLoadedRef = useRef(false);
|
|
17
17
|
const hasAutoLoadedFromCacheRef = useRef(false);
|
|
18
18
|
const isSessionInitializedRef = useRef(isSessionInitialized);
|
|
19
|
-
|
|
19
|
+
const checkoutTokenRef = useRef(checkoutToken);
|
|
20
|
+
// Keep refs in sync with state
|
|
20
21
|
useEffect(() => {
|
|
21
22
|
isSessionInitializedRef.current = isSessionInitialized;
|
|
22
|
-
|
|
23
|
+
checkoutTokenRef.current = checkoutToken;
|
|
24
|
+
}, [isSessionInitialized, checkoutToken]);
|
|
23
25
|
// Update token when provided token changes
|
|
24
26
|
useEffect(() => {
|
|
25
27
|
if (providedToken && providedToken !== checkoutToken) {
|
|
@@ -29,33 +31,68 @@ export function useCheckoutToken(options = {}) {
|
|
|
29
31
|
hasAutoLoadedFromCacheRef.current = false;
|
|
30
32
|
}
|
|
31
33
|
}, [providedToken, checkoutToken]);
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const cachedToken = queryKey[1];
|
|
45
|
-
if (cachedToken) {
|
|
46
|
-
setCheckoutToken(cachedToken);
|
|
47
|
-
hasAutoLoadedFromCacheRef.current = true;
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
34
|
+
// Helper function to extract token from checkout queries in cache
|
|
35
|
+
const getTokenFromCache = useCallback(() => {
|
|
36
|
+
try {
|
|
37
|
+
const queryCache = queryClient.getQueryCache();
|
|
38
|
+
const checkoutQueries = queryCache.findAll({ queryKey: ['checkout'] });
|
|
39
|
+
// Find the first query with a valid token in the query key
|
|
40
|
+
for (const query of checkoutQueries) {
|
|
41
|
+
const queryKey = query.queryKey;
|
|
42
|
+
if (Array.isArray(queryKey) && queryKey.length >= 2 && typeof queryKey[1] === 'string' && queryKey[1]) {
|
|
43
|
+
const cachedToken = queryKey[1];
|
|
44
|
+
if (cachedToken) {
|
|
45
|
+
return cachedToken;
|
|
50
46
|
}
|
|
51
47
|
}
|
|
52
48
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
// Silently fail if query cache access fails
|
|
52
|
+
console.debug('Failed to load checkoutToken from query cache:', err);
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}, [queryClient]);
|
|
56
|
+
// Auto-load token from React Query cache if available (from useCheckoutQuery)
|
|
57
|
+
// This runs initially and also reactively when cache changes
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!providedToken && autoLoadFromQueryCache) {
|
|
60
|
+
const cachedToken = getTokenFromCache();
|
|
61
|
+
// Update token if we found one in cache and don't have one yet
|
|
62
|
+
if (cachedToken && cachedToken !== checkoutToken) {
|
|
63
|
+
setCheckoutToken(cachedToken);
|
|
64
|
+
hasAutoLoadedFromCacheRef.current = true;
|
|
56
65
|
}
|
|
57
66
|
}
|
|
58
|
-
}, [providedToken, autoLoadFromQueryCache, checkoutToken,
|
|
67
|
+
}, [providedToken, autoLoadFromQueryCache, checkoutToken, getTokenFromCache]);
|
|
68
|
+
// Subscribe to query cache changes to reactively pick up new checkout tokens
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!providedToken && autoLoadFromQueryCache) {
|
|
71
|
+
const queryCache = queryClient.getQueryCache();
|
|
72
|
+
const unsubscribe = queryCache.subscribe((event) => {
|
|
73
|
+
// When a query is added or updated, check if it's a checkout query
|
|
74
|
+
if (event?.type === 'added' || event?.type === 'updated') {
|
|
75
|
+
const query = event.query;
|
|
76
|
+
const queryKey = query?.queryKey;
|
|
77
|
+
// Check if this is a checkout query with a token
|
|
78
|
+
// Use ref to get latest token value without causing re-subscriptions
|
|
79
|
+
if (Array.isArray(queryKey) &&
|
|
80
|
+
queryKey[0] === 'checkout' &&
|
|
81
|
+
queryKey.length >= 2 &&
|
|
82
|
+
typeof queryKey[1] === 'string' &&
|
|
83
|
+
queryKey[1] &&
|
|
84
|
+
queryKey[1] !== checkoutTokenRef.current) {
|
|
85
|
+
const newToken = queryKey[1];
|
|
86
|
+
setCheckoutToken(newToken);
|
|
87
|
+
hasAutoLoadedFromCacheRef.current = true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
return () => {
|
|
92
|
+
unsubscribe();
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}, [providedToken, autoLoadFromQueryCache, queryClient]);
|
|
59
96
|
// Auto-load token from URL if no token provided
|
|
60
97
|
useEffect(() => {
|
|
61
98
|
if (!providedToken && autoLoadFromToken && !checkoutToken && !hasAutoLoadedRef.current) {
|