Skip to content

Commit

Permalink
feat: empty connect button module for non-browser env
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Jun 7, 2024
1 parent 1e0b5db commit daaef7b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 32 deletions.
2 changes: 2 additions & 0 deletions packages/dapp-toolkit/src/helpers/is-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isBrowser = () =>
![typeof window, typeof document].includes('undefined')
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NEVER } from 'rxjs'
import { ConnectButtonModuleOutput } from './types'

export const ConnectButtonNoopModule = (): ConnectButtonModuleOutput => {
return {
status$: NEVER,
onConnect$: NEVER,
onDisconnect$: NEVER,
onUpdateSharedData$: NEVER,
onShowPopover$: NEVER,
onCancelRequestItem$: NEVER,
onLinkClick$: NEVER,
setStatus: () => {},
setMode: () => {},
setTheme: () => {},
setActiveTab: () => {},
setIsMobile: () => {},
setIsWalletLinked: () => {},
setIsExtensionAvailable: () => {},
setConnected: () => {},
setLoggedInTimestamp: () => {},
setRequestItems: () => {},
setAccounts: () => {},
setPersonaData: () => {},
setPersonaLabel: () => {},
setDappName: () => {},
destroy: () => {},
disconnect: () => {},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {
map,
merge,
mergeMap,
Observable,
of,
Subscription,
switchMap,
tap,
timer,
} from 'rxjs'
import type { ConnectButton } from '@radixdlt/connect-button'
import { ConnectButton } from '@radixdlt/connect-button'
import type {
Account,
RadixButtonStatus,
Expand All @@ -30,39 +29,12 @@ import {
import { GatewayModule, RadixNetworkConfigById } from '../gateway'
import { StateModule } from '../state'
import { StorageModule } from '../storage'
import { ConnectButtonStatus } from './types'
import { ConnectButtonModuleOutput, ConnectButtonStatus } from './types'
import { isBrowser } from '../../helpers/is-browser'
import { ConnectButtonNoopModule } from './connect-button-noop.module'

export type ConnectButtonModule = ReturnType<typeof ConnectButtonModule>

export type ConnectButtonModuleOutput = {
status$: Observable<RadixButtonStatus>
onConnect$: Observable<{ challenge: string } | undefined>
onDisconnect$: Observable<void>
onUpdateSharedData$: Observable<void>
onShowPopover$: Observable<void>
onCancelRequestItem$: Observable<string>
onLinkClick$: Observable<{
type: 'account' | 'transaction' | 'showQrCode' | 'setupGuide'
data: string
}>
setStatus: (value: RadixButtonStatus) => void
setMode: (value: 'light' | 'dark') => void
setTheme: (value: RadixButtonTheme) => void
setActiveTab: (value: 'sharing' | 'requests') => void
setIsMobile: (value: boolean) => void
setIsWalletLinked: (value: boolean) => void
setIsExtensionAvailable: (value: boolean) => void
setConnected: (value: boolean) => void
setLoggedInTimestamp: (value: string) => void
setRequestItems: (value: RequestItem[]) => void
setAccounts: (value: Account[]) => void
setPersonaData: (value: { value: string; field: string }[]) => void
setPersonaLabel: (value: string) => void
setDappName: (value: string) => void
destroy: () => void
disconnect: () => void
}

export type ConnectButtonModuleInput = {
networkId: number
environment?: string
Expand All @@ -86,6 +58,10 @@ export type ConnectButtonModuleInput = {
export const ConnectButtonModule = (
input: ConnectButtonModuleInput,
): ConnectButtonModuleOutput => {
if (!isBrowser()) {
return ConnectButtonNoopModule()
}

import('@radixdlt/connect-button')
const logger = input?.logger?.getSubLogger({ name: 'ConnectButtonModule' })
const subjects = input.subjects || ConnectButtonSubjects()
Expand Down
37 changes: 37 additions & 0 deletions packages/dapp-toolkit/src/modules/connect-button/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import {
RadixButtonStatus,
RadixButtonTheme,
RequestItem,
Account,
} from 'radix-connect-common'
import { Observable } from 'rxjs'

export type ConnectButtonStatus =
(typeof ConnectButtonStatus)[keyof typeof ConnectButtonStatus]
export const ConnectButtonStatus = {
Expand All @@ -6,3 +14,32 @@ export const ConnectButtonStatus = {
default: 'default',
error: 'error',
} as const

export type ConnectButtonModuleOutput = {
status$: Observable<RadixButtonStatus>
onConnect$: Observable<{ challenge: string } | undefined>
onDisconnect$: Observable<void>
onUpdateSharedData$: Observable<void>
onShowPopover$: Observable<void>
onCancelRequestItem$: Observable<string>
onLinkClick$: Observable<{
type: 'account' | 'transaction' | 'showQrCode' | 'setupGuide'
data: string
}>
setStatus: (value: RadixButtonStatus) => void
setMode: (value: 'light' | 'dark') => void
setTheme: (value: RadixButtonTheme) => void
setActiveTab: (value: 'sharing' | 'requests') => void
setIsMobile: (value: boolean) => void
setIsWalletLinked: (value: boolean) => void
setIsExtensionAvailable: (value: boolean) => void
setConnected: (value: boolean) => void
setLoggedInTimestamp: (value: string) => void
setRequestItems: (value: RequestItem[]) => void
setAccounts: (value: Account[]) => void
setPersonaData: (value: { value: string; field: string }[]) => void
setPersonaLabel: (value: string) => void
setDappName: (value: string) => void
destroy: () => void
disconnect: () => void
}
2 changes: 2 additions & 0 deletions packages/dapp-toolkit/src/radix-dapp-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
SendTransactionInput,
WalletApi,
} from './_types'
import { isBrowser } from './helpers/is-browser'
import {
type WalletData,
LocalStorageModule,
Expand All @@ -14,6 +15,7 @@ import {
ConnectButtonModule,
generateGatewayApiConfig,
} from './modules'
import { ConnectButtonNoopModule } from './modules/connect-button/connect-button-noop.module'

export type RadixDappToolkit = {
walletApi: WalletApi
Expand Down

0 comments on commit daaef7b

Please sign in to comment.