@tagadapay/plugin-sdk 2.5.2 โ†’ 2.6.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.
@@ -11,6 +11,8 @@ export interface UsePluginConfigResult<TConfig = Record<string, any>> {
11
11
  storeId?: string;
12
12
  accountId?: string;
13
13
  basePath?: string;
14
+ loading: boolean;
15
+ error?: Error;
14
16
  isValid: boolean;
15
17
  }
16
18
  export declare function usePluginConfig<TConfig = Record<string, any>>(options?: UsePluginConfigOptions): UsePluginConfigResult<TConfig>;
@@ -6,7 +6,7 @@ import { useMemo } from 'react';
6
6
  import { useTagadaContext } from '../providers/TagadaProvider';
7
7
  import { PluginConfigUtils } from '../../core/utils/pluginConfig';
8
8
  export function usePluginConfig(options = {}) {
9
- const { pluginConfig } = useTagadaContext();
9
+ const { pluginConfig, pluginConfigLoading } = useTagadaContext();
10
10
  const config = useMemo(() => {
11
11
  const baseConfig = PluginConfigUtils.getPluginConfig(options.rawConfig, {
12
12
  storeId: pluginConfig.storeId,
@@ -30,6 +30,8 @@ export function usePluginConfig(options = {}) {
30
30
  storeId: config.storeId,
31
31
  accountId: config.accountId,
32
32
  basePath: config.basePath,
33
+ loading: pluginConfigLoading,
34
+ error: undefined, // TODO: Add error handling if needed
33
35
  isValid,
34
36
  };
35
37
  }
@@ -48,9 +48,39 @@ const InitializationLoader = () => (_jsxs("div", { style: {
48
48
  }
49
49
  ` })] }));
50
50
  const TagadaContext = createContext(null);
51
+ // Global instance tracking for TagadaProvider
52
+ let globalTagadaInstance = null;
53
+ let globalTagadaInitialized = false;
51
54
  export function TagadaProvider({ children, environment, customApiConfig, debugMode, // Remove default, will be set based on environment
52
55
  localConfig, blockUntilSessionReady = false, // Default to new non-blocking behavior
53
56
  rawPluginConfig, }) {
57
+ // Instance tracking
58
+ const [instanceId] = useState(() => {
59
+ if (!globalTagadaInstance) {
60
+ globalTagadaInstance = Math.random().toString(36).substr(2, 9);
61
+ }
62
+ return globalTagadaInstance;
63
+ });
64
+ const isActiveInstance = useMemo(() => {
65
+ if (!globalTagadaInitialized) {
66
+ globalTagadaInitialized = true;
67
+ console.log(`โœ… [TagadaProvider] Instance ${instanceId} is now the active instance`);
68
+ return true;
69
+ }
70
+ else {
71
+ console.log(`๐Ÿšซ [TagadaProvider] Instance ${instanceId} is duplicate - blocking execution`);
72
+ return false;
73
+ }
74
+ }, [instanceId]);
75
+ useEffect(() => {
76
+ return () => {
77
+ if (globalTagadaInstance === instanceId) {
78
+ globalTagadaInitialized = false;
79
+ globalTagadaInstance = null;
80
+ console.log(`๐Ÿงน [TagadaProvider] Instance ${instanceId} cleanup - allowing new instances`);
81
+ }
82
+ };
83
+ }, [instanceId]);
54
84
  // LOCAL DEV ONLY: Use localConfig override if in local development, otherwise use default
55
85
  const isLocalDev = typeof window !== 'undefined' &&
56
86
  (window.location.hostname === 'localhost' ||
@@ -60,7 +90,7 @@ rawPluginConfig, }) {
60
90
  // Debug logging (only log once during initial render)
61
91
  const hasLoggedRef = useRef(false);
62
92
  if (!hasLoggedRef.current) {
63
- console.log('๐Ÿ” TagadaProvider Config Debug:', {
93
+ console.log(`๐Ÿ” [TagadaProvider] Instance ${instanceId} Config Debug:`, {
64
94
  hostname: typeof window !== 'undefined' ? window.location.hostname : 'SSR',
65
95
  isLocalDev,
66
96
  localConfig,
@@ -84,6 +114,11 @@ rawPluginConfig, }) {
84
114
  const [configLoading, setConfigLoading] = useState(!rawPluginConfig);
85
115
  // Load plugin config on mount with the specified variant
86
116
  useEffect(() => {
117
+ // Prevent multiple config loads
118
+ if (configLoading === false && pluginConfig.storeId) {
119
+ console.log('๐Ÿ”’ [TagadaProvider] Config already loaded, skipping reload');
120
+ return;
121
+ }
87
122
  const loadConfig = async () => {
88
123
  try {
89
124
  // Use the v2 core loadPluginConfig function
@@ -239,12 +274,21 @@ rawPluginConfig, }) {
239
274
  });
240
275
  // Initialize session
241
276
  const initializeSession = useCallback(async (sessionData) => {
277
+ if (!isActiveInstance) {
278
+ console.log(`๐Ÿšซ [TagadaProvider] Instance ${instanceId} is not active, skipping session initialization`);
279
+ return;
280
+ }
242
281
  if (!sessionData.storeId || !sessionData.accountId) {
243
- console.error('[SDK] Missing required session data');
282
+ console.error(`[TagadaProvider] Instance ${instanceId} missing required session data`);
283
+ return;
284
+ }
285
+ // Prevent multiple session initializations
286
+ if (isSessionInitialized) {
287
+ console.log(`๐Ÿ”’ [TagadaProvider] Instance ${instanceId} session already initialized, skipping`);
244
288
  return;
245
289
  }
246
290
  if (finalDebugMode) {
247
- console.debug('[SDK][DEBUG] Initializing session with store config...', sessionData);
291
+ console.debug(`[TagadaProvider] Instance ${instanceId} [DEBUG] Initializing session with store config...`, sessionData);
248
292
  }
249
293
  setIsLoading(true);
250
294
  try {
@@ -352,15 +396,23 @@ rawPluginConfig, }) {
352
396
  setIsInitialized(true);
353
397
  setIsLoading(false);
354
398
  }
355
- }, [apiService, finalDebugMode]);
399
+ }, [apiService, finalDebugMode, isActiveInstance, instanceId]);
356
400
  // Create anonymous token if needed
357
401
  const createAnonymousToken = useCallback(async (targetStoreId) => {
402
+ if (!isActiveInstance) {
403
+ console.log(`๐Ÿšซ [TagadaProvider] Instance ${instanceId} is not active, skipping anonymous token creation`);
404
+ return;
405
+ }
358
406
  if (hasAttemptedAnonymousToken || !targetStoreId) {
407
+ console.log(`๐Ÿ”’ [TagadaProvider] Instance ${instanceId} anonymous token already attempted or no storeId:`, {
408
+ hasAttemptedAnonymousToken,
409
+ targetStoreId,
410
+ });
359
411
  return;
360
412
  }
361
- console.log('[SDK] ๐Ÿš€ Starting Phase 3 - Session initialization...');
413
+ console.log(`[TagadaProvider] Instance ${instanceId} ๐Ÿš€ Starting Phase 3 - Session initialization...`);
362
414
  if (finalDebugMode) {
363
- console.debug('[SDK][DEBUG] Creating anonymous token for store:', targetStoreId);
415
+ console.debug(`[TagadaProvider] Instance ${instanceId} [DEBUG] Creating anonymous token for store:`, targetStoreId);
364
416
  }
365
417
  setHasAttemptedAnonymousToken(true);
366
418
  try {
@@ -405,15 +457,24 @@ rawPluginConfig, }) {
405
457
  setIsInitialized(true);
406
458
  setIsLoading(false);
407
459
  }
408
- }, [apiService, hasAttemptedAnonymousToken, initializeSession, finalDebugMode]);
460
+ }, [apiService, hasAttemptedAnonymousToken, initializeSession, finalDebugMode, isActiveInstance, instanceId]);
409
461
  // Initialize token from storage or create anonymous token
410
462
  // This runs in the background after phases 1 & 2 complete, but doesn't block rendering
411
463
  useEffect(() => {
464
+ if (!isActiveInstance) {
465
+ console.log(`๐Ÿšซ [TagadaProvider] Instance ${instanceId} is not active, skipping token initialization`);
466
+ return;
467
+ }
412
468
  if (isInitializing.current) {
469
+ console.log(`๐Ÿ”’ [TagadaProvider] Instance ${instanceId} already initializing, skipping`);
413
470
  return;
414
471
  }
415
472
  // Wait for plugin config to load AND ensure we have a store ID before initializing
416
473
  if (configLoading || !storeId) {
474
+ console.log(`โณ [TagadaProvider] Instance ${instanceId} waiting for config or storeId:`, {
475
+ configLoading,
476
+ storeId,
477
+ });
417
478
  return;
418
479
  }
419
480
  isInitializing.current = true;
@@ -469,7 +530,7 @@ rawPluginConfig, }) {
469
530
  }
470
531
  };
471
532
  void initializeToken();
472
- }, [storeId, createAnonymousToken, initializeSession, configLoading]);
533
+ }, [storeId, createAnonymousToken, initializeSession, configLoading, isActiveInstance, instanceId]);
473
534
  // Update auth state when customer/session changes
474
535
  useEffect(() => {
475
536
  setAuth({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.5.2",
3
+ "version": "2.6.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",