Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: version 1.0.11 with all event listeners #202

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dapp-kit-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vechain/dapp-kit-react",
"version": "1.0.8",
"version": "1.0.11",
"private": false,
"homepage": "https://github.com/vechainfoundation/vechain-dapp-kit",
"repository": "github:vechain/vechain-dapp-kit",
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vechain/dapp-kit-ui",
"version": "1.0.8",
"version": "1.0.11",
"private": false,
"description": "Vanilla JS DAppKit",
"keywords": [
Expand Down
29 changes: 21 additions & 8 deletions packages/dapp-kit-ui/src/components/buttons/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DAppKitUI } from '../../client';
import { defaultI18n, type I18n, type ThemeMode } from '../../constants';
import { subscribeToCustomEvent } from '../../utils';

let dappKitConfiguredListener: () => void;

@customElement('vdk-button')
export class Button extends LitElement {
constructor() {
Expand All @@ -15,18 +17,29 @@ export class Button extends LitElement {
this.initAddressListener();

// this subscribeToCustomEvent need to be done if the DappKitUI button is reconfigured and so recreated after the initial configuration
subscribeToCustomEvent('vdk-dapp-kit-configured', () => {
this.setAddressFromState();
this.initAddressListener();
});
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.setAddressFromState();
this.initAddressListener();
},
);
} else {
subscribeToCustomEvent('vdk-dapp-kit-configured', () => {
this.setAddressFromState();
this.initAddressListener();
});
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.setAddressFromState();
this.initAddressListener();
},
);
}
}

disconnectedCallback(): void {
super.disconnectedCallback();
dappKitConfiguredListener?.();
}

private setAddressFromState(): void {
this.address = DAppKitUI.wallet.state.address ?? '';
this.requestUpdate();
Expand Down
26 changes: 20 additions & 6 deletions packages/dapp-kit-ui/src/components/modals/address-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from '../../assets/icons';
import { DAppKitUI } from '../../client';

let openWalletModalListener: () => void;
let closeWalletModalListener: () => void;
@customElement('vdk-address-modal')
export class AddressModal extends LitElement {
static override styles = [
Expand Down Expand Up @@ -133,13 +135,25 @@ export class AddressModal extends LitElement {
constructor() {
super();

subscribeToCustomEvent('vdk-open-wallet-modal', () => {
this.open = true;
});
openWalletModalListener = subscribeToCustomEvent(
'vdk-open-wallet-modal',
() => {
this.open = true;
},
);

closeWalletModalListener = subscribeToCustomEvent(
'vdk-close-wallet-modal',
() => {
this.open = false;
},
);
}

subscribeToCustomEvent('vdk-close-wallet-modal', () => {
this.open = false;
});
disconnectedCallback(): void {
super.disconnectedCallback();
openWalletModalListener?.();
closeWalletModalListener?.();
}

@property({ type: Function })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import { isMobile, subscribeToCustomEvent, useTranslate } from '../../../utils';
import { DAppKitUI } from '../../../client';
import { iconButtonStyle } from '../../../assets/styles';

let openWcQrcodeListener: () => void;
let closeWcQrcodeListener: () => void;
let openWalletModalListener: () => void;
let closeWalletModalListener: () => void;
let requestConnectionCertificateListener: () => void;
let closeConnectionCertificateRequestListener: () => void;

@customElement('vdk-connect-modal')
export class ConnectModal extends LitElement {
static override styles = [
Expand Down Expand Up @@ -51,44 +58,69 @@ export class ConnectModal extends LitElement {
constructor() {
super();

subscribeToCustomEvent('vdk-open-wc-qrcode', (options) => {
if (isMobile()) {
this.openingVeWorld = true;
window.open(
`veworld://app.veworld?uri=${encodeURIComponent(
options.uri,
)}`,
'_self',
);
}
this.open = true;
this.walletConnectQRcode = options.uri;
});
subscribeToCustomEvent('vdk-close-wc-qrcode', () => {
this.walletConnectQRcode = undefined;
this.openingVeWorld = false;
});
subscribeToCustomEvent('vdk-open-wallet-modal', () => {
if (window.vechain?.isInAppBrowser) {
this.onSourceClick(WalletSources.veworld);
} else {
openWcQrcodeListener = subscribeToCustomEvent(
'vdk-open-wc-qrcode',
(options) => {
if (isMobile()) {
this.openingVeWorld = true;
window.open(
`veworld://app.veworld?uri=${encodeURIComponent(
options.uri,
)}`,
'_self',
);
}
this.open = true;
}
});
subscribeToCustomEvent('vdk-close-wallet-modal', () => {
this.open = false;
});
subscribeToCustomEvent('vdk-request-connection-certificate', () => {
this.requestForConnectionCertificate = true;
});
subscribeToCustomEvent(
this.walletConnectQRcode = options.uri;
},
);
closeWcQrcodeListener = subscribeToCustomEvent(
'vdk-close-wc-qrcode',
() => {
this.walletConnectQRcode = undefined;
this.openingVeWorld = false;
},
);
openWalletModalListener = subscribeToCustomEvent(
'vdk-open-wallet-modal',
() => {
if (window.vechain?.isInAppBrowser) {
this.onSourceClick(WalletSources.veworld);
} else {
this.open = true;
}
},
);
closeWalletModalListener = subscribeToCustomEvent(
'vdk-close-wallet-modal',
() => {
this.open = false;
},
);
requestConnectionCertificateListener = subscribeToCustomEvent(
'vdk-request-connection-certificate',
() => {
this.requestForConnectionCertificate = true;
},
);
closeConnectionCertificateRequestListener = subscribeToCustomEvent(
'vdk-close-connection-certificate-request',
() => {
this.requestForConnectionCertificate = false;
},
);
}

disconnectedCallback(): void {
super.disconnectedCallback();
openWcQrcodeListener?.();
closeWcQrcodeListener?.();
openWalletModalListener?.();
closeWalletModalListener?.();
requestConnectionCertificateListener?.();
closeConnectionCertificateRequestListener?.();
}

private get availableSources(): SourceInfo[] {
return DAppKitUI.wallet.state.availableSources.map(
(source) => WalletSources[source],
Expand Down
16 changes: 13 additions & 3 deletions packages/dapp-kit-ui/src/components/modals/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,29 @@ import {
} from '../../constants';
import { subscribeToCustomEvent } from '../../utils';

let dappKitConfiguredListener: () => void;

@customElement('vdk-modal')
export class Modal extends LitElement {
constructor() {
super();
if (DAppKitUI.initialized) {
this.init();
} else {
subscribeToCustomEvent('vdk-dapp-kit-configured', () => {
this.init();
});
dappKitConfiguredListener = subscribeToCustomEvent(
'vdk-dapp-kit-configured',
() => {
this.init();
},
);
}
}

disconnectedCallback(): void {
super.disconnectedCallback();
dappKitConfiguredListener?.();
}

private init(): void {
this.address = DAppKitUI.wallet.state.address ?? '';
this.wallet.subscribeToKey('address', (addr) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vechain/dapp-kit",
"version": "1.0.8",
"version": "1.0.11",
"private": false,
"homepage": "https://github.com/vechainfoundation/vechain-dapp-kit",
"repository": "github:vechain/vechain-dapp-kit",
Expand Down
Loading