tiwiflix-wallet-connector 1.6.3 → 1.6.4

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/index.esm.js CHANGED
@@ -7517,6 +7517,31 @@ const TWCBalanceModal = ({ isOpen, onClose, twcBalance, usdValue, colors, isDark
7517
7517
  }, children: "Learn More" })] })] }) }));
7518
7518
  };
7519
7519
 
7520
+ // Track previous connection status to detect new connections
7521
+ const prevStatusRef = useRef(null);
7522
+ // Listen for connector status changes and fetch TWC balance after WalletConnect connects
7523
+ useEffect(() => {
7524
+ const status = connector?.getState?.().status;
7525
+ const account = connector?.getState?.().account;
7526
+ const chainId = connector?.getState?.().chainId;
7527
+ // Only trigger on transition to CONNECTED
7528
+ if (status === 'connected' && prevStatusRef.current !== 'connected' && account && account.chainType === 'evm' && chainId === 56) {
7529
+ // Fetch TWC balance immediately after connection
7530
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
7531
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
7532
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
7533
+ setTwcBalance(result.balance);
7534
+ setUsdValueStable(result.usdValue);
7535
+ if (account.address) {
7536
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
7537
+ }
7538
+ setIsInitializing(false);
7539
+ console.log('[ConnectButton] TWC balance fetched after WalletConnect connect:', result);
7540
+ }
7541
+ });
7542
+ }
7543
+ prevStatusRef.current = status;
7544
+ }, [connector, fetchTWCBalanceWithRetry]);
7520
7545
  const tonApiCache = new Map();
7521
7546
  const CACHE_DURATION = 30000; // 30 seconds
7522
7547
  const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet', getExplorerUrl, }) => {
@@ -8427,42 +8452,59 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8427
8452
  console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8428
8453
  // For EVM wallets, only fetch TWC balance if on BSC
8429
8454
  if (account.chainType === 'evm' && currentChainId === 56) {
8430
- // Check cache first (30s TTL)
8431
- const cache = loadTWCBalanceFromCache(account.address);
8432
- const cacheValid = cache && typeof cache.balance === 'string' && cache.balance !== '0' && cache.balance !== '0.00' && cache.usdValue !== undefined && (() => {
8433
- const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8455
+ // Always check localStorage for cache (30s TTL)
8456
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8457
+ let cache = null;
8458
+ try {
8434
8459
  const raw = localStorage.getItem(cacheKey);
8435
- if (!raw)
8436
- return false;
8437
- try {
8460
+ if (raw) {
8438
8461
  const parsed = JSON.parse(raw);
8439
- return parsed.timestamp && (Date.now() - parsed.timestamp < 30000);
8440
- }
8441
- catch {
8442
- return false;
8462
+ if (parsed && parsed.balance && parsed.timestamp && (Date.now() - parsed.timestamp < 30000)) {
8463
+ cache = parsed;
8464
+ }
8443
8465
  }
8444
- })();
8445
- if (cacheValid) {
8466
+ }
8467
+ catch { }
8468
+ if (cache && cache.balance !== '0' && cache.balance !== '0.00') {
8446
8469
  setTwcBalance(cache.balance);
8447
8470
  setUsdValueStable(cache.usdValue);
8448
8471
  setIsInitializing(false);
8449
- console.log('[ConnectButton] Using cached TWC balance:', cache);
8472
+ console.log('[ConnectButton] Using localStorage cached TWC balance:', cache);
8473
+ // Fetch in background to update cache, but don't reset UI while fetching
8474
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8475
+ if (!isActive)
8476
+ return;
8477
+ if (result && result.balance !== '0' && result.balance !== '0.00' && result.balance !== cache?.balance) {
8478
+ setTwcBalance(result.balance);
8479
+ setUsdValueStable(result.usdValue);
8480
+ if (account.address) {
8481
+ // Save to localStorage with timestamp
8482
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8483
+ }
8484
+ setIsInitializing(false);
8485
+ console.log('[ConnectButton] TWC balance updated after background fetch:', result);
8486
+ }
8487
+ }).catch(() => {
8488
+ // Ignore errors in background fetch
8489
+ });
8450
8490
  }
8451
8491
  else {
8492
+ // No valid cache, show loading state until fetch completes
8493
+ setIsInitializing(true);
8452
8494
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8453
8495
  if (!isActive)
8454
8496
  return;
8455
- if (result) {
8497
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8456
8498
  setTwcBalance(result.balance);
8457
8499
  setUsdValueStable(result.usdValue);
8458
8500
  if (account.address) {
8459
- saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
8501
+ // Save to localStorage with timestamp
8502
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8460
8503
  }
8461
8504
  setIsInitializing(false);
8462
- console.log('[ConnectButton] TWC balance fetched:', result);
8505
+ console.log('[ConnectButton] TWC balance fetched and cached:', result);
8463
8506
  }
8464
8507
  else {
8465
- // Final fallback
8466
8508
  setTwcBalance('0');
8467
8509
  setUsdValueStable(null);
8468
8510
  setIsInitializing(false);
@@ -8490,7 +8532,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8490
8532
  setTwcBalance(state.twcBalance);
8491
8533
  setUsdValueStable(state.usdValue ?? null);
8492
8534
  if (account.address) {
8493
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8535
+ // Save to localStorage for consistency
8536
+ localStorage.setItem(`tiwiflix_twc_balance_cache_${account.address}`, JSON.stringify({ balance: state.twcBalance, usdValue: state.usdValue ?? null, timestamp: Date.now() }));
8494
8537
  }
8495
8538
  setIsInitializing(false);
8496
8539
  }
package/dist/index.js CHANGED
@@ -7519,6 +7519,31 @@ const TWCBalanceModal = ({ isOpen, onClose, twcBalance, usdValue, colors, isDark
7519
7519
  }, children: "Learn More" })] })] }) }));
