@tagadapay/plugin-sdk 2.2.8 → 2.3.0

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.
@@ -14,6 +14,11 @@ export function useCheckout(options = {}) {
14
14
  const { checkoutToken: providedToken, autoLoadFromToken = true, } = options;
15
15
  const currentCheckoutTokenRef = useRef(null);
16
16
  const hasAutoLoadedRef = useRef(false);
17
+ const isSessionInitializedRef = useRef(isSessionInitialized);
18
+ // Keep ref in sync with state
19
+ useEffect(() => {
20
+ isSessionInitializedRef.current = isSessionInitialized;
21
+ }, [isSessionInitialized]);
17
22
  // Update debug data whenever checkout state changes with comprehensive information
18
23
  useEffect(() => {
19
24
  const debugData = checkout
@@ -52,9 +57,23 @@ export function useCheckout(options = {}) {
52
57
  console.warn('[Checkout] Cannot init - checkoutToken already provided:', providedToken.substring(0, 8) + '...');
53
58
  throw new Error('Cannot initialize new checkout when checkoutToken is already provided. The existing checkout will be auto-loaded.');
54
59
  }
55
- // Check if CMS session is initialized before making API calls
60
+ // Wait for CMS session to initialize before making API calls
61
+ // Note: If TagadaProvider has blockUntilSessionReady=true, this component won't render
62
+ // until session is ready, so this wait is mainly for the new non-blocking mode
56
63
  if (!isSessionInitialized) {
57
- throw new Error('Session not initialized. Please wait for the SDK to complete initialization before calling init().');
64
+ console.log('🔄 [useCheckout] Waiting for session initialization to complete...');
65
+ await new Promise((resolve) => {
66
+ const checkSession = () => {
67
+ if (isSessionInitializedRef.current) {
68
+ console.log('✅ [useCheckout] Session initialized, proceeding with checkout init');
69
+ resolve();
70
+ }
71
+ else {
72
+ setTimeout(checkSession, 100); // Check every 100ms
73
+ }
74
+ };
75
+ checkSession();
76
+ });
58
77
  }
59
78
  setIsLoading(true);
60
79
  setError(null);
@@ -110,9 +129,21 @@ export function useCheckout(options = {}) {
110
129
  }
111
130
  }, [apiService, currentCurrency, providedToken, storeId, isSessionInitialized]);
112
131
  const getCheckout = useCallback(async (checkoutToken) => {
113
- // Check if CMS session is initialized before making API calls
132
+ // Wait for CMS session to initialize before making API calls
114
133
  if (!isSessionInitialized) {
115
- throw new Error('Session not initialized. Please wait for the SDK to complete initialization before calling getCheckout().');
134
+ console.log('🔄 [useCheckout] Waiting for session initialization to complete...');
135
+ await new Promise((resolve) => {
136
+ const checkSession = () => {
137
+ if (isSessionInitializedRef.current) {
138
+ console.log('✅ [useCheckout] Session initialized, proceeding with getCheckout');
139
+ resolve();
140
+ }
141
+ else {
142
+ setTimeout(checkSession, 100); // Check every 100ms
143
+ }
144
+ };
145
+ checkSession();
146
+ });
116
147
  }
117
148
  setIsLoading(true);
118
149
  setError(null);
@@ -53,8 +53,9 @@ interface TagadaProviderProps {
53
53
  customApiConfig?: Partial<EnvironmentConfig>;
54
54
  debugMode?: boolean;
55
55
  localConfig?: string;
56
+ blockUntilSessionReady?: boolean;
56
57
  }
57
58
  export declare function TagadaProvider({ children, environment, customApiConfig, debugMode, // Remove default, will be set based on environment
58
- localConfig, }: TagadaProviderProps): import("react/jsx-runtime").JSX.Element;
59
+ localConfig, blockUntilSessionReady, }: TagadaProviderProps): import("react/jsx-runtime").JSX.Element;
59
60
  export declare function useTagadaContext(): TagadaContextValue;
60
61
  export {};
@@ -46,7 +46,8 @@ const InitializationLoader = () => (_jsxs("div", { style: {
46
46
  ` })] }));
47
47
  const TagadaContext = createContext(null);
48
48
  export function TagadaProvider({ children, environment, customApiConfig, debugMode, // Remove default, will be set based on environment
49
- localConfig, }) {
49
+ localConfig, blockUntilSessionReady = false, // Default to new non-blocking behavior
50
+ }) {
50
51
  // LOCAL DEV ONLY: Use localConfig override if in local development, otherwise use default
51
52
  const isLocalDev = typeof window !== 'undefined' &&
52
53
  (window.location.hostname === 'localhost' ||
@@ -83,7 +84,12 @@ localConfig, }) {
83
84
  basePath: config.basePath,
84
85
  hasConfig: !!config.config,
85
86
  });
86
- console.log('🚀 Children can now render - Phase 3 (session init) will continue in background');
87
+ if (blockUntilSessionReady) {
88
+ console.log('⏳ Blocking mode: Children will render after Phase 3 (session init) completes');
89
+ }
90
+ else {
91
+ console.log('🚀 Non-blocking mode: Children can now render - Phase 3 (session init) will continue in background');
92
+ }
87
93
  }
88
94
  catch (error) {
89
95
  console.error('❌ Failed to load plugin config in TagadaProvider:', error);
@@ -517,9 +523,11 @@ localConfig, }) {
517
523
  ]);
518
524
  // Determine if we should show loading
519
525
  // Phase 1 & 2 are mandatory: config loading and storeId availability
520
- // Phase 3 (session initialization) is now optional/non-blocking
526
+ // Phase 3 (session initialization) is optional/non-blocking by default
521
527
  const shouldShowLoading = configLoading || (!storeId && configLoading);
522
- const canRenderChildren = !configLoading && storeId; // Remove isInitialized requirement
528
+ const canRenderChildren = blockUntilSessionReady
529
+ ? (!configLoading && storeId && isInitialized) // Old behavior: wait for all phases
530
+ : (!configLoading && storeId); // New behavior: render after phases 1 & 2
523
531
  return (_jsxs(TagadaContext.Provider, { value: contextValue, children: [shouldShowLoading && _jsx(InitializationLoader, {}), finalDebugMode && canRenderChildren && (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => setIsDebugDrawerOpen(true), style: {
524
532
  position: 'fixed',
525
533
  bottom: '16px',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.2.8",
3
+ "version": "2.3.0",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",