Files
TelegramMemeVault/miniapp/src/lib/telegram.js
T

40 lines
1.7 KiB
JavaScript
Raw Normal View History

2026-10-07 02:17:30 +04:00
/** Обёртка над Telegram WebApp SDK */
const tg = window.Telegram?.WebApp
export const webApp = tg
/** initData для обмена на JWT */
export const initData = tg?.initData ?? ''
/** Закрыть Mini App */
export const close = () => tg?.close()
/**
* query_id присутствует когда Mini App открыта из меню «Приложить».
* Используется для answerWebAppQuery: позволяет отправить контент в чат одним тапом.
*/
export const getQueryId = () => tg?.initDataUnsafe?.query_id ?? null
export const isAttachmentMode = () => !!getQueryId()
/** Показать нативный алерт Telegram, или fallback на browser alert вне TG-контекста */
export const showAlert = (msg) => {
if (tg?.showAlert) tg.showAlert(msg)
else alert(msg)
}
/** Применить тему Telegram к CSS-переменным */
export function applyTheme() {
if (!tg) return
tg.expand()
tg.ready()
const colors = tg.themeParams
const root = document.documentElement.style
if (colors.bg_color) root.setProperty('--tg-bg', colors.bg_color)
if (colors.text_color) root.setProperty('--tg-text', colors.text_color)
if (colors.hint_color) root.setProperty('--tg-hint', colors.hint_color)
if (colors.link_color) root.setProperty('--tg-link', colors.link_color)
if (colors.button_color) root.setProperty('--tg-button', colors.button_color)
if (colors.button_text_color) root.setProperty('--tg-btn-text', colors.button_text_color)
if (colors.secondary_bg_color) root.setProperty('--tg-secondary-bg', colors.secondary_bg_color)
}