From 425ce924b3412cd39fd332f30732cdecfc981994 Mon Sep 17 00:00:00 2001 From: Divyansh Date: Sun, 11 Jan 2026 14:42:29 +0530 Subject: [PATCH] fix: cleanup store subscription in bc-button --- src/components/bc-button.ts | 71 ++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/src/components/bc-button.ts b/src/components/bc-button.ts index 6c654e30..97cf8c10 100644 --- a/src/components/bc-button.ts +++ b/src/components/bc-button.ts @@ -11,7 +11,7 @@ import store from '../state/store.js'; import {waitingIcon} from './icons/waitingIcon.js'; /** - * A button that when clicked launches the modal. + * A button that launches the Bitcoin Connect modal. */ @customElement('bc-button') export class Button extends withTwind()(BitcoinConnectElement) { @@ -19,57 +19,78 @@ export class Button extends withTwind()(BitcoinConnectElement) { override title = 'Connect Wallet'; @state() - protected _showBalance: boolean | undefined = undefined; + protected _showBalance = false; + + private _unsubscribe?: () => void; constructor() { super(); - this._showBalance = - store.getState().bitcoinConnectConfig.showBalance && - store.getState().supports('getBalance'); + this._updateShowBalance(); - // TODO: handle unsubscribe - store.subscribe((store) => { + this._unsubscribe = store.subscribe((state) => { this._showBalance = - store.bitcoinConnectConfig.showBalance && store.supports('getBalance'); + state.bitcoinConnectConfig.showBalance && state.supports('getBalance'); }); } + disconnectedCallback() { + super.disconnectedCallback(); + this._unsubscribe?.(); + this._unsubscribe = undefined; + } + + private _updateShowBalance() { + const state = store.getState(); + this._showBalance = + state.bitcoinConnectConfig.showBalance && state.supports('getBalance'); + } + override render() { const isLoading = this._connecting || (!this._connected && this._modalOpen); - return html`
-
+ const ariaLabel = isLoading + ? 'Connecting to wallet' + : this._connected + ? 'Wallet connected' + : this.title; + + return html` +
+ ${this._connected ? innerBorder() : ''} - + + ${isLoading - ? html` ${waitingIcon(`w-11 h-11 -mr-2 mr-1 -ml-2.5`)} ` + ? html`${waitingIcon('w-11 h-11 -mr-2 mr-1 -ml-2.5')}` : this._connected ? null : html`${bcIcon}`} + ${isLoading - ? html`Connecting...` + ? 'Connecting...' : this._connected - ? html`Connected` - : html`${this.title}`} + ? 'Connected' + : this.title} + ${this._connected && this._showBalance - ? html` ` + ? html`` : null}
-
`; + `; } private _onClick() {