diff --git a/packages/dapp-toolkit/src/_types.ts b/packages/dapp-toolkit/src/_types.ts index 8ca7c698..1450ebd4 100644 --- a/packages/dapp-toolkit/src/_types.ts +++ b/packages/dapp-toolkit/src/_types.ts @@ -24,9 +24,9 @@ import type { GatewayModule, WalletRequestModule, ConnectButtonModule, + EnvironmentModule, } from './modules' import { BuildableSubintentRequest } from './modules/wallet-request/pre-authorization-request/subintent-builder' -import { EnvironmentModule } from './modules/environment' export type Providers = { connectButtonModule: ConnectButtonModule diff --git a/packages/dapp-toolkit/src/modules/environment/environment.module.ts b/packages/dapp-toolkit/src/modules/environment/environment.module.ts index bd524ee0..d520c097 100644 --- a/packages/dapp-toolkit/src/modules/environment/environment.module.ts +++ b/packages/dapp-toolkit/src/modules/environment/environment.module.ts @@ -12,6 +12,16 @@ export const EnvironmentModule = () => { return typeof navigator !== 'undefined' ? navigator : undefined } + /** + * Checks if the provided object is a Telegram Mobile App (TMA) global object. + * + * @param maybeTgGlobalObject - The object to check. + * @returns `true` if the object has WebView initialization parameters, otherwise `false`. + */ + const isTMA = () => + Object.keys((globalThis as any)?.Telegram?.WebView?.initParams || {}) + .length > 0 + return { get globalThis() { return globalThis @@ -19,6 +29,7 @@ export const EnvironmentModule = () => { isMobile: (userAgent?: string) => { return isMobile(userAgent ?? getNavigator()?.userAgent ?? '') }, + isTMA, isBrowser: () => ![typeof window, typeof document].includes('undefined'), } -} \ No newline at end of file +} diff --git a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts index aa9772eb..8f64e027 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/deep-link.module.ts @@ -2,20 +2,19 @@ import { ResultAsync } from 'neverthrow' import { errAsync, okAsync } from 'neverthrow' import { Logger } from '../../../../helpers' import { SdkError } from '../../../../error' -import { isTMA } from './helpers' import { EnvironmentModule } from '../../../environment' export type DeepLinkModule = ReturnType export const DeepLinkModule = (input: { logger?: Logger - walletUrl: string, + walletUrl: string providers: { environmentModule: EnvironmentModule } }) => { const { walletUrl } = input const logger = input?.logger?.getSubLogger({ name: 'DeepLinkModule' }) - const isTelegramMiniApp = isTMA(globalThis) + const isTelegramMiniApp = input.providers.environmentModule.isTMA() const deepLinkToWallet = ( values: Record, @@ -35,8 +34,10 @@ export const DeepLinkModule = (input: { const deepLink = outboundUrl.toString() // Telegram Mini App does not support deep linking by changing location.href value - if (isTelegramMiniApp) globalThis.open(deepLink, '_blank') - else if (globalThis.location?.href) globalThis.location.href = deepLink + if (isTelegramMiniApp) + input.providers.environmentModule.globalThis.open(deepLink, '_blank') + else if (input.providers.environmentModule.globalThis.location?.href) + input.providers.environmentModule.globalThis.location.href = deepLink return okAsync(undefined) } diff --git a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/index.ts b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/index.ts index 502b20aa..4a0ecfb1 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/index.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/index.ts @@ -1,2 +1 @@ export * from './base64url' -export * from './isTma' diff --git a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/isTma.ts b/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/isTma.ts deleted file mode 100644 index f2218c1e..00000000 --- a/packages/dapp-toolkit/src/modules/wallet-request/transport/radix-connect-relay/helpers/isTma.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Checks if the provided object is a Telegram Mobile App (TMA) global object. - * - * @param maybeTgGlobalObject - The object to check. - * @returns `true` if the object has WebView initialization parameters, otherwise `false`. - */ -export const isTMA = (globalObject: any) => - Object.keys(globalObject?.Telegram?.WebView?.initParams || {}).length > 0