From 4561314d13411a5854a278d707895ddd8efcfdc6 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Mon, 13 May 2024 20:22:41 +0700 Subject: [PATCH 1/5] chore: simplify conditional balance display and bitcoin connect store --- package.json | 2 +- src/api.ts | 31 +-------- src/components/BitcoinConnectElement.ts | 35 ++-------- src/components/bc-balance.ts | 13 +--- src/components/bc-button.ts | 23 ++----- src/components/bc-start.ts | 24 ++----- .../connectors/bc-alby-nwc-connector.ts | 4 +- src/components/pages/bc-umbrel.ts | 2 +- src/state/store.ts | 65 +++++++++---------- src/types/BitcoinConnectConfig.ts | 27 ++++++++ 10 files changed, 84 insertions(+), 142 deletions(-) create mode 100644 src/types/BitcoinConnectConfig.ts diff --git a/package.json b/package.json index 30eb03d..c369869 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getalby/bitcoin-connect", - "version": "3.4.1", + "version": "3.4.2", "description": "Web components to connect to a lightning wallet and power a website with WebLN", "type": "module", "source": "src/index.ts", diff --git a/src/api.ts b/src/api.ts index 7e8aa0a..85a8235 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,29 +1,7 @@ import {SendPaymentResponse, WebLNProvider} from '@webbtc/webln-types'; import store from './state/store'; -import {ConnectorFilter} from './types/ConnectorFilter'; -import {WebLNProviderConfig} from './types/WebLNProviderConfig'; import {PaymentMethods} from './types/PaymentMethods'; - -type BitcoinConnectConfig = { - /** - * Name of the application that the user is interacting with. - * May be passed to the connector the user chooses to connect with (e.g. NWC) - */ - appName?: string; - /** - * Limit which connectors are shown in the connect flow - */ - filters?: ConnectorFilter[]; - /** - * Set to false to not request or show the user's wallet balance - */ - showBalance?: boolean; - - /** - * Customize individual providers (NWC, LNC, LNbits etc) - */ - providerConfig?: WebLNProviderConfig; -}; +import {BitcoinConnectConfig} from './types/BitcoinConnectConfig'; type LaunchPaymentModalArgs = { /** @@ -193,10 +171,7 @@ export function isConnected() { * @param config */ export function init(config: BitcoinConnectConfig = {}) { - store.getState().setAppName(config.appName); - store.getState().setFilters(config.filters); - store.getState().setShowBalance(config.showBalance); - store.getState().setProviderConfig(config.providerConfig); + store.getState().setBitcoinConnectConfig(config); } /** @@ -312,5 +287,5 @@ export function disconnect() { * @returns the configuration of the current connector (if connected) */ export function getConnectorConfig() { - return store.getState().config; + return store.getState().connectorConfig; } diff --git a/src/components/BitcoinConnectElement.ts b/src/components/BitcoinConnectElement.ts index d2d0102..3f9a96b 100644 --- a/src/components/BitcoinConnectElement.ts +++ b/src/components/BitcoinConnectElement.ts @@ -1,4 +1,4 @@ -import {property, state} from 'lit/decorators.js'; +import {state} from 'lit/decorators.js'; import store from '../state/store'; import {InternalElement} from './internal/InternalElement'; import {ConnectorFilter} from '../types/ConnectorFilter'; @@ -29,28 +29,13 @@ export class BitcoinConnectElement extends InternalElement { @state() protected _route: Route; - @property({ - type: String, - attribute: 'app-name', - }) - appName?: string; - - @property({ - type: Array, - attribute: 'filters', - converter(value, _type) { - return value?.split(','); - }, - }) - filters?: ConnectorFilter[]; - constructor() { super(); this._connected = store.getState().connected; this._connecting = store.getState().connecting; this._connectorName = store.getState().connectorName; - this._appName = store.getState().appName; - this._filters = store.getState().filters; + this._appName = store.getState().bitcoinConnectConfig.appName; + this._filters = store.getState().bitcoinConnectConfig.filters; this._error = store.getState().error; this._route = store.getState().route; this._modalOpen = store.getState().modalOpen; @@ -60,21 +45,11 @@ export class BitcoinConnectElement extends InternalElement { this._connected = currentState.connected; this._connecting = currentState.connecting; this._connectorName = currentState.connectorName; - this._appName = currentState.appName; - this._filters = currentState.filters; + this._appName = currentState.bitcoinConnectConfig.appName; + this._filters = currentState.bitcoinConnectConfig.filters; this._error = currentState.error; this._route = currentState.route; this._modalOpen = currentState.modalOpen; }); } - - override connectedCallback() { - super.connectedCallback(); - if (this.appName != undefined) { - store.getState().setAppName(this.appName); - } - if (this.filters != undefined) { - store.getState().setFilters(this.filters); - } - } } diff --git a/src/components/bc-balance.ts b/src/components/bc-balance.ts index 387d660..e6129b2 100644 --- a/src/components/bc-balance.ts +++ b/src/components/bc-balance.ts @@ -82,21 +82,10 @@ export class Balance extends withTwind()(BitcoinConnectElement) { (async () => { try { const provider = store.getState().provider; - if (!provider) { + if (!provider?.getBalance) { return; } - const info = store.getState().info; - if ( - !info?.methods || - info.methods.indexOf('getBalance') < 0 || - !provider.getBalance - ) { - throw new Error( - 'The current WebLN provider does not support getBalance' - ); - } - const balanceResponse = await provider.getBalance(); if (balanceResponse) { this._balanceSats = balanceResponse.balance; diff --git a/src/components/bc-button.ts b/src/components/bc-button.ts index fbc5466..487984c 100644 --- a/src/components/bc-button.ts +++ b/src/components/bc-button.ts @@ -9,7 +9,6 @@ import {launchModal} from '../api.js'; import './bc-balance'; import store from '../state/store.js'; import {waitingIcon} from './icons/waitingIcon.js'; -import {GetInfoResponse, WebLNProvider} from '@webbtc/webln-types'; /** * A button that when clicked launches the modal. @@ -21,33 +20,23 @@ export class Button extends withTwind()(BitcoinConnectElement) { @state() protected _showBalance: boolean | undefined = undefined; - @state() - protected _info: GetInfoResponse | undefined = undefined; - @state() - protected _provider: WebLNProvider | undefined = undefined; constructor() { super(); - this._showBalance = store.getState().showBalance; - this._info = store.getState().info; - this._provider = store.getState().provider; + this._showBalance = + store.getState().bitcoinConnectConfig.showBalance && + store.getState().supportsGetBalance; // TODO: handle unsubscribe store.subscribe((store) => { - this._showBalance = store.showBalance; - this._info = store.info; - this._provider = store.provider; + this._showBalance = + store.bitcoinConnectConfig.showBalance && store.supportsGetBalance; }); } override render() { const isLoading = this._connecting || (!this._connected && this._modalOpen); - const showBalance = - !!this._showBalance && - !!this._info?.methods && - this._info.methods.indexOf('getBalance') > -1 && - !!this._provider?.getBalance; return html`
- ${this._connected && showBalance + ${this._connected && this._showBalance ? html` ` : null}
diff --git a/src/components/bc-start.ts b/src/components/bc-start.ts index a25502c..054bfe3 100644 --- a/src/components/bc-start.ts +++ b/src/components/bc-start.ts @@ -9,46 +9,34 @@ import {disconnectSection} from './templates/disconnectSection'; import './bc-balance'; import store from '../state/store'; import './bc-currency-switcher'; -import {GetInfoResponse, WebLNProvider} from '@webbtc/webln-types'; // TODO: split up this component into disconnected and connected @customElement('bc-start') export class Start extends withTwind()(BitcoinConnectElement) { @state() protected _showBalance: boolean | undefined = undefined; - @state() - protected _info: GetInfoResponse | undefined = undefined; - @state() - protected _provider: WebLNProvider | undefined = undefined; constructor() { super(); - this._showBalance = store.getState().showBalance; - this._info = store.getState().info; - this._provider = store.getState().provider; + this._showBalance = + store.getState().bitcoinConnectConfig.showBalance && + store.getState().supportsGetBalance; // TODO: handle unsubscribe store.subscribe((store) => { - this._showBalance = store.showBalance; - this._info = store.info; - this._provider = store.provider; + this._showBalance = + store.bitcoinConnectConfig.showBalance && store.supportsGetBalance; }); } override render() { - const showBalance = - !!this._showBalance && - !!this._info?.methods && - this._info.methods.indexOf('getBalance') > -1 && - !!this._provider?.getBalance; - return html`
${this._connected ? html` - ${showBalance + ${this._showBalance ? html` - import {launchModal} from 'https://esm.sh/@getalby/bitcoin-connect@3.4.1'; // jsdelivr.net, skypack.dev also work + import {launchModal} from 'https://esm.sh/@getalby/bitcoin-connect@3.4.2'; // jsdelivr.net, skypack.dev also work // use Bitcoin connect API normally... launchModal(); // or if you just want access to the web components: - import 'https://esm.sh/@getalby/bitcoin-connect@3.4.1'; + import 'https://esm.sh/@getalby/bitcoin-connect@3.4.2'; diff --git a/demos/html/index.html b/demos/html/index.html index 8121b08..dd05f75 100644 --- a/demos/html/index.html +++ b/demos/html/index.html @@ -5,7 +5,7 @@ Bitcoin Connect