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: pass selected provider when not persisting selection #351

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changeset/ninety-zebras-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@stacks/connect-ui': patch
'@stacks/connect': patch
---

Fix bug that would revert to undefined wallet popup behavior.
4 changes: 2 additions & 2 deletions packages/connect-ui/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Element, Prop, h } from '@stencil/core';
import { WebBTCProvider } from '../../providers';
import { WebBTCProvider, getProviderFromId } from '../../providers';
import { setSelectedProviderId } from '../../session';
import CloseIcon from './assets/close-icon.svg';
import { getBrowser, getPlatform } from './utils';
Expand All @@ -23,7 +23,7 @@ export class Modal {
handleSelectProvider(providerId: string) {
if (this.persistSelection) setSelectedProviderId(providerId);
this.modalEl.remove();
this.callback();
this.callback(getProviderFromId(providerId));
}

handleCloseModal() {
Expand Down
10 changes: 7 additions & 3 deletions packages/connect-ui/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ export const getInstalledProviders = (defaultProviders: WebBTCProvider[] = []) =

const registeredProviders = getRegisteredProviders();

const additionalInstalledProviders = defaultProviders.filter(dp => {
const additionalInstalledProviders = defaultProviders.filter(defaultProvider => {
// already registered, don't add again
if (registeredProviders.find(rp => rp.id === dp.id)) return false;
if (registeredProviders.find(rp => rp.id === defaultProvider.id)) return false;

// check if default provider is installed (even if not registered)
const provider = dp.id.split('.').reduce((acc, part) => acc?.[part], window);
const provider = getProviderFromId(defaultProvider.id);
return !!provider;
});

return registeredProviders.concat(additionalInstalledProviders);
};

export const getProviderFromId = (id: string | undefined) => {
janniks marked this conversation as resolved.
Show resolved Hide resolved
return id?.split('.').reduce((acc, part) => acc?.[part], window);
};
17 changes: 9 additions & 8 deletions packages/connect/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,28 @@ export type ActionOptions = (

/** Helper higher-order function for creating connect methods that allow for wallet selection */
function wrapConnectCall<O extends ActionOptions>(
action: (o: O, p?: StacksProvider) => any,
action: (options: O, provider?: StacksProvider) => any,
persistSelection = true
) {
return function wrapped(o: O, p?: StacksProvider) {
if (p) return action(o, p); // if a provider is passed, use it
return function wrapped(options: O, provider?: StacksProvider) {
if (provider) return action(options, provider); // if a provider is passed, use it

const providerId = getSelectedProviderId();
const provider = getStacksProvider();
if (providerId && provider) return action(o, provider); // if a provider is selected, use it
const selectedId = getSelectedProviderId();
const selectedProvider = getStacksProvider();
if (selectedId && selectedProvider) return action(options, selectedProvider); // if a provider is selected, use it

if (typeof window === 'undefined') return;
void defineCustomElements(window);

const defaultProviders = o?.defaultProviders ?? DEFAULT_PROVIDERS;
const defaultProviders = options?.defaultProviders ?? DEFAULT_PROVIDERS;
const installedProviders = getInstalledProviders(defaultProviders);

const element = document.createElement('connect-modal');
element.defaultProviders = defaultProviders;
element.installedProviders = installedProviders;
element.persistSelection = persistSelection;
element.callback = () => action(o);
element.callback = (selectedProvider: StacksProvider | undefined) =>
action(options, selectedProvider);

document.body.appendChild(element);

Expand Down
6 changes: 2 additions & 4 deletions packages/connect/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { getSelectedProviderId } from '@stacks/connect-ui';
import { getSelectedProviderId, getProviderFromId } from '@stacks/connect-ui';

export function getStacksProvider() {
const providerId = getSelectedProviderId();
const provider = providerId?.split('.').reduce((acc, part) => acc?.[part], window);

const provider = getProviderFromId(getSelectedProviderId());
return provider || window.StacksProvider || window.BlockstackProvider;
}

Expand Down
Loading