Skip to content

Commit

Permalink
fix(rdt): deep link issue for telegram mini apps
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Oct 17, 2024
1 parent 566da1c commit c19ed47
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/simple-dapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<title>Simple dApp</title>
</head>
<body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { errAsync, okAsync } from 'neverthrow'
import { Logger, isMobile } from '../../../../helpers'
import Bowser from 'bowser'
import { SdkError } from '../../../../error'
import { isTMA } from './helpers'

export type DeepLinkModule = ReturnType<typeof DeepLinkModule>
export const DeepLinkModule = (input: {
Expand All @@ -13,6 +14,7 @@ export const DeepLinkModule = (input: {
const userAgent = Bowser.parse(window.navigator.userAgent)
const { platform } = userAgent
const logger = input?.logger?.getSubLogger({ name: 'DeepLinkModule' })
const isTelegramMiniApp = isTMA(globalThis)

logger?.debug({
platform,
Expand All @@ -34,8 +36,12 @@ export const DeepLinkModule = (input: {
data: { ...values },
})

if (isMobile() && globalThis.location?.href) {
globalThis.location.href = outboundUrl.toString()
if (isMobile()) {
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

return okAsync(undefined)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './base64url'
export * from './isTma'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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

0 comments on commit c19ed47

Please sign in to comment.