Skip to content

Commit 02ae3b2

Browse files
wc adapter on click hndler
1 parent 8e1ddd1 commit 02ae3b2

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

examples/vue-app/package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/adapters/wallet-connect-v1-adapter/src/walletConnectV1adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class WalletConnectV1Adapter extends BaseAdapter<void> {
180180
return reject(err);
181181
}
182182
const uri = payload.params[0];
183-
this.updateAdapterData({ uri } as WalletConnectV1Data);
183+
this.updateAdapterData({ uri, extensionAdapters: WALLET_CONNECT_EXTENSION_ADAPTERS } as WalletConnectV1Data);
184184

185185
this.connector?.off("display_uri");
186186
return resolve();

packages/ui/src/components/WalletConnect.tsx

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IWalletConnectExtensionAdapter } from "@web3auth/base";
2-
import bowser, { PLATFORMS_MAP } from "bowser";
3-
import { memo, useMemo } from "react";
2+
import bowser, { OS_MAP, PLATFORMS_MAP } from "bowser";
3+
import { memo, useEffect, useMemo, useState } from "react";
44
import QRCode from "react-qr-code";
55

66
import Image from "./Image";
@@ -12,18 +12,78 @@ interface WalletConnectProps {
1212
wcAdapters: IWalletConnectExtensionAdapter[];
1313
}
1414

15+
interface IMobileRegistryEntry {
16+
name: string;
17+
logo: string;
18+
universalLink: string;
19+
deepLink: string;
20+
href: string;
21+
}
22+
type platform = "mobile" | "desktop";
23+
24+
type os = "iOS" | "Android";
25+
26+
function formatIOSMobile(params: { uri: string; universalLink?: string; deepLink?: string }) {
27+
const encodedUri: string = encodeURIComponent(params.uri);
28+
if (params.universalLink) {
29+
return `${params.universalLink}/wc?uri=${encodedUri}`;
30+
}
31+
if (params.deepLink) {
32+
return `${params.deepLink}${params.deepLink.endsWith(":") ? "//" : "/"}wc?uri=${encodedUri}`;
33+
}
34+
return "";
35+
}
36+
37+
function formatMobileRegistryEntry(
38+
entry: IWalletConnectExtensionAdapter,
39+
walletConnectUri: string,
40+
os: os,
41+
platform: "mobile" | "desktop" = "mobile"
42+
): IMobileRegistryEntry {
43+
const universalLink = entry[platform].universal || "";
44+
const deepLink = entry[platform].native || "";
45+
return {
46+
name: entry.name || "",
47+
logo: entry.logo || "",
48+
universalLink,
49+
deepLink,
50+
href: os === OS_MAP.iOS ? formatIOSMobile({ uri: walletConnectUri, universalLink, deepLink }) : walletConnectUri,
51+
};
52+
}
53+
54+
function formatMobileRegistry(
55+
registry: IWalletConnectExtensionAdapter[],
56+
walletConnectUri: string,
57+
os: os,
58+
platform: platform = "mobile"
59+
): IMobileRegistryEntry[] {
60+
return Object.values<IWalletConnectExtensionAdapter>(registry)
61+
.filter((entry) => !!entry[platform].universal || !!entry[platform].native)
62+
.map((entry) => formatMobileRegistryEntry(entry, walletConnectUri, os, platform));
63+
}
64+
1565
function WalletConnect(props: WalletConnectProps) {
1666
const { walletConnectUri, wcAdapters } = props;
17-
const platform = useMemo(() => {
67+
const [links, setLinks] = useState<IMobileRegistryEntry[]>([]);
68+
69+
const deviceDetails = useMemo<{ platform: platform; os: os }>(() => {
1870
const browser = bowser.getParser(window.navigator.userAgent);
19-
return browser.getPlatformType();
71+
return { platform: browser.getPlatformType() as platform, os: browser.getOSName() as os };
2072
}, []);
73+
74+
useEffect(() => {
75+
if (deviceDetails.platform === PLATFORMS_MAP.mobile) {
76+
const mobileLinks = formatMobileRegistry(wcAdapters, walletConnectUri, deviceDetails.os, deviceDetails.platform);
77+
setLinks(mobileLinks);
78+
}
79+
}, [wcAdapters, deviceDetails.os, deviceDetails.platform, walletConnectUri]);
80+
2181
// TODO: show only wcAdapters of current chain
2282
return (
2383
<div className="w3ajs-wallet-connect w3a-wallet-connect">
2484
<i className="w3a-wallet-connect__logo">{walletConnectIcon}</i>
2585
<div className="w3ajs-wallet-connect__container w3a-wallet-connect__container">
26-
{platform === PLATFORMS_MAP.desktop ? (
86+
{deviceDetails.platform === PLATFORMS_MAP.desktop ? (
2787
<>
2888
<div>Scan QR code with a WalletConnect-compatible wallet</div>
2989
<div className="w3ajs-wallet-connect-qr w3a-wallet-connect-qr">
@@ -32,10 +92,24 @@ function WalletConnect(props: WalletConnectProps) {
3292
</>
3393
) : (
3494
<>
35-
{wcAdapters.map((adapter) => {
95+
{links.map((link) => {
3696
// TODO: render logo and on click,
3797
// https://github.com/WalletConnect/walletconnect-monorepo/blob/54f3ca0b1cd1ac24e8992a5a812fdfad01769abb/packages/helpers/browser-utils/src/registry.ts#L24
3898
// format and show
99+
return deviceDetails.os === OS_MAP.iOS ? (
100+
<a key={link.name} href={link.href} rel="noopener noreferrer" target="_blank">
101+
<button type="button" className="w3a-button w3a-button--icon">
102+
{link.logo}
103+
</button>
104+
<p className="w3a-adapter-item__label">{link.name}</p>
105+
</a>
106+
) : (
107+
<a key={link.name} href={link.href} rel="noopener noreferrer" target="_blank">
108+
<button type="button" className="w3a-button w3a-button--icon">
109+
Connect
110+
</button>
111+
</a>
112+
);
39113
})}
40114
</>
41115
)}

0 commit comments

Comments
 (0)