tronprotocol / tronwallet-adapter

MIT License
64 stars 29 forks source link

react hook won't autoconnect when selecting the same adapter as last time #27

Closed alienzhangyw closed 1 year ago

alienzhangyw commented 1 year ago

To reproduce:

  1. Run the react-ui demo
  2. Disconnect any existing connection
  3. Select one wallet with the SelectWalletButton
  4. Fail to connect by any method, like refusing the request or closing the modal
  5. Select the same wallet again
  6. Nothing happens, auto-connection was not triggered since it depends on the adapter's name change.

Maybe you can uncomment this line to fix it. https://github.com/tronprotocol/tronwallet-adapter/blob/eaaac80af7de46efe05620ee9b2cc173a804ca4c/packages/react/react-hooks/src/WalletProvider.tsx#L241C38-L241C38

Manully connecting the wallet with connect from useWallet also works badly. If I use connect just after select, it will always connect to the last selected adapter.

way2ex commented 1 year ago

Sorry for any inconvenience. autoConnect is designed to work when selected wallet changes. If there is a selected wallet and it is the wallet that the user want to connect, we can call connect(). If the selected wallet is not the wallet that the user want to connect, we can call select() and it will automatically connect.

Before call select method , we can judge that if the current selected wallet is we want to connect. If so just call connect().

alienzhangyw commented 1 year ago

Sorry for any inconvenience. autoConnect is designed to work when selected wallet changes. If there is a selected wallet and it is the wallet that the user want to connect, we can call connect(). If the selected wallet is not the wallet that the user want to connect, we can call select() and it will automatically connect.

Before call select method , we can judge that if the current selected wallet is we want to connect. If so just call connect().

Yeah, I've already handled this before. Just manually connect when selecting while adapter.name is the same

  const handleSelect = useCallback((w: Wallet) => {
    if (w.adapter.name === wallet?.adapter.name) {
      connect();
    } else {
      select(w.adapter.name);
    }
    onClose();
  }, [connect, onClose, select, wallet?.adapter.name]);