Skip to content

Commit

Permalink
Fix auto connect to nonexist wallet gh-380
Browse files Browse the repository at this point in the history
  • Loading branch information
twhy committed Dec 23, 2023
1 parent cd920f1 commit 69ab243
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/bases/main-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export abstract class MainWalletBase extends WalletBase {
}
}

protected onSetChainsDone(): void {}
protected onSetChainsDone(): void { }

private makeFinalEndpoints(chain: ChainRecord) {
const isTestNet = chain.name.includes('testnet');
Expand Down Expand Up @@ -192,6 +192,11 @@ export abstract class MainWalletBase extends WalletBase {
};

async update() {
// in case nonexist wallet is set as current wallet
if (this.walletStatus === 'NotExist') {
return localStorage.removeItem('cosmos-kit@2:core//current-wallet');
}

this.setData(void 0);
this.setMessage(void 0);
this.setState(State.Done);
Expand Down
2 changes: 1 addition & 1 deletion packages/example/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function MyApp({ Component, pageProps }: AppProps) {
// ...web3AuthWallets,
// ...trustWallets,
// ...stationWallets,
// ...cosmostationWallets,
...cosmostationWallets,
// ...omniWallets,
// ...exodusWallets,
// ...shellWallets,
Expand Down
2 changes: 1 addition & 1 deletion packages/example/pages/use-chains.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function () {
</thead>
<tbody>
{Object.entries(chains).map(([name, chain]) =>
<tr>
<tr key={name}>
<td>chains.{name}.address</td>
<td>{chain.address}</td>
</tr>
Expand Down
6 changes: 3 additions & 3 deletions packages/react-lite/src/hooks/useChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export function useChains(chainNames: ChainName[], sync = true) {
if (wallet.isModeExtension) {
wallet.callbacks.beforeConnect = async () => {
try {
await wallet.client.enable?.(ids);
await wallet.client?.enable?.(ids);
} catch (e) {
for (const repo of repos) {
await wallet.client.addChain?.(repo.chainRecord)
await wallet.client?.addChain?.(repo.chainRecord)
}
await wallet.client.enable?.(ids);
await wallet.client?.enable?.(ids);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/modal/components/views/WalletList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ function DynamicWalletList({ wallets, onClose }: DynamicWalletListProps) {
const [isLargeScreen, setIsLargeScreen] = useState(true);

const onWalletClicked = useCallback(async (wallet: ChainWalletBase) => {
await wallet.connect(true);
onClose();
await wallet.connect(wallet.walletStatus !== 'NotExist');
if (wallet.walletStatus !== 'NotExist') {
onClose();
}
}, []);

useEffect(() => {
Expand Down
17 changes: 15 additions & 2 deletions packages/react/src/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export function WalletModal({
setCurrentView(ModalView.Rejected);
break;
case WalletStatus.NotExist:
setCurrentView(ModalView.NotExist);
setCurrentView((prev) =>
prev === ModalView.Connected
? ModalView.WalletList
: ModalView.NotExist
);
break;
case WalletStatus.Disconnected:
setCurrentView(ModalView.WalletList);
Expand All @@ -114,7 +118,16 @@ export function WalletModal({
break;
}
}
}, [isOpen, qrState, walletStatus, qrMsg, message]);
}, [qrState, walletStatus, qrMsg, message]);

useEffect(() => {
if (!isOpen) return;
if (walletStatus === 'Connected') {
setCurrentView(ModalView.Connected);
} else {
setCurrentView(ModalView.WalletList);
}
}, [isOpen]);

const onCloseModal = useCallback(() => {
setOpen(false);
Expand Down

0 comments on commit 69ab243

Please sign in to comment.