fix: tg detection
All checks were successful
Deploy to VPS (dist) / deploy (push) Successful in 1m32s

This commit is contained in:
Hewston Fox
2026-03-18 02:02:25 +02:00
parent 5cff74cb39
commit 81c99c6c5f

View File

@@ -5,9 +5,14 @@ export const STORAGE_KEYS = {
} as const; } as const;
export type StorageKey = (typeof STORAGE_KEYS)[keyof typeof STORAGE_KEYS]; export type StorageKey = (typeof STORAGE_KEYS)[keyof typeof STORAGE_KEYS];
const MOCKED_START_PARAM = `debug+${Math.random().toString(36).substring(2, 15)}`; const isRealTelegram = () => {
if (typeof window === "undefined") return false;
const search = location.hash.slice(1) || location.search.slice(1);
return search.includes("tgWebAppData");
};
tg.mockTelegramEnv({ if (!isRealTelegram()) {
tg.mockTelegramEnv({
launchParams: { launchParams: {
tgWebAppData: new URLSearchParams({ tgWebAppData: new URLSearchParams({
user: JSON.stringify({ user: JSON.stringify({
@@ -27,12 +32,12 @@ tg.mockTelegramEnv({
signature: "", signature: "",
auth_date: Date.now().toString(), auth_date: Date.now().toString(),
}), }),
tgWebAppStartParam: MOCKED_START_PARAM,
tgWebAppThemeParams: {}, tgWebAppThemeParams: {},
tgWebAppVersion: "8", tgWebAppVersion: "8",
tgWebAppPlatform: "android", tgWebAppPlatform: "android",
}, },
}); });
}
type WithChecks<Result> = { type WithChecks<Result> = {
ifAvailable: (...args: any[]) => { ok: true; data: Result } | { ok: false }; ifAvailable: (...args: any[]) => { ok: true; data: Result } | { ok: false };
@@ -44,8 +49,6 @@ const isPromise = <T>(value: T | Promise<T>): value is Promise<T> =>
const promisify = <T>(value: T | Promise<T>): Promise<T> => const promisify = <T>(value: T | Promise<T>): Promise<T> =>
isPromise(value) ? value : Promise.resolve(value); isPromise(value) ? value : Promise.resolve(value);
const isTMA = () => tg.retrieveLaunchParams()?.tgWebAppStartParam !== MOCKED_START_PARAM;
const fallbackImplementation = < const fallbackImplementation = <
T extends ((...args: any) => any) & WithChecks<any>, T extends ((...args: any) => any) & WithChecks<any>,
Async extends boolean, Async extends boolean,
@@ -58,7 +61,7 @@ const fallbackImplementation = <
cb: T, cb: T,
onErr: F, onErr: F,
): Async extends true ? Promise<Result> : Awaited<Result> => { ): Async extends true ? Promise<Result> : Awaited<Result> => {
if (isTMA()) { if (isRealTelegram()) {
const res = cb.ifAvailable.apply(null, args); const res = cb.ifAvailable.apply(null, args);
const returnValue = (res.ok ? res.data : onErr.apply(null, args)) as Result; const returnValue = (res.ok ? res.data : onErr.apply(null, args)) as Result;
if (!async) return returnValue as Async extends true ? Promise<Result> : Awaited<Result>; if (!async) return returnValue as Async extends true ? Promise<Result> : Awaited<Result>;
@@ -81,7 +84,9 @@ export default {
tg.swipeBehavior.disableVertical.ifAvailable(); tg.swipeBehavior.disableVertical.ifAvailable();
tg.viewport.expand.ifAvailable(); tg.viewport.expand.ifAvailable();
tg.miniApp.setHeaderColor.ifAvailable("#000000"); tg.miniApp.setHeaderColor.ifAvailable("#000000");
console.log(isTMA() ? "Telegram Mini App initialized" : "TMA Debug mode in Web initialized"); console.log(
isRealTelegram() ? "Telegram Mini App initialized" : "TMA Debug mode in Web initialized",
);
}, },
openLink(url: string | URL, options?: tg.OpenLinkOptions) { openLink(url: string | URL, options?: tg.OpenLinkOptions) {
tg.openLink.ifAvailable(url, options); tg.openLink.ifAvailable(url, options);