Skip to content

Commit

Permalink
Update var names
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Mar 21, 2024
1 parent 293bba0 commit 3769652
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 60 deletions.
12 changes: 4 additions & 8 deletions src/ExampleDApp.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { onMount } from "solid-js";

import { registerWalletSelector, selectProvider } from "./lib/utils";
import { registerWalletSelector, selectWalletProvider } from "./lib/utils";
import { mockGetAvailableProviders } from "./mocks";

registerWalletSelector();

function handleButtonClick() {
selectProvider(mockGetAvailableProviders())
selectWalletProvider(mockGetAvailableProviders())
.then(
(wallet) => {
console.log("Wallet selected", wallet);
Expand All @@ -19,10 +19,6 @@ function handleButtonClick() {
}

export function ExampleDApp() {
onMount(() => {
registerWalletSelector();
});

return (
<div>
<h1>This is a mock DApp</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { createSignal } from "solid-js";
import { ProviderOption } from "./utils";

interface Props extends ProviderOption {
onWalletSelected: (walletId: string) => void;
onProviderSelected: (walletId: string) => void;
}

export function WalletOption(props: Props) {
export function WalletProviderOption(props: Props) {
function handleWalletSelected() {
if (props.installPrompt) {
window.open(props.installPrompt.url, "_blank");
return;
}

props.onWalletSelected(props.id);
props.onProviderSelected(props.id);
}

function handleKeyDown(e: KeyboardEvent) {
Expand Down Expand Up @@ -76,6 +76,7 @@ export function WalletOption(props: Props) {
<div
style={{
color: isOutlined() ? focusTextColor : undefined,
"font-size": "14px",
"text-align": "center",
}}
>
Expand Down
21 changes: 11 additions & 10 deletions src/lib/Selector.tsx → src/lib/WalletProviderSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { Dialog } from "@ark-ui/solid";
import { For, Show, batch, createSignal, onCleanup, onMount } from "solid-js";

import { CssReset } from "./CssReset";
import { WalletOption } from "./ProviderOption";
import { WalletProviderOption } from "./WalletProviderOption";
import { XCircle } from "./XCircle";
import { cancel, close, open, select } from "./constants";
import { ProviderOption } from "./utils";

export function WalletSelector() {
export function WalletProviderSelector() {
const [isVisible, setIsVisible] = createSignal(false);
const [shouldRender, setShouldRender] = createSignal(false);
const [providers, setProviders] = createSignal<Array<ProviderOption>>([]);

const triggerFadeOut = () => setIsVisible(false);

function handleCancelClick() {
const event = new CustomEvent("sats-connect_wallet-selector_cancel", {
const event = new CustomEvent(cancel, {
bubbles: true,
composed: true,
});
Expand All @@ -23,7 +24,7 @@ export function WalletSelector() {
}

function handleWalletSelected(walletId: string) {
const event = new CustomEvent("sats-connect_wallet-selector_select", {
const event = new CustomEvent(select, {
detail: walletId,
bubbles: true,
composed: true,
Expand Down Expand Up @@ -54,8 +55,8 @@ export function WalletSelector() {
};

onMount(() => {
window.addEventListener("sats-connect_wallet-selector_open", handleOpen);
window.addEventListener("sats-connect_wallet-selector_close", handleClose);
window.addEventListener(open, handleOpen);
window.addEventListener(close, handleClose);

// Adds the DM Sans font stylesheet into the document head. Seems fonts
// can't (or are difficult) to load from the shadow DOM, yet globally
Expand All @@ -76,7 +77,7 @@ export function WalletSelector() {
}

onCleanup(() => {
window.removeEventListener("sats-connect_wallet-selector_open", handleOpen);
window.removeEventListener(open, handleOpen);
window.removeEventListener(
"sats-connect_wallet-selector_close",
handleClose,
Expand Down Expand Up @@ -202,14 +203,14 @@ export function WalletSelector() {
display: "grid",
"grid-template-columns": "repeat(3, 1fr)",
"column-gap": "8px",
"row-gap": "14px",
"row-gap": "6px",
}}
>
<For each={providers()}>
{(provider) => (
<WalletOption
<WalletProviderOption
{...provider}
onWalletSelected={handleWalletSelected}
onProviderSelected={handleWalletSelected}
/>
)}
</For>
Expand Down
4 changes: 4 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const select = "sats-connect_wallet-provider-selector_select";
export const cancel = "sats-connect_wallet-provider-selector_cancel";
export const open = "sats-connect_wallet-provider-selector_open";
export const close = "sats-connect_wallet-provider-selector_close";
67 changes: 31 additions & 36 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { customElement } from "solid-element";

import { WalletSelector } from "./Selector";
import { WalletProviderSelector } from "./WalletProviderSelector";
import { cancel, open, select } from "./constants";

export const selectorId = "sats-connect-wallet-selector";
export const elementId = "sats-connect-wallet-provider-selector";
export const elementName = elementId;

export function getWalletSelectorElement() {
return document.getElementById(selectorId);
export function getWalletProviderSelectorElement() {
return document.getElementById(elementId);
}

/**
* Call this once in your app to register the wallet provider selector element.
*/
export function registerWalletSelector() {
customElement("wallet-selector", WalletSelector);
if (customElements.get(elementName)) {
return;
}

customElement(elementName, WalletProviderSelector);

const walletSelectorElement = document.createElement("wallet-selector");
walletSelectorElement.id = selectorId;
document.body.appendChild(walletSelectorElement);
const element = document.createElement(elementName);
element.id = elementId;
document.body.appendChild(element);
}

export function cleanup() {
const walletSelectorElement = getWalletSelectorElement();
if (walletSelectorElement) {
walletSelectorElement.remove();
const element = getWalletProviderSelectorElement();
if (element) {
element.remove();
}
}

Expand All @@ -34,28 +43,20 @@ export interface ProviderOption {
installPrompt?: InstallPrompt;
}

export function selectProvider(
providers: Array<ProviderOption>,
export function selectWalletProvider(
walletProviders: Array<ProviderOption>,
): Promise<string> {
return new Promise((resolve, reject) => {
const walletSelectorElement = getWalletSelectorElement();
const walletSelectorElement = getWalletProviderSelectorElement();
if (!walletSelectorElement) {
reject(
"Failed to detect the wallet selector, aborting wallet selection.",
);
reject("Failed to detect the wallet provider selector.");
return;
}

function cleanup() {
window.removeEventListener(
"sats-connect_wallet-selector_select",
handleWalletSelectorSelectEvent,
);

window.removeEventListener(
"sats-connect_wallet-selector_cancel",
handleWalletSelectorCancelEvent,
);
window.removeEventListener(select, handleWalletSelectorSelectEvent);

window.removeEventListener(cancel, handleWalletSelectorCancelEvent);
}

function handleWalletSelectorSelectEvent(event: CustomEvent<string>) {
Expand All @@ -68,18 +69,12 @@ export function selectProvider(
cleanup();
}

window.addEventListener(
"sats-connect_wallet-selector_select",
handleWalletSelectorSelectEvent,
);
window.addEventListener(select, handleWalletSelectorSelectEvent);

window.addEventListener(
"sats-connect_wallet-selector_cancel",
handleWalletSelectorCancelEvent,
);
window.addEventListener(cancel, handleWalletSelectorCancelEvent);

const event = new CustomEvent("sats-connect_wallet-selector_open", {
detail: providers,
const event = new CustomEvent(open, {
detail: walletProviders,
});
window.dispatchEvent(event);
});
Expand Down
6 changes: 3 additions & 3 deletions src/selector-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export {};
// Allow adding custom event listeners to the window object.
declare global {
interface WindowEventMap {
"sats-connect_wallet-selector_select": CustomEvent<string>;
"sats-connect_wallet-selector_cancel": CustomEvent;
"sats-connect_wallet-selector_open": CustomEvent;
"sats-connect_wallet-provider-selector_select": CustomEvent<string>;
"sats-connect_wallet-provider-selector_cancel": CustomEvent;
"sats-connect_wallet-provider-selector_open": CustomEvent;
"sats-connect_wallet-selector_close": CustomEvent;
}
}

0 comments on commit 3769652

Please sign in to comment.