Skip to content

Commit

Permalink
fix: extract isTMA to environment module
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Dec 10, 2024
1 parent 62c907c commit fe596ff
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/dapp-toolkit/src/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ 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
},
isMobile: (userAgent?: string) => {
return isMobile(userAgent ?? getNavigator()?.userAgent ?? '')
},
isTMA,
isBrowser: () => ![typeof window, typeof document].includes('undefined'),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof DeepLinkModule>
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<string, string>,
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './base64url'
export * from './isTma'

This file was deleted.

0 comments on commit fe596ff

Please sign in to comment.