@reown/appkit-scaffold-ui
Version:
The full stack toolkit to build onchain app UX.
227 lines • 9.93 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { LitElement, html } from 'lit';
import { property, state } from 'lit/decorators.js';
import { ErrorUtil } from '@reown/appkit-common';
import { AppKitError, ChainController, ConnectionController, CoreHelperUtil, EventsController, MobileWalletUtil, ModalController, OptionsController, RouterController, SnackController } from '@reown/appkit-controllers';
import { customElement } from '@reown/appkit-ui';
import { CaipNetworksUtil } from '@reown/appkit-utils';
import '../../partials/w3m-connecting-header/index.js';
import '../../partials/w3m-connecting-wc-browser/index.js';
import '../../partials/w3m-connecting-wc-desktop/index.js';
import '../../partials/w3m-connecting-wc-mobile/index.js';
import '../../partials/w3m-connecting-wc-qrcode/index.js';
import '../../partials/w3m-connecting-wc-unsupported/index.js';
import '../../partials/w3m-connecting-wc-web/index.js';
import styles from './styles.js';
let W3mConnectingWcView = class W3mConnectingWcView extends LitElement {
constructor() {
super();
this.wallet = RouterController.state.data?.wallet;
this.unsubscribe = [];
this.platform = undefined;
this.platforms = [];
this.isSiwxEnabled = Boolean(OptionsController.state.siwx);
this.remoteFeatures = OptionsController.state.remoteFeatures;
this.displayBranding = true;
this.basic = false;
this.determinePlatforms();
this.initializeConnection();
this.unsubscribe.push(OptionsController.subscribeKey('remoteFeatures', val => (this.remoteFeatures = val)));
}
disconnectedCallback() {
this.unsubscribe.forEach(unsubscribe => unsubscribe());
}
render() {
if (OptionsController.state.enableMobileFullScreen) {
this.setAttribute('data-mobile-fullscreen', 'true');
}
return html `
${this.headerTemplate()}
<div class="platform-container">${this.platformTemplate()}</div>
${this.reownBrandingTemplate()}
`;
}
reownBrandingTemplate() {
if (!this.remoteFeatures?.reownBranding || !this.displayBranding) {
return null;
}
return html `<wui-ux-by-reown></wui-ux-by-reown>`;
}
async initializeConnection(retry = false) {
if (this.platform === 'browser' || (OptionsController.state.manualWCControl && !retry)) {
return;
}
try {
const { wcPairingExpiry, status } = ConnectionController.state;
const { redirectView } = RouterController.state.data ?? {};
if (retry ||
OptionsController.state.enableEmbedded ||
CoreHelperUtil.isPairingExpired(wcPairingExpiry) ||
status === 'connecting') {
const connectionsByNamespace = ConnectionController.getConnections(ChainController.state.activeChain);
const isMultiWalletEnabled = this.remoteFeatures?.multiWallet;
const hasConnections = connectionsByNamespace.length > 0;
await ConnectionController.connectWalletConnect({ cache: 'never' });
if (!this.isSiwxEnabled) {
if (hasConnections && isMultiWalletEnabled) {
RouterController.replace('ProfileWallets');
SnackController.showSuccess('New Wallet Added');
}
else if (redirectView) {
RouterController.replace(redirectView);
}
else {
ModalController.close();
}
}
}
}
catch (error) {
if (error instanceof Error &&
error.message.includes('An error occurred when attempting to switch chain') &&
!OptionsController.state.enableNetworkSwitch) {
if (ChainController.state.activeChain) {
ChainController.setActiveCaipNetwork(CaipNetworksUtil.getUnsupportedNetwork(`${ChainController.state.activeChain}:${ChainController.state.activeCaipNetwork?.id}`));
ChainController.showUnsupportedChainUI();
return;
}
}
const isUserRejectedRequestError = error instanceof AppKitError &&
error.originalName === ErrorUtil.PROVIDER_RPC_ERROR_NAME.USER_REJECTED_REQUEST;
if (isUserRejectedRequestError) {
EventsController.sendEvent({
type: 'track',
event: 'USER_REJECTED',
properties: { message: error.message }
});
}
else {
EventsController.sendEvent({
type: 'track',
event: 'CONNECT_ERROR',
properties: { message: error?.message ?? 'Unknown' }
});
}
ConnectionController.setWcError(true);
SnackController.showError(error.message ?? 'Connection error');
ConnectionController.resetWcConnection();
RouterController.goBack();
}
}
determinePlatforms() {
if (!this.wallet) {
this.platforms.push('qrcode');
this.platform = 'qrcode';
return;
}
if (this.platform) {
return;
}
const { mobile_link, desktop_link, webapp_link, injected, rdns } = this.wallet;
const injectedIds = injected?.map(({ injected_id }) => injected_id).filter(Boolean);
const browserIds = [...(rdns ? [rdns] : (injectedIds ?? []))];
const isBrowser = OptionsController.state.isUniversalProvider ? false : browserIds.length;
const hasMobileWCLink = mobile_link;
const isWebWc = webapp_link;
const isBrowserInstalled = ConnectionController.checkInstalled(browserIds);
const isBrowserWc = isBrowser && isBrowserInstalled;
const isDesktopWc = desktop_link && !CoreHelperUtil.isMobile();
if (isBrowserWc && !ChainController.state.noAdapters) {
this.platforms.push('browser');
}
if (hasMobileWCLink) {
this.platforms.push(CoreHelperUtil.isMobile() ? 'mobile' : 'qrcode');
}
if (isWebWc) {
this.platforms.push('web');
}
if (isDesktopWc) {
this.platforms.push('desktop');
}
const isCustomDeeplinkWallet = MobileWalletUtil.isCustomDeeplinkWallet(this.wallet.id, ChainController.state.activeChain);
if (!isBrowserWc && isBrowser && !ChainController.state.noAdapters && !isCustomDeeplinkWallet) {
this.platforms.push('unsupported');
}
this.platform = this.platforms[0];
}
platformTemplate() {
switch (this.platform) {
case 'browser':
return html `<w3m-connecting-wc-browser></w3m-connecting-wc-browser>`;
case 'web':
return html `<w3m-connecting-wc-web></w3m-connecting-wc-web>`;
case 'desktop':
return html `
<w3m-connecting-wc-desktop .onRetry=${() => this.initializeConnection(true)}>
</w3m-connecting-wc-desktop>
`;
case 'mobile':
return html `
<w3m-connecting-wc-mobile isMobile .onRetry=${() => this.initializeConnection(true)}>
</w3m-connecting-wc-mobile>
`;
case 'qrcode':
return html `<w3m-connecting-wc-qrcode ?basic=${this.basic}></w3m-connecting-wc-qrcode>`;
default:
return html `<w3m-connecting-wc-unsupported></w3m-connecting-wc-unsupported>`;
}
}
headerTemplate() {
const multiPlatform = this.platforms.length > 1;
if (!multiPlatform) {
return null;
}
return html `
<w3m-connecting-header
.platforms=${this.platforms}
.onSelectPlatfrom=${this.onSelectPlatform.bind(this)}
>
</w3m-connecting-header>
`;
}
async onSelectPlatform(platform) {
const container = this.shadowRoot?.querySelector('div');
if (container) {
await container.animate([{ opacity: 1 }, { opacity: 0 }], {
duration: 200,
fill: 'forwards',
easing: 'ease'
}).finished;
this.platform = platform;
container.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 200,
fill: 'forwards',
easing: 'ease'
});
}
}
};
W3mConnectingWcView.styles = styles;
__decorate([
state()
], W3mConnectingWcView.prototype, "platform", void 0);
__decorate([
state()
], W3mConnectingWcView.prototype, "platforms", void 0);
__decorate([
state()
], W3mConnectingWcView.prototype, "isSiwxEnabled", void 0);
__decorate([
state()
], W3mConnectingWcView.prototype, "remoteFeatures", void 0);
__decorate([
property({ type: Boolean })
], W3mConnectingWcView.prototype, "displayBranding", void 0);
__decorate([
property({ type: Boolean })
], W3mConnectingWcView.prototype, "basic", void 0);
W3mConnectingWcView = __decorate([
customElement('w3m-connecting-wc-view')
], W3mConnectingWcView);
export { W3mConnectingWcView };
//# sourceMappingURL=index.js.map