Skip to content

Commit

Permalink
fix: Fixed trustwallet losing account status after refreshing (#44)
Browse files Browse the repository at this point in the history
* chore: prettier & eslint ignore changeset's md files

* fix: No need refresh when open WalletConnect Modal in QRCode page

* chore: Add vconsole for dev demo

* fix: fixed trustwallet losing status after refreshing

* chore: update release info
  • Loading branch information
wenty22 authored Nov 30, 2023
1 parent 56d4526 commit 52e9976
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 17 deletions.
5 changes: 0 additions & 5 deletions .changeset/late-birds-compare.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/lovely-months-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@totejs/walletkit': patch
---

Fixed trustwallet losing account status after refreshing
2 changes: 1 addition & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"initialVersions": {
"example-nextjs": "0.0.1",
"example-vite": "0.0.1",
"@totejs/walletkit": "1.0.7-alpha.1",
"@totejs/walletkit": "1.0.7-alpha.2",
"website": "0.0.1"
},
"changesets": [
Expand Down
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.next
dist
CHANGELOG.md
build.cjs
.changeset/*.md
1 change: 0 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:
- alpha
- 0.x
- 0.x-alpha
- dev

jobs:
release:
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.next
dist
CHANGELOG.md
CHANGELOG.md
.changeset/*.md
3 changes: 3 additions & 0 deletions packages/walletkit/dev/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chains } from './chains';
import { WagmiConfig, createConfig } from 'wagmi';
import VConsole from 'vconsole';
import {
SwitchNetworkModal,
ThemeMode,
Expand All @@ -23,6 +24,8 @@ import {
} from '../src/wallets';
import React from 'react';

new VConsole();

const config = createConfig(
getDefaultConfig({
autoConnect: true,
Expand Down
1 change: 1 addition & 0 deletions packages/walletkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"react-dom": "^18.2.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"typescript": "^5.3.2",
"vconsole": "^3.15.1",
"viem": "^1.19.9",
"vite": "^4.5.0",
"vite-plugin-dts": "^3.6.3",
Expand Down
1 change: 1 addition & 0 deletions packages/walletkit/src/globalData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface GlobalDataType {
appUrl?: string;
walletConnectProjectId?: string;
walletConnectConnector?: Connector;
walletConnectModalIsOpen?: boolean;
}

let globalData: GlobalDataType = {
Expand Down
4 changes: 2 additions & 2 deletions packages/walletkit/src/hooks/useQRCodeUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export function useQRCodeUri() {
const [wcUri, setWcUri] = useState<string>('');

useEffect(() => {
const { walletConnectConnector: connector } = getGlobalData();
const connector = getGlobalData().walletConnectConnector;
if (isConnected || !connector) return;

const onUpdateWcUri = ({ type, data }: any) => {
if (type === 'display_uri') {
if (type === 'display_uri' && !getGlobalData().walletConnectModalIsOpen) {
setWcUri(data);
}
};
Expand Down
8 changes: 6 additions & 2 deletions packages/walletkit/src/hooks/useWalletConnectModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MODAL_AUTO_CLOSE_DELAY } from '@/constants/common';
import { useEffect, useState } from 'react';
import { useModal, useWalletKitContext } from '..';
import { useWalletKitConnect } from './useWalletKitConnect';
import { getGlobalData } from '@/globalData';
import { getGlobalData, setGlobalData } from '@/globalData';

export function useWalletConnectModal() {
const { connectAsync } = useWalletKitConnect();
Expand All @@ -21,14 +21,18 @@ export function useWalletConnectModal() {
clearTimeout(timer);
};
}

setGlobalData({
walletConnectModalIsOpen: isOpen,
});
}, [isOpen, onClose]);

return {
isOpenWcModal: isOpen,
onOpenWcModal: async () => {
document.body.style.setProperty('--wcm-z-index', '2147483647');

const { walletConnectConnector: connector } = getGlobalData();
const connector = getGlobalData().walletConnectConnector;
const provider = await connector?.getProvider();
provider.rpc.showQrModal = true;

Expand Down
12 changes: 9 additions & 3 deletions packages/walletkit/src/pages/Connecting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useWalletConfig } from '@/hooks/useWalletConfig';
import { useWalletDownloadUrl } from '@/hooks/useWalletDownloadUrl';
import { useWalletKitConnect } from '@/hooks/useWalletKitConnect';
import { useWalletLogos } from '@/hooks/useWalletLogos';
import { useWalletKitContext } from '@/index';
import { cx, useWalletKitContext } from '@/index';
import { useState, useCallback, useEffect } from 'react';
import { ConnectSpinner } from './ConnectSpinner';
import { Content } from './Content';
Expand Down Expand Up @@ -167,15 +167,21 @@ export function ConnectingPage() {

{(status === states.FAILED || status === states.REJECTED) && (
<ModalFooter className={clsFooter}>
<Button className={clsButton} onClick={runConnect}>
<Button className={cx('wk-retry-button', clsButton)} onClick={runConnect}>
Try Again
</Button>
</ModalFooter>
)}

{status === states.UNAVAILABLE && (
<ModalFooter className={clsFooter}>
<Button className={clsButton} as="a" href={downloadUrl} target="_blank" rel="noopener">
<Button
className={cx('wk-download-button', clsButton)}
as="a"
href={downloadUrl}
target="_blank"
rel="noopener"
>
Install the Extension
</Button>
</ModalFooter>
Expand Down
8 changes: 8 additions & 0 deletions packages/walletkit/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ export function commonErrorHandler(props: { log: any; handler: any; error: any }
handler?.(error, description);
}
}

export async function sleep(duration = 1000) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(null);
}, duration);
});
}
10 changes: 9 additions & 1 deletion packages/walletkit/src/wallets/trustWallet/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Chain } from 'wagmi';
import { MetaMaskConnector } from 'wagmi/connectors/metaMask';
import { TRUST_WALLET_ID } from '.';
import { getInjectedProvider } from '../utils';
import { sleep } from '@/utils/common';

export type TrustWalletConnectorOptions = {
shimDisconnect?: boolean;
Expand Down Expand Up @@ -30,14 +31,21 @@ export class TrustWalletConnector extends MetaMaskConnector {
options,
});
}

public async getProvider() {
if (typeof window !== 'undefined' && !window.trustwallet?.request) {
await sleep();
}
return this.options.getProvider();
}
}

function getProvider() {
if (typeof window === 'undefined') return;

const provider = getInjectedProvider('isTrust') ?? window.trustwallet ?? window.trustWallet;

if (provider && provider.removeListener === undefined) {
if (provider && !provider.removeListener === undefined) {
provider.removeListener = provider.off;
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 52e9976

Please sign in to comment.