Skip to content

Commit

Permalink
Merge pull request #307 from Telegram-Mini-Apps/306-bug-tmajssdk-reac…
Browse files Browse the repository at this point in the history
…t-istma-function-returns-incorrect-value-on-some-devices

Fix incorrect `isTMA` behavior in native apps
  • Loading branch information
heyqbnk authored May 13, 2024
2 parents 5a9f1df + a77d249 commit 1aea3a8
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-parrots-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tma.js/sdk": patch
---

Fix `isTMA` incorrect behavior in native apps.
1 change: 1 addition & 0 deletions apps/local-playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<title>Document</title>
</head>
<body>
<script type="module" src="src/eruda.ts"></script>
<script type="module" src="src/index.ts"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions apps/local-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"tsconfig": "workspace:*"
},
"devDependencies": {
"eruda": "^3.0.1"
}
}
3 changes: 3 additions & 0 deletions apps/local-playground/src/eruda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eruda from 'eruda';

eruda.init();
21 changes: 2 additions & 19 deletions apps/local-playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
* import { postEvent } from '@/sdk';
*/

// import { initBackButton } from '@/sdk';
//
// const bb = initBackButton();
//
// // setDebug(true);
//
// bb.show();
//
// // on('main_button_pressed', console.warn);
import { postEvent } from '@/sdk';


import { setDebug } from '@packages/sdk/src/debug/debug.js';
import { postEvent } from '@packages/sdk/src/mini-apps/methods/postEvent.js';
// import { on } from '@packages/sdk/src/mini-apps/events/listening/on.js';

setDebug(true);

postEvent('web_app_setup_back_button', { is_visible: true, });
postEvent('web_app_setup_settings_button', { is_visible: true, });
// on('back_button_pressed', console.warn);
postEvent('web_app_expand');
39 changes: 12 additions & 27 deletions apps/local-playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "tsconfig/esnext-dom.json",
"compilerOptions": {
"composite": false,
"declaration": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": true,
"isolatedModules": true,
"lib": [
"esnext",
"DOM"
],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveWatchOutput": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es5",
"paths": {
"@/*": ["../../packages/*/src/index.ts"],
"@packages/*": ["../../packages/*"]
"@/*": [
"../../packages/*/src/index.ts"
]
}
},
"exclude": ["node_modules"],
"exclude": [
"node_modules"
],
"include": [
"src"
],
"references": [
{
"path": "./tsconfig.node.json"
}
]
// "references": [
// "./tsconfig.node.json"
// ]
}
6 changes: 5 additions & 1 deletion apps/local-playground/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"extends": "tsconfig/esnext.json",
"compilerOptions": {
"composite": true,
"module": "NodeNext"
},
"include": ["vite.config.ts"]
"include": [
"vite.config.ts"
]
}
4 changes: 2 additions & 2 deletions apps/local-playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default defineConfig({
server: {
port: 443,
https: {
cert: readFileSync(resolve(dir, '../../https-cert.pem')),
key: readFileSync(resolve(dir, '../../https-key.pem')),
cert: readFileSync(resolve(dir, '../../tma.internal.pem')),
key: readFileSync(resolve(dir, '../../tma.internal-key.pem')),
},
host: 'tma.internal',
},
Expand Down
38 changes: 29 additions & 9 deletions packages/sdk/src/env/isTMA.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect, it, vi } from 'vitest';
import { afterEach, expect, it, vi } from 'vitest';
import type { FnToSpy } from '@test-utils/types.js';

import { request as requestFn } from '@/bridge/utils/request.js';
import { isTMA } from '@/env/isTMA.js';
import { createWindow } from '@test-utils/createWindow.js';

const request = requestFn as unknown as FnToSpy<typeof requestFn>;

Expand All @@ -12,14 +13,33 @@ vi.mock('@/bridge/utils/request.js', async () => {
};
});

it('should return promise with true value resolved, if requesting theme parameters was successful', () => {
request.mockImplementationOnce(async () => ({}));
expect(isTMA()).resolves.toBe(true);
afterEach(() => {
vi.resetAllMocks();
});

it('should return promise with false value resolved, if requesting theme parameters was unsuccessful', () => {
request.mockImplementationOnce(async () => {
throw new Error('Timed out.');
});
expect(isTMA()).resolves.toBe(false);
it('should return true if current window contains TelegramWebviewProxy property', () => {
createWindow({
TelegramWebviewProxy: {},
} as any);
expect(isTMA()).resolves.toBe(true);
});

it(
'should return promise with true value resolved, if requesting theme parameters was successful',
() => {
createWindow();
request.mockImplementationOnce(async () => ({}));
expect(isTMA()).resolves.toBe(true);
},
);

it(
'should return promise with false value resolved, if requesting theme parameters was unsuccessful',
() => {
createWindow();
request.mockImplementationOnce(async () => {
throw new Error('Timed out.');
});
expect(isTMA()).resolves.toBe(false);
},
);
4 changes: 4 additions & 0 deletions packages/sdk/src/env/isTMA.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { request } from '@/bridge/utils/request.js';
import { hasWebviewProxy } from '@/env/hasWebviewProxy.js';

/**
* Returns true in case, current environment is Telegram Mini Apps.
*/
export async function isTMA(): Promise<boolean> {
if (hasWebviewProxy(window)) {
return true;
}
try {
await request({ method: 'web_app_request_theme', event: 'theme_changed', timeout: 100 });
return true;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export { setDebug } from '@/debug/debug.js';
*/
export { initWeb } from '@/env/initWeb.js';
export { isIframe } from '@/env/isIframe.js';
export { isSSR } from '@/env/isSSR.js';
export { isTMA } from '@/env/isTMA.js';

/**
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit 1aea3a8

Please sign in to comment.