Skip to content

Commit

Permalink
feat: remove bowser, add EnvironmentModule (#292)
Browse files Browse the repository at this point in the history
* test: add various user agents to `isMobile` unit test

* feat: add environment module

- remove bowser in favor of custom regex
- use DI for `isMobile` and `isBrowser`

* fix: extract `isTMA` to environment module
  • Loading branch information
dawidsowardx authored Dec 10, 2024
1 parent 12c23bd commit 76d61c9
Show file tree
Hide file tree
Showing 25 changed files with 758 additions and 80 deletions.
6 changes: 6 additions & 0 deletions examples/simple-dapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DataRequestBuilder,
OneTimeDataRequestBuilder,
LocalStorageModule,
EnvironmentModule,
generateRolaChallenge,
SubintentRequestBuilder,
} from '@radixdlt/radix-dapp-toolkit'
Expand All @@ -15,6 +16,11 @@ const dAppDefinitionAddress = import.meta.env.VITE_DAPP_DEFINITION_ADDRESS
const networkId = RadixNetwork.Stokenet
const storageModule = LocalStorageModule(
`rdt:${dAppDefinitionAddress}:${networkId}`,
{
providers: {
environmentModule: EnvironmentModule(),
},
},
)
const requestsStore = storageModule.getPartition('requests')
const sessionStore = storageModule.getPartition('sessions')
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/dapp-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@noble/curves": "^1.4.0",
"base64url": "^3.0.1",
"blakejs": "^1.2.1",
"bowser": "^2.11.0",
"buffer": "^6.0.3",
"immer": "^10.0.4",
"lit": "^3.1.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/dapp-toolkit/src/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
GatewayModule,
WalletRequestModule,
ConnectButtonModule,
EnvironmentModule,
} from './modules'
import { BuildableSubintentRequest } from './modules/wallet-request/pre-authorization-request/subintent-builder'

Expand All @@ -32,6 +33,7 @@ export type Providers = {
gatewayModule: GatewayModule
stateModule: StateModule
storageModule: StorageModule
environmentModule: EnvironmentModule
walletRequestModule: WalletRequestModule
}

Expand Down
1 change: 0 additions & 1 deletion packages/dapp-toolkit/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './exponential-backoff'
export * from './fetch-wrapper'
export * from './is-mobile'
export * from './logger'
export * from './parse-json'
export * from './remove-undefined'
Expand Down
2 changes: 0 additions & 2 deletions packages/dapp-toolkit/src/helpers/is-browser.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/dapp-toolkit/src/helpers/is-mobile.spec.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/dapp-toolkit/src/helpers/is-mobile.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
RequestItem,
} from 'radix-connect-common'
import { ConnectButtonSubjects } from './subjects'
import { isMobile, type Logger } from '../../helpers'
import { type Logger } from '../../helpers'
import { ExplorerConfig } from '../../_types'
import {
transformWalletDataToConnectButton,
Expand All @@ -30,8 +30,8 @@ import {
import { GatewayModule, RadixNetworkConfigById } from '../gateway'
import { StateModule } from '../state'
import { ConnectButtonModuleOutput } from './types'
import { isBrowser } from '../../helpers/is-browser'
import { ConnectButtonNoopModule } from './connect-button-noop.module'
import { EnvironmentModule } from '../environment'

export type ConnectButtonModule = ReturnType<typeof ConnectButtonModule>

Expand All @@ -47,20 +47,25 @@ export type ConnectButtonModuleInput = {
providers: {
stateModule: StateModule
gatewayModule: GatewayModule
environmentModule: EnvironmentModule
walletRequestModule: WalletRequestModule
}
}

export const ConnectButtonModule = (
input: ConnectButtonModuleInput,
): ConnectButtonModuleOutput => {
if (!isBrowser()) {
if (!input.providers.environmentModule.isBrowser()) {
return ConnectButtonNoopModule()
}

import('@radixdlt/connect-button')
const logger = input?.logger?.getSubLogger({ name: 'ConnectButtonModule' })
const subjects = input.subjects || ConnectButtonSubjects()
const subjects =
input.subjects ||
ConnectButtonSubjects({
providers: { environmentModule: input.providers.environmentModule },
})
const dAppDefinitionAddress = input.dAppDefinitionAddress
const { baseUrl, accountsPath, transactionPath, subintentPath } =
input.explorer ?? {
Expand All @@ -86,7 +91,7 @@ export const ConnectButtonModule = (

const subscriptions = new Subscription()

const onConnectButtonRender$ = fromEvent(window, 'onConnectButtonRender')
const onConnectButtonRender$ = fromEvent(input.providers.environmentModule.globalThis, 'onConnectButtonRender')

subscriptions.add(
onConnectButtonRender$
Expand Down Expand Up @@ -418,7 +423,11 @@ export const ConnectButtonModule = (
oneTime: false,
}),
)
.map(() => isMobile() && subjects.showPopoverMenu.next(false)),
.map(
() =>
input.providers.environmentModule.isMobile() &&
subjects.showPopoverMenu.next(false),
),
),
)
.subscribe(),
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp-toolkit/src/modules/connect-button/subjects.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Account, RadixButtonTheme, RequestItem } from 'radix-connect-common'
import { BehaviorSubject, ReplaySubject, Subject } from 'rxjs'
import { isMobile } from '../../helpers'
import { ConnectButtonStatus } from './types'
import { EnvironmentModule } from '../environment'

export type ConnectButtonSubjects = ReturnType<typeof ConnectButtonSubjects>
export const ConnectButtonSubjects = () => ({
export const ConnectButtonSubjects = (input: { providers: { environmentModule: EnvironmentModule }}) => ({
onConnect: new Subject<{ challenge: string } | undefined>(),
onDisconnect: new Subject<void>(),
onUpdateSharedAccounts: new Subject<void>(),
Expand All @@ -16,7 +16,7 @@ export const ConnectButtonSubjects = () => ({
onShowPopover: new Subject<void>(),
status: new BehaviorSubject<ConnectButtonStatus>('default'),
loggedInTimestamp: new BehaviorSubject<string>(''),
isMobile: new BehaviorSubject<boolean>(isMobile()),
isMobile: new BehaviorSubject<boolean>(input.providers.environmentModule.isMobile()),
isWalletLinked: new BehaviorSubject<boolean>(false),
showPopoverMenu: new BehaviorSubject<boolean>(false),
isExtensionAvailable: new BehaviorSubject<boolean>(false),
Expand Down
Loading

0 comments on commit 76d61c9

Please sign in to comment.