@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.
@@ -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
- // Keep ref in sync with state
19
+ const checkoutTokenRef = useRef(checkoutToken);
20
+ // Keep refs in sync with state
20
21
  useEffect(() => {
21
22
  isSessionInitializedRef.current = isSessionInitialized;
22
- }, [isSessionInitialized]);
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
- // 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
- }
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
- catch (err) {
54
- // Silently fail if query cache access fails
55
- console.debug('Failed to load checkoutToken from query cache:', err);
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, queryClient]);
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.7.39",
3
+ "version": "2.7.41",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",