tiwiflix-wallet-connector 1.5.8 → 1.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.
package/dist/index.esm.js CHANGED
@@ -7516,20 +7516,6 @@ const TWCBalanceModal = ({ isOpen, onClose, twcBalance, usdValue, colors, isDark
7516
7516
 
7517
7517
  const tonApiCache = new Map();
7518
7518
  const CACHE_DURATION = 30000; // 30 seconds
7519
- const defaultStyles = {
7520
- button: {
7521
- padding: '10px 20px',
7522
- borderRadius: '14px',
7523
- border: 'none',
7524
- backgroundColor: '#3396FF', // This will be overridden by theme colors in actual usage
7525
- color: 'white',
7526
- fontWeight: '500',
7527
- fontSize: '14px',
7528
- cursor: 'pointer',
7529
- transition: 'all 0.15s ease',
7530
- fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
7531
- boxShadow: '0 4px 14px 0 rgba(51, 150, 255, 0.39)',
7532
- }};
7533
7519
  const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet Now', getExplorerUrl, }) => {
7534
7520
  // Always fetch balance automatically when wallet is connected
7535
7521
  // Balance is always fetched automatically from the chain
@@ -7705,7 +7691,6 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7705
7691
  React.useRef(null);
7706
7692
  // Get theme colors
7707
7693
  const colors = getThemeColors(isDarkMode);
7708
- console.log('above loadbalance');
7709
7694
  // Load balance with caching
7710
7695
  const loadBalance = useCallback(async () => {
7711
7696
  if (!account) {
@@ -7825,7 +7810,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7825
7810
  }, 4000); // force-resolve after 4s
7826
7811
  return () => clearTimeout(timeout);
7827
7812
  }, [isInitializing, balanceLoading, isConnecting]);
7828
- // Check chain ID and validate network
7813
+ // Remove network restriction: allow all EVM networks to connect
7829
7814
  useEffect(() => {
7830
7815
  const checkChain = async () => {
7831
7816
  if (!account || account.chainType !== 'evm') {
@@ -7836,16 +7821,13 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7836
7821
  try {
7837
7822
  const chainId = await connector.getCurrentChain();
7838
7823
  setCurrentChainId(chainId);
7839
- // BSC Mainnet = 56, BSC Testnet = 97
7840
- const isBSC = chainId === 56 || chainId === 97;
7841
- setIsWrongNetwork(!isBSC && chainId !== null);
7824
+ // No restriction: allow all EVM networks
7825
+ setIsWrongNetwork(false);
7842
7826
  }
7843
7827
  catch (err) {
7844
7828
  }
7845
7829
  };
7846
7830
  checkChain();
7847
- // Also periodically check if still connected to EVM wallet
7848
- // This handles cases where chain is changed from wallet UI
7849
7831
  if (account && account.chainType === 'evm') {
7850
7832
  const interval = setInterval(checkChain, 2000);
7851
7833
  return () => clearInterval(interval);
@@ -7906,7 +7888,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7906
7888
  }
7907
7889
  let initTimeout;
7908
7890
  // If already connected, get account immediately and load balance
7909
- if (initialState.status === ConnectionStatus.CONNECTED) {
7891
+ if (initialState.status === ConnectionStatus.CONNECTED && initialState.account) {
7910
7892
  // Set isInitializing to true initially, will be set to false once balance loads
7911
7893
  setIsInitializing(true);
7912
7894
  // Safety timeout - ensure we NEVER stay in loading state for more than 2 seconds
@@ -7917,9 +7899,22 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7917
7899
  connector.getAccount().then(async (acc) => {
7918
7900
  if (!isMounted)
7919
7901
  return;
7920
- setAccount(acc);
7902
+ // Only set account to null if status is truly disconnected
7903
+ if (!acc && connector.getState().status === ConnectionStatus.DISCONNECTED) {
7904
+ setAccount(null);
7905
+ }
7906
+ else if (acc) {
7907
+ setAccount(acc);
7908
+ }
7909
+ // Only proceed if account is present
7910
+ if (!acc) {
7911
+ clearTimeout(safetyTimeout);
7912
+ setIsInitializing(false);
7913
+ setBalanceLoading(false);
7914
+ return;
7915
+ }
7921
7916
  // Load balance when account is available
7922
- if (acc && acc.chainType === 'ton') {
7917
+ if (acc.chainType === 'ton') {
7923
7918
  // For TON wallets, fetch balance and NFTs
7924
7919
  setTwcBalance('0');
7925
7920
  try {
@@ -7943,7 +7938,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7943
7938
  // Check if we have real TWC balance (not 0)
7944
7939
  const state = connector.getState();
7945
7940
  const stateTwcBalance = state.twcBalance;
7946
- if (acc && stateTwcBalance && stateTwcBalance !== '0' && stateTwcBalance !== '0.00') {
7941
+ if (stateTwcBalance && stateTwcBalance !== '0' && stateTwcBalance !== '0.00') {
7947
7942
  if (!isMounted)
7948
7943
  return;
7949
7944
  setTwcBalance(stateTwcBalance);
@@ -7955,7 +7950,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7955
7950
  clearTimeout(safetyTimeout);
7956
7951
  setIsInitializing(false);
7957
7952
  }
7958
- else if (acc) {
7953
+ else {
7959
7954
  // Try to load from cache as fallback
7960
7955
  const cached = loadTWCBalanceFromCache(acc.address);
7961
7956
  if (cached?.balance && cached.balance !== '0' && cached.balance !== '0.00') {
@@ -8047,9 +8042,11 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8047
8042
  return;
8048
8043
  clearTimeout(safetyTimeout);
8049
8044
  setIsInitializing(false);
8050
- // If we can't get account even though status is connected, disconnect
8051
- setAccount(null);
8052
- connector.disconnect().catch(() => { });
8045
+ setBalanceLoading(false);
8046
+ // Only set account to null if status is truly disconnected
8047
+ if (connector.getState().status === ConnectionStatus.DISCONNECTED) {
8048
+ setAccount(null);
8049
+ }
8053
8050
  });
8054
8051
  }
8055
8052
  else {
@@ -8381,7 +8378,13 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8381
8378
  }
8382
8379
  }, [account]);
8383
8380
  const fetchTWCBalanceWithRetry = useCallback(async (maxRetries = 3, initialDelay = 200) => {
8381
+ // Only fetch TWC balance if wallet is on BSC (chainId 56)
8384
8382
  try {
8383
+ const state = connector.getState();
8384
+ if (state.account?.chainType !== 'evm' || state.chainId !== 56) {
8385
+ // Not on BSC, do not fetch TWC balance
8386
+ return null;
8387
+ }
8385
8388
  const twcBalance = await connector.getTWCBalance();
8386
8389
  return {
8387
8390
  balance: twcBalance.amount || '0',
@@ -8403,13 +8406,14 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8403
8406
  }
8404
8407
  return null;
8405
8408
  }, [connector]);
8406
- // Load balance when account changes
8409
+ // Load balance when account or network (chain) changes
8407
8410
  useEffect(() => {
8408
8411
  let isActive = true;
8409
- console.log('[ConnectButton] useEffect: account changed', { account });
8412
+ // Only log and run effect if account is not null
8410
8413
  if (account) {
8411
- // For EVM wallets, try to get TWC balance
8412
- if (account.chainType === 'evm') {
8414
+ console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8415
+ // For EVM wallets, only fetch TWC balance if on BSC
8416
+ if (account.chainType === 'evm' && currentChainId === 56) {
8413
8417
  // First check state/cache for immediate display
8414
8418
  const state = connector.getState();
8415
8419
  let hasBalance = false;
@@ -8432,9 +8436,9 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8432
8436
  hasBalance = true;
8433
8437
  }
8434
8438
  }
8435
- // Only fetch if we don't have a balance yet
8436
- if (!hasBalance) {
8437
- console.log('[ConnectButton] Fetching TWC balance for EVM wallet:', account.address);
8439
+ // Only fetch if we don't have a balance yet, or if the chain/network changed
8440
+ if (!hasBalance || currentChainId !== null) {
8441
+ console.log('[ConnectButton] Fetching TWC balance for BSC wallet:', account.address, currentChainId);
8438
8442
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8439
8443
  if (!isActive)
8440
8444
  return;
@@ -8452,16 +8456,23 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8452
8456
  setTwcBalance('0');
8453
8457
  setUsdValueStable(null);
8454
8458
  setIsInitializing(false);
8455
- console.warn('[ConnectButton] No TWC balance found for EVM wallet:', account.address);
8459
+ console.warn('[ConnectButton] No TWC balance found for BSC wallet:', account.address);
8456
8460
  }
8457
8461
  }).catch(() => {
8458
8462
  if (!isActive)
8459
8463
  return;
8460
8464
  setIsInitializing(false);
8461
- console.error('[ConnectButton] Error fetching TWC balance for EVM wallet:', account.address);
8465
+ console.error('[ConnectButton] Error fetching TWC balance for BSC wallet:', account.address);
8462
8466
  });
8463
8467
  }
8464
8468
  }
8469
+ else if (account.chainType === 'evm' && currentChainId !== 56) {
8470
+ // EVM wallet but not on BSC, do not fetch TWC balance, do not show switch chain
8471
+ setTwcBalance(null);
8472
+ setUsdValueStable(null);
8473
+ setIsInitializing(false);
8474
+ // Do not show error or prompt
8475
+ }
8465
8476
  else {
8466
8477
  // Non-EVM wallet, just check state
8467
8478
  const state = connector.getState();
@@ -8482,7 +8493,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8482
8493
  isActive = false;
8483
8494
  };
8484
8495
  // eslint-disable-next-line react-hooks/exhaustive-deps
8485
- }, [account?.address]);
8496
+ }, [account?.address, currentChainId]);
8486
8497
  // Polling mechanism as fallback to ensure TWC balance is always fetched
8487
8498
  useEffect(() => {
8488
8499
  if (!account || account.chainType !== 'evm') {
@@ -8649,7 +8660,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8649
8660
  alert('Address copied to clipboard!');
8650
8661
  }
8651
8662
  }, [account]);
8652
- const handleSwitchToBSC = useCallback(async () => {
8663
+ useCallback(async () => {
8653
8664
  try {
8654
8665
  // Try to switch to BSC Mainnet (56)
8655
8666
  await connector.switchChain(56);
@@ -8674,85 +8685,47 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8674
8685
  }, [connector, account]);
8675
8686
  // Render button based on connection status
8676
8687
  const renderButton = () => {
8677
- // Check if we're waiting for connection OR waiting for balance to load
8678
- // Show loading if: initializing, connecting, OR (account exists but balance hasn't loaded/resolved yet)
8679
- // For TON: treat as resolved after fetch completes (even if 0). For others: treat zero/empty as resolved.
8680
- (balance.amount && balance.amount !== '0' && balance.amount !== '0.00' && balance.amount !== '' && balance.amount !== 'Loading...') ||
8681
- (twcBalance && twcBalance !== '0' && twcBalance !== '0.00' && twcBalance !== '' && twcBalance !== 'Loading...');
8682
- // Treat balance loading as background; only gate on initial connection.
8683
- const isCheckingConnection = isInitializing || ((status === ConnectionStatus.CONNECTING || isConnecting) && !account);
8684
- const isActuallyConnecting = (status === ConnectionStatus.CONNECTING || isConnecting) && !!account && !isInitializing;
8685
- // Check connection state
8686
- if (isCheckingConnection) {
8687
- // Show loading connected button structure while checking connection
8688
- const isMobile = isMobileViewport();
8689
- // Loading spinner component
8690
- const LoadingSpinner = () => (jsxs(Fragment, { children: [jsx("div", { style: {
8691
- width: isMobile ? '12px' : '14px',
8692
- height: isMobile ? '12px' : '14px',
8693
- border: '2px solid rgba(255, 255, 255, 0.3)',
8694
- borderTopColor: '#FFFFFF',
8695
- borderRadius: '50%',
8696
- animation: 'spin 0.8s linear infinite',
8697
- } }), jsx("style", { children: `
8698
- @keyframes spin {
8699
- to { transform: rotate(360deg); }
8700
- }
8701
- ` })] }));
8702
- return (jsx("button", { onClick: () => setShowDetailsModal(false), style: {
8688
+ // Only show connect button if we are fully initialized and truly disconnected
8689
+ if (!isInitializing && (!account || status === ConnectionStatus.DISCONNECTED)) {
8690
+ // Wallet icon SVG component
8691
+ const WalletIcon = () => (jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", style: { display: 'block' }, children: [jsx("path", { d: "M19 7V4a1 1 0 0 0-1-1H5a2 2 0 0 0 0 4h15a1 1 0 0 1 1 1v4h-3a2 2 0 0 0 0 4h3a1 1 0 0 0 1-1v-2a1 1 0 0 0-1-1" }), jsx("path", { d: "M3 5v14a2 2 0 0 0 2 2h15a1 1 0 0 0 1-1v-4" })] }));
8692
+ return (jsxs("button", { onClick: () => handleOpenModal(), style: {
8703
8693
  display: 'flex',
8704
8694
  alignItems: 'center',
8705
- justifyContent: 'space-between',
8706
- padding: isMobile ? '4px 4px' : '6px 6px',
8707
- borderRadius: '50px',
8695
+ gap: '10px',
8696
+ padding: '12px 12px',
8697
+ borderRadius: '12px',
8708
8698
  border: 'none',
8709
- background: '#2A2A2A',
8710
- minWidth: isMobile ? '160px' : '320px',
8711
- maxWidth: isMobile ? '100%' : undefined,
8712
- cursor: 'default',
8699
+ backgroundColor: '#FF9814',
8700
+ color: '#000000',
8701
+ fontWeight: '700',
8702
+ fontSize: '15px',
8703
+ cursor: 'pointer',
8704
+ transition: 'all 0.15s ease',
8713
8705
  fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
8706
+ boxShadow: '0 2px 8px rgba(255, 152, 20, 0.3)',
8714
8707
  ...style,
8715
- }, className: className, disabled: true, children: jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: isMobile ? '6px' : '10px', flex: '1', minWidth: 0, padding: isMobile ? '4px' : '8px' }, children: [twcIconMemoized, jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: isMobile ? '4px' : '6px', minWidth: 0 }, children: [jsx(LoadingSpinner, {}), jsx("div", { style: {
8716
- fontSize: isMobile ? '11px' : '12px',
8717
- fontWeight: '400',
8718
- color: '#FFFFFF',
8719
- fontFamily: 'Lexend, sans-serif',
8720
- }, children: "Loading..." }), jsx("div", { style: {
8721
- fontSize: isMobile ? '11px' : '12px',
8722
- fontWeight: '400',
8723
- color: '#FFFFFF',
8724
- fontFamily: 'Lexend, sans-serif',
8725
- flexShrink: 0,
8726
- }, children: "TWC" })] })] }) }));
8708
+ }, className: className, onMouseEnter: (e) => {
8709
+ e.currentTarget.style.backgroundColor = '#E8870F';
8710
+ e.currentTarget.style.transform = 'translateY(-1px)';
8711
+ e.currentTarget.style.boxShadow = '0 4px 12px rgba(255, 152, 20, 0.4)';
8712
+ }, onMouseLeave: (e) => {
8713
+ e.currentTarget.style.backgroundColor = '#FF9814';
8714
+ e.currentTarget.style.transform = 'translateY(0)';
8715
+ e.currentTarget.style.boxShadow = '0 2px 8px rgba(255, 152, 20, 0.3)';
8716
+ }, children: [jsx("span", { style: { color: '#000000', display: 'flex', alignItems: 'center' }, children: jsx(WalletIcon, {}) }), jsx("span", { style: { color: '#000000', fontWeight: '500', fontSize: '14px' }, children: buttonText })] }));
8727
8717
  }
8728
- if (isActuallyConnecting) {
8729
- return (jsx("button", { style: { ...defaultStyles.button, opacity: 0.7, ...style }, className: className, disabled: true, children: "\u23F3 Connecting..." }));
8718
+ // ...existing code...
8719
+ // All other states (including loading, connecting, connected) should only show the connected button or loading spinner if account is present or initializing is true
8720
+ // If initializing, show nothing (or a spinner if desired)
8721
+ if (isInitializing) {
8722
+ // Optionally, you can show a spinner or skeleton here, but do not show connect/disconnect buttons
8723
+ return null;
8730
8724
  }
8725
+ // ...existing code...
8726
+ // The rest of the renderButton logic (wrong network, connected, etc.) remains unchanged
8731
8727
  if (account) {
8732
- // Show "Wrong Network" button if connected to non-BSC network
8733
- if (isWrongNetwork) {
8734
- return (jsx("button", { onClick: handleSwitchToBSC, style: {
8735
- padding: '10px 16px',
8736
- borderRadius: '16px',
8737
- border: 'none',
8738
- backgroundColor: '#F59E0B',
8739
- display: 'flex',
8740
- alignItems: 'center',
8741
- gap: '10px',
8742
- cursor: 'pointer',
8743
- transition: 'all 0.15s ease',
8744
- fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
8745
- ...style,
8746
- }, className: className, onMouseEnter: (e) => {
8747
- e.currentTarget.style.backgroundColor = '#D97706';
8748
- e.currentTarget.style.transform = 'translateY(-1px)';
8749
- e.currentTarget.style.boxShadow = '0 4px 12px rgba(245, 158, 11, 0.4)';
8750
- }, onMouseLeave: (e) => {
8751
- e.currentTarget.style.backgroundColor = '#F59E0B';
8752
- e.currentTarget.style.transform = 'translateY(0)';
8753
- e.currentTarget.style.boxShadow = 'none';
8754
- }, children: jsx("span", { style: { fontSize: '13px', fontWeight: '600', color: 'white' }, children: "\uD83D\uDD04 Switch to BSC" }) }));
8755
- }
8728
+ // ...existing code...
8756
8729
  // Show normal connected button - styled to match design
8757
8730
  // Parse balance amount and symbol separately
8758
8731
  // For TON: show TON balance (not TWC, not NFT count in button)
package/dist/index.js CHANGED
@@ -7518,20 +7518,6 @@ const TWCBalanceModal = ({ isOpen, onClose, twcBalance, usdValue, colors, isDark
7518
7518
 
7519
7519
  const tonApiCache = new Map();
7520
7520
  const CACHE_DURATION = 30000; // 30 seconds
7521
- const defaultStyles = {
7522
- button: {
7523
- padding: '10px 20px',
7524
- borderRadius: '14px',
7525
- border: 'none',
7526
- backgroundColor: '#3396FF', // This will be overridden by theme colors in actual usage
7527
- color: 'white',
7528
- fontWeight: '500',
7529
- fontSize: '14px',
7530
- cursor: 'pointer',
7531
- transition: 'all 0.15s ease',
7532
- fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
7533
- boxShadow: '0 4px 14px 0 rgba(51, 150, 255, 0.39)',
7534
- }};
7535
7521
  const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet Now', getExplorerUrl, }) => {
7536
7522
  // Always fetch balance automatically when wallet is connected
7537
7523
  // Balance is always fetched automatically from the chain
@@ -7707,7 +7693,6 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7707
7693
  React.useRef(null);
7708
7694
  // Get theme colors
7709
7695
  const colors = getThemeColors(isDarkMode);
7710
- console.log('above loadbalance');
7711
7696
  // Load balance with caching
7712
7697
  const loadBalance = React.useCallback(async () => {
7713
7698
  if (!account) {
@@ -7827,7 +7812,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7827
7812
  }, 4000); // force-resolve after 4s
7828
7813
  return () => clearTimeout(timeout);
7829
7814
  }, [isInitializing, balanceLoading, isConnecting]);
7830
- // Check chain ID and validate network
7815
+ // Remove network restriction: allow all EVM networks to connect
7831
7816
  React.useEffect(() => {
7832
7817
  const checkChain = async () => {
7833
7818
  if (!account || account.chainType !== 'evm') {
@@ -7838,16 +7823,13 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7838
7823
  try {
7839
7824
  const chainId = await connector.getCurrentChain();
7840
7825
  setCurrentChainId(chainId);
7841
- // BSC Mainnet = 56, BSC Testnet = 97
7842
- const isBSC = chainId === 56 || chainId === 97;
7843
- setIsWrongNetwork(!isBSC && chainId !== null);
7826
+ // No restriction: allow all EVM networks
7827
+ setIsWrongNetwork(false);
7844
7828
  }
7845
7829
  catch (err) {
7846
7830
  }
7847
7831
  };
7848
7832
  checkChain();
7849
- // Also periodically check if still connected to EVM wallet
7850
- // This handles cases where chain is changed from wallet UI
7851
7833
  if (account && account.chainType === 'evm') {
7852
7834
  const interval = setInterval(checkChain, 2000);
7853
7835
  return () => clearInterval(interval);
@@ -7908,7 +7890,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7908
7890
  }
7909
7891
  let initTimeout;
7910
7892
  // If already connected, get account immediately and load balance
7911
- if (initialState.status === exports.ConnectionStatus.CONNECTED) {
7893
+ if (initialState.status === exports.ConnectionStatus.CONNECTED && initialState.account) {
7912
7894
  // Set isInitializing to true initially, will be set to false once balance loads
7913
7895
  setIsInitializing(true);
7914
7896
  // Safety timeout - ensure we NEVER stay in loading state for more than 2 seconds
@@ -7919,9 +7901,22 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7919
7901
  connector.getAccount().then(async (acc) => {
7920
7902
  if (!isMounted)
7921
7903
  return;
7922
- setAccount(acc);
7904
+ // Only set account to null if status is truly disconnected
7905
+ if (!acc && connector.getState().status === exports.ConnectionStatus.DISCONNECTED) {
7906
+ setAccount(null);
7907
+ }
7908
+ else if (acc) {
7909
+ setAccount(acc);
7910
+ }
7911
+ // Only proceed if account is present
7912
+ if (!acc) {
7913
+ clearTimeout(safetyTimeout);
7914
+ setIsInitializing(false);
7915
+ setBalanceLoading(false);
7916
+ return;
7917
+ }
7923
7918
  // Load balance when account is available
7924
- if (acc && acc.chainType === 'ton') {
7919
+ if (acc.chainType === 'ton') {
7925
7920
  // For TON wallets, fetch balance and NFTs
7926
7921
  setTwcBalance('0');
7927
7922
  try {
@@ -7945,7 +7940,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7945
7940
  // Check if we have real TWC balance (not 0)
7946
7941
  const state = connector.getState();
7947
7942
  const stateTwcBalance = state.twcBalance;
7948
- if (acc && stateTwcBalance && stateTwcBalance !== '0' && stateTwcBalance !== '0.00') {
7943
+ if (stateTwcBalance && stateTwcBalance !== '0' && stateTwcBalance !== '0.00') {
7949
7944
  if (!isMounted)
7950
7945
  return;
7951
7946
  setTwcBalance(stateTwcBalance);
@@ -7957,7 +7952,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
7957
7952
  clearTimeout(safetyTimeout);
7958
7953
  setIsInitializing(false);
7959
7954
  }
7960
- else if (acc) {
7955
+ else {
7961
7956
  // Try to load from cache as fallback
7962
7957
  const cached = loadTWCBalanceFromCache(acc.address);
7963
7958
  if (cached?.balance && cached.balance !== '0' && cached.balance !== '0.00') {
@@ -8049,9 +8044,11 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8049
8044
  return;
8050
8045
  clearTimeout(safetyTimeout);
8051
8046
  setIsInitializing(false);
8052
- // If we can't get account even though status is connected, disconnect
8053
- setAccount(null);
8054
- connector.disconnect().catch(() => { });
8047
+ setBalanceLoading(false);
8048
+ // Only set account to null if status is truly disconnected
8049
+ if (connector.getState().status === exports.ConnectionStatus.DISCONNECTED) {
8050
+ setAccount(null);
8051
+ }
8055
8052
  });
8056
8053
  }
8057
8054
  else {
@@ -8383,7 +8380,13 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8383
8380
  }
8384
8381
  }, [account]);
8385
8382
  const fetchTWCBalanceWithRetry = React.useCallback(async (maxRetries = 3, initialDelay = 200) => {
8383
+ // Only fetch TWC balance if wallet is on BSC (chainId 56)
8386
8384
  try {
8385
+ const state = connector.getState();
8386
+ if (state.account?.chainType !== 'evm' || state.chainId !== 56) {
8387
+ // Not on BSC, do not fetch TWC balance
8388
+ return null;
8389
+ }
8387
8390
  const twcBalance = await connector.getTWCBalance();
8388
8391
  return {
8389
8392
  balance: twcBalance.amount || '0',
@@ -8405,13 +8408,14 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8405
8408
  }
8406
8409
  return null;
8407
8410
  }, [connector]);
8408
- // Load balance when account changes
8411
+ // Load balance when account or network (chain) changes
8409
8412
  React.useEffect(() => {
8410
8413
  let isActive = true;
8411
- console.log('[ConnectButton] useEffect: account changed', { account });
8414
+ // Only log and run effect if account is not null
8412
8415
  if (account) {
8413
- // For EVM wallets, try to get TWC balance
8414
- if (account.chainType === 'evm') {
8416
+ console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8417
+ // For EVM wallets, only fetch TWC balance if on BSC
8418
+ if (account.chainType === 'evm' && currentChainId === 56) {
8415
8419
  // First check state/cache for immediate display
8416
8420
  const state = connector.getState();
8417
8421
  let hasBalance = false;
@@ -8434,9 +8438,9 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8434
8438
  hasBalance = true;
8435
8439
  }
8436
8440
  }
8437
- // Only fetch if we don't have a balance yet
8438
- if (!hasBalance) {
8439
- console.log('[ConnectButton] Fetching TWC balance for EVM wallet:', account.address);
8441
+ // Only fetch if we don't have a balance yet, or if the chain/network changed
8442
+ if (!hasBalance || currentChainId !== null) {
8443
+ console.log('[ConnectButton] Fetching TWC balance for BSC wallet:', account.address, currentChainId);
8440
8444
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8441
8445
  if (!isActive)
8442
8446
  return;
@@ -8454,16 +8458,23 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8454
8458
  setTwcBalance('0');
8455
8459
  setUsdValueStable(null);
8456
8460
  setIsInitializing(false);
8457
- console.warn('[ConnectButton] No TWC balance found for EVM wallet:', account.address);
8461
+ console.warn('[ConnectButton] No TWC balance found for BSC wallet:', account.address);
8458
8462
  }
8459
8463
  }).catch(() => {
8460
8464
  if (!isActive)
8461
8465
  return;
8462
8466
  setIsInitializing(false);
8463
- console.error('[ConnectButton] Error fetching TWC balance for EVM wallet:', account.address);
8467
+ console.error('[ConnectButton] Error fetching TWC balance for BSC wallet:', account.address);
8464
8468
  });
8465
8469
  }
8466
8470
  }
8471
+ else if (account.chainType === 'evm' && currentChainId !== 56) {
8472
+ // EVM wallet but not on BSC, do not fetch TWC balance, do not show switch chain
8473
+ setTwcBalance(null);
8474
+ setUsdValueStable(null);
8475
+ setIsInitializing(false);
8476
+ // Do not show error or prompt
8477
+ }
8467
8478
  else {
8468
8479
  // Non-EVM wallet, just check state
8469
8480
  const state = connector.getState();
@@ -8484,7 +8495,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8484
8495
  isActive = false;
8485
8496
  };
8486
8497
  // eslint-disable-next-line react-hooks/exhaustive-deps
8487
- }, [account?.address]);
8498
+ }, [account?.address, currentChainId]);
8488
8499
  // Polling mechanism as fallback to ensure TWC balance is always fetched
8489
8500
  React.useEffect(() => {
8490
8501
  if (!account || account.chainType !== 'evm') {
@@ -8651,7 +8662,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8651
8662
  alert('Address copied to clipboard!');
8652
8663
  }
8653
8664
  }, [account]);
8654
- const handleSwitchToBSC = React.useCallback(async () => {
8665
+ React.useCallback(async () => {
8655
8666
  try {
8656
8667
  // Try to switch to BSC Mainnet (56)
8657
8668
  await connector.switchChain(56);
@@ -8676,85 +8687,47 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8676
8687
  }, [connector, account]);
8677
8688
  // Render button based on connection status
8678
8689
  const renderButton = () => {
8679
- // Check if we're waiting for connection OR waiting for balance to load
8680
- // Show loading if: initializing, connecting, OR (account exists but balance hasn't loaded/resolved yet)
8681
- // For TON: treat as resolved after fetch completes (even if 0). For others: treat zero/empty as resolved.
8682
- (balance.amount && balance.amount !== '0' && balance.amount !== '0.00' && balance.amount !== '' && balance.amount !== 'Loading...') ||
8683
- (twcBalance && twcBalance !== '0' && twcBalance !== '0.00' && twcBalance !== '' && twcBalance !== 'Loading...');
8684
- // Treat balance loading as background; only gate on initial connection.
8685
- const isCheckingConnection = isInitializing || ((status === exports.ConnectionStatus.CONNECTING || isConnecting) && !account);
8686
- const isActuallyConnecting = (status === exports.ConnectionStatus.CONNECTING || isConnecting) && !!account && !isInitializing;
8687
- // Check connection state
8688
- if (isCheckingConnection) {
8689
- // Show loading connected button structure while checking connection
8690
- const isMobile = isMobileViewport();
8691
- // Loading spinner component
8692
- const LoadingSpinner = () => (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { style: {
8693
- width: isMobile ? '12px' : '14px',
8694
- height: isMobile ? '12px' : '14px',
8695
- border: '2px solid rgba(255, 255, 255, 0.3)',
8696
- borderTopColor: '#FFFFFF',
8697
- borderRadius: '50%',
8698
- animation: 'spin 0.8s linear infinite',
8699
- } }), jsxRuntime.jsx("style", { children: `
8700
- @keyframes spin {
8701
- to { transform: rotate(360deg); }
8702
- }
8703
- ` })] }));
8704
- return (jsxRuntime.jsx("button", { onClick: () => setShowDetailsModal(false), style: {
8690
+ // Only show connect button if we are fully initialized and truly disconnected
8691
+ if (!isInitializing && (!account || status === exports.ConnectionStatus.DISCONNECTED)) {
8692
+ // Wallet icon SVG component
8693
+ const WalletIcon = () => (jsxRuntime.jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", style: { display: 'block' }, children: [jsxRuntime.jsx("path", { d: "M19 7V4a1 1 0 0 0-1-1H5a2 2 0 0 0 0 4h15a1 1 0 0 1 1 1v4h-3a2 2 0 0 0 0 4h3a1 1 0 0 0 1-1v-2a1 1 0 0 0-1-1" }), jsxRuntime.jsx("path", { d: "M3 5v14a2 2 0 0 0 2 2h15a1 1 0 0 0 1-1v-4" })] }));
8694
+ return (jsxRuntime.jsxs("button", { onClick: () => handleOpenModal(), style: {
8705
8695
  display: 'flex',
8706
8696
  alignItems: 'center',
8707
- justifyContent: 'space-between',
8708
- padding: isMobile ? '4px 4px' : '6px 6px',
8709
- borderRadius: '50px',
8697
+ gap: '10px',
8698
+ padding: '12px 12px',
8699
+ borderRadius: '12px',
8710
8700
  border: 'none',
8711
- background: '#2A2A2A',
8712
- minWidth: isMobile ? '160px' : '320px',
8713
- maxWidth: isMobile ? '100%' : undefined,
8714
- cursor: 'default',
8701
+ backgroundColor: '#FF9814',
8702
+ color: '#000000',
8703
+ fontWeight: '700',
8704
+ fontSize: '15px',
8705
+ cursor: 'pointer',
8706
+ transition: 'all 0.15s ease',
8715
8707
  fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
8708
+ boxShadow: '0 2px 8px rgba(255, 152, 20, 0.3)',
8716
8709
  ...style,
8717
- }, className: className, disabled: true, children: jsxRuntime.jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: isMobile ? '6px' : '10px', flex: '1', minWidth: 0, padding: isMobile ? '4px' : '8px' }, children: [twcIconMemoized, jsxRuntime.jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: isMobile ? '4px' : '6px', minWidth: 0 }, children: [jsxRuntime.jsx(LoadingSpinner, {}), jsxRuntime.jsx("div", { style: {
8718
- fontSize: isMobile ? '11px' : '12px',
8719
- fontWeight: '400',
8720
- color: '#FFFFFF',
8721
- fontFamily: 'Lexend, sans-serif',
8722
- }, children: "Loading..." }), jsxRuntime.jsx("div", { style: {
8723
- fontSize: isMobile ? '11px' : '12px',
8724
- fontWeight: '400',
8725
- color: '#FFFFFF',
8726
- fontFamily: 'Lexend, sans-serif',
8727
- flexShrink: 0,
8728
- }, children: "TWC" })] })] }) }));
8710
+ }, className: className, onMouseEnter: (e) => {
8711
+ e.currentTarget.style.backgroundColor = '#E8870F';
8712
+ e.currentTarget.style.transform = 'translateY(-1px)';
8713
+ e.currentTarget.style.boxShadow = '0 4px 12px rgba(255, 152, 20, 0.4)';
8714
+ }, onMouseLeave: (e) => {
8715
+ e.currentTarget.style.backgroundColor = '#FF9814';
8716
+ e.currentTarget.style.transform = 'translateY(0)';
8717
+ e.currentTarget.style.boxShadow = '0 2px 8px rgba(255, 152, 20, 0.3)';
8718
+ }, children: [jsxRuntime.jsx("span", { style: { color: '#000000', display: 'flex', alignItems: 'center' }, children: jsxRuntime.jsx(WalletIcon, {}) }), jsxRuntime.jsx("span", { style: { color: '#000000', fontWeight: '500', fontSize: '14px' }, children: buttonText })] }));
8729
8719
  }
8730
- if (isActuallyConnecting) {
8731
- return (jsxRuntime.jsx("button", { style: { ...defaultStyles.button, opacity: 0.7, ...style }, className: className, disabled: true, children: "\u23F3 Connecting..." }));
8720
+ // ...existing code...
8721
+ // All other states (including loading, connecting, connected) should only show the connected button or loading spinner if account is present or initializing is true
8722
+ // If initializing, show nothing (or a spinner if desired)
8723
+ if (isInitializing) {
8724
+ // Optionally, you can show a spinner or skeleton here, but do not show connect/disconnect buttons
8725
+ return null;
8732
8726
  }
8727
+ // ...existing code...
8728
+ // The rest of the renderButton logic (wrong network, connected, etc.) remains unchanged
8733
8729
  if (account) {
8734
- // Show "Wrong Network" button if connected to non-BSC network
8735
- if (isWrongNetwork) {
8736
- return (jsxRuntime.jsx("button", { onClick: handleSwitchToBSC, style: {
8737
- padding: '10px 16px',
8738
- borderRadius: '16px',
8739
- border: 'none',
8740
- backgroundColor: '#F59E0B',
8741
- display: 'flex',
8742
- alignItems: 'center',
8743
- gap: '10px',
8744
- cursor: 'pointer',
8745
- transition: 'all 0.15s ease',
8746
- fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
8747
- ...style,
8748
- }, className: className, onMouseEnter: (e) => {
8749
- e.currentTarget.style.backgroundColor = '#D97706';
8750
- e.currentTarget.style.transform = 'translateY(-1px)';
8751
- e.currentTarget.style.boxShadow = '0 4px 12px rgba(245, 158, 11, 0.4)';
8752
- }, onMouseLeave: (e) => {
8753
- e.currentTarget.style.backgroundColor = '#F59E0B';
8754
- e.currentTarget.style.transform = 'translateY(0)';
8755
- e.currentTarget.style.boxShadow = 'none';
8756
- }, children: jsxRuntime.jsx("span", { style: { fontSize: '13px', fontWeight: '600', color: 'white' }, children: "\uD83D\uDD04 Switch to BSC" }) }));
8757
- }
8730
+ // ...existing code...
8758
8731
  // Show normal connected button - styled to match design
8759
8732
  // Parse balance amount and symbol separately
8760
8733
  // For TON: show TON balance (not TWC, not NFT count in button)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiwiflix-wallet-connector",
3
- "version": "1.5.8",
3
+ "version": "1.6.0",
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",