7520
7520
  };
7521
7521
 
7522
+ // Track previous connection status to detect new connections
7523
+ const prevStatusRef = React.useRef(null);
7524
+ // Listen for connector status changes and fetch TWC balance after WalletConnect connects
7525
+ React.useEffect(() => {
7526
+ const status = connector?.getState?.().status;
7527
+ const account = connector?.getState?.().account;
7528
+ const chainId = connector?.getState?.().chainId;
7529
+ // Only trigger on transition to CONNECTED
7530
+ if (status === 'connected' && prevStatusRef.current !== 'connected' && account && account.chainType === 'evm' && chainId === 56) {
7531
+ // Fetch TWC balance immediately after connection
7532
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
7533
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
7534
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
7535
+ setTwcBalance(result.balance);
7536
+ setUsdValueStable(result.usdValue);
7537
+ if (account.address) {
7538
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
7539
+ }
7540
+ setIsInitializing(false);
7541
+ console.log('[ConnectButton] TWC balance fetched after WalletConnect connect:', result);
7542
+ }
7543
+ });
7544
+ }
7545
+ prevStatusRef.current = status;
7546
+ }, [connector, fetchTWCBalanceWithRetry]);
7522
7547
  const tonApiCache = new Map();
7523
7548
  const CACHE_DURATION = 30000; // 30 seconds
7524
7549
  const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet', getExplorerUrl, }) => {
@@ -8429,42 +8454,59 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8429
8454
  console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8430
8455
  // For EVM wallets, only fetch TWC balance if on BSC
8431
8456
  if (account.chainType === 'evm' && currentChainId === 56) {
8432
- // Check cache first (30s TTL)
8433
- const cache = loadTWCBalanceFromCache(account.address);
8434
- const cacheValid = cache && typeof cache.balance === 'string' && cache.balance !== '0' && cache.balance !== '0.00' && cache.usdValue !== undefined && (() => {
8435
- const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8457
+ // Always check localStorage for cache (30s TTL)
8458
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8459
+ let cache = null;
8460
+ try {
8436
8461
  const raw = localStorage.getItem(cacheKey);
8437
- if (!raw)
8438
- return false;
8439
- try {
8462
+ if (raw) {
8440
8463
  const parsed = JSON.parse(raw);
8441
- return parsed.timestamp && (Date.now() - parsed.timestamp < 30000);
8442
- }
8443
- catch {
8444
- return false;
8464
+ if (parsed && parsed.balance && parsed.timestamp && (Date.now() - parsed.timestamp < 30000)) {
8465
+ cache = parsed;
8466
+ }
8445
8467
  }
8446
- })();
8447
- if (cacheValid) {
8468
+ }
8469
+ catch { }
8470
+ if (cache && cache.balance !== '0' && cache.balance !== '0.00') {
8448
8471
  setTwcBalance(cache.balance);
8449
8472
  setUsdValueStable(cache.usdValue);
8450
8473
  setIsInitializing(false);
8451
- console.log('[ConnectButton] Using cached TWC balance:', cache);
8474
+ console.log('[ConnectButton] Using localStorage cached TWC balance:', cache);
8475
+ // Fetch in background to update cache, but don't reset UI while fetching
8476
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8477
+ if (!isActive)
8478
+ return;
8479
+ if (result && result.balance !== '0' && result.balance !== '0.00' && result.balance !== cache?.balance) {
8480
+ setTwcBalance(result.balance);
8481
+ setUsdValueStable(result.usdValue);
8482
+ if (account.address) {
8483
+ // Save to localStorage with timestamp
8484
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8485
+ }
8486
+ setIsInitializing(false);
8487
+ console.log('[ConnectButton] TWC balance updated after background fetch:', result);
8488
+ }
8489
+ }).catch(() => {
8490
+ // Ignore errors in background fetch
8491
+ });
8452
8492
  }
8453
8493
  else {
8494
+ // No valid cache, show loading state until fetch completes
8495
+ setIsInitializing(true);
8454
8496
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8455
8497
  if (!isActive)
8456
8498
  return;
8457
- if (result) {
8499
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8458
8500
  setTwcBalance(result.balance);
8459
8501
  setUsdValueStable(result.usdValue);
8460
8502
  if (account.address) {
8461
- saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
8503
+ // Save to localStorage with timestamp
8504
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8462
8505
  }
8463
8506
  setIsInitializing(false);
8464
- console.log('[ConnectButton] TWC balance fetched:', result);
8507
+ console.log('[ConnectButton] TWC balance fetched and cached:', result);
8465
8508
  }
8466
8509
  else {
8467
- // Final fallback
8468
8510
  setTwcBalance('0');
8469
8511
  setUsdValueStable(null);
8470
8512
  setIsInitializing(false);
@@ -8492,7 +8534,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8492
8534
  setTwcBalance(state.twcBalance);
8493
8535
  setUsdValueStable(state.usdValue ?? null);
8494
8536
  if (account.address) {
8495
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8537
+ // Save to localStorage for consistency
8538
+ localStorage.setItem(`tiwiflix_twc_balance_cache_${account.address}`, JSON.stringify({ balance: state.twcBalance, usdValue: state.usdValue ?? null, timestamp: Date.now() }));
8496
8539
  }
8497
8540
  setIsInitializing(false);
8498
8541
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiwiflix-wallet-connector",
3
- "version": "1.6.3",
3
+ "version": "1.6.4",
4
4
  "description": "Multi-chain wallet connector for Tiwiflix supporting EVM, TON, Solana, and Tron wallets",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",