fix: tg detection
All checks were successful
Deploy to VPS (dist) / deploy (push) Successful in 1m32s
All checks were successful
Deploy to VPS (dist) / deploy (push) Successful in 1m32s
This commit is contained in:
@@ -5,34 +5,39 @@ export const STORAGE_KEYS = {
|
||||
} as const;
|
||||
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({
|
||||
launchParams: {
|
||||
tgWebAppData: new URLSearchParams({
|
||||
user: JSON.stringify({
|
||||
id: 1,
|
||||
first_name: "Pavel",
|
||||
is_bot: false,
|
||||
last_name: "Durov",
|
||||
username: "durov",
|
||||
language_code: "en",
|
||||
is_premium: true,
|
||||
photo_url:
|
||||
"https://media4.giphy.com/media/v1.Y2lkPTZjMDliOTUyeXF1MzYyY2pwMjR2YWFhNDhqdXBsc216MWo2aW9pczNnNXM2ZmZmbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xUPGcHc4I3wICqp8bu/giphy.gif",
|
||||
added_to_attachment_menu: false,
|
||||
allows_write_to_pm: true,
|
||||
} satisfies tg.User),
|
||||
hash: "",
|
||||
signature: "",
|
||||
auth_date: Date.now().toString(),
|
||||
}),
|
||||
tgWebAppStartParam: MOCKED_START_PARAM,
|
||||
tgWebAppThemeParams: {},
|
||||
tgWebAppVersion: "8",
|
||||
tgWebAppPlatform: "android",
|
||||
},
|
||||
});
|
||||
if (!isRealTelegram()) {
|
||||
tg.mockTelegramEnv({
|
||||
launchParams: {
|
||||
tgWebAppData: new URLSearchParams({
|
||||
user: JSON.stringify({
|
||||
id: 1,
|
||||
first_name: "Pavel",
|
||||
is_bot: false,
|
||||
last_name: "Durov",
|
||||
username: "durov",
|
||||
language_code: "en",
|
||||
is_premium: true,
|
||||
photo_url:
|
||||
"https://media4.giphy.com/media/v1.Y2lkPTZjMDliOTUyeXF1MzYyY2pwMjR2YWFhNDhqdXBsc216MWo2aW9pczNnNXM2ZmZmbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xUPGcHc4I3wICqp8bu/giphy.gif",
|
||||
added_to_attachment_menu: false,
|
||||
allows_write_to_pm: true,
|
||||
} satisfies tg.User),
|
||||
hash: "",
|
||||
signature: "",
|
||||
auth_date: Date.now().toString(),
|
||||
}),
|
||||
tgWebAppThemeParams: {},
|
||||
tgWebAppVersion: "8",
|
||||
tgWebAppPlatform: "android",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
type WithChecks<Result> = {
|
||||
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> =>
|
||||
isPromise(value) ? value : Promise.resolve(value);
|
||||
|
||||
const isTMA = () => tg.retrieveLaunchParams()?.tgWebAppStartParam !== MOCKED_START_PARAM;
|
||||
|
||||
const fallbackImplementation = <
|
||||
T extends ((...args: any) => any) & WithChecks<any>,
|
||||
Async extends boolean,
|
||||
@@ -58,7 +61,7 @@ const fallbackImplementation = <
|
||||
cb: T,
|
||||
onErr: F,
|
||||
): Async extends true ? Promise<Result> : Awaited<Result> => {
|
||||
if (isTMA()) {
|
||||
if (isRealTelegram()) {
|
||||
const res = cb.ifAvailable.apply(null, args);
|
||||
const returnValue = (res.ok ? res.data : onErr.apply(null, args)) as Result;
|
||||
if (!async) return returnValue as Async extends true ? Promise<Result> : Awaited<Result>;
|
||||
@@ -81,7 +84,9 @@ export default {
|
||||
tg.swipeBehavior.disableVertical.ifAvailable();
|
||||
tg.viewport.expand.ifAvailable();
|
||||
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) {
|
||||
tg.openLink.ifAvailable(url, options);
|
||||
|
||||
Reference in New Issue
Block a user