2026-07-10 14:58:20 +03:00
|
|
|
// Telegram WebApp integration
|
|
|
|
|
// Uses window.Telegram.WebApp directly for maximum compatibility
|
|
|
|
|
// @telegram-apps/sdk-react provides typed wrappers but we keep a thin layer here
|
|
|
|
|
|
|
|
|
|
export function initTelegram(): void {
|
|
|
|
|
const tg = window.Telegram?.WebApp
|
|
|
|
|
if (tg) {
|
|
|
|
|
tg.ready()
|
|
|
|
|
tg.expand()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getInitData(): string {
|
|
|
|
|
return window.Telegram?.WebApp?.initData || ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getColorScheme(): 'light' | 'dark' {
|
|
|
|
|
return window.Telegram?.WebApp?.colorScheme || 'dark'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getThemeParams(): Record<string, string> {
|
|
|
|
|
return window.Telegram?.WebApp?.themeParams || {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function hapticFeedback(type: 'light' | 'medium' | 'heavy' = 'medium'): void {
|
|
|
|
|
window.Telegram?.WebApp?.HapticFeedback?.impactOccurred(type)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function showMainButton(text: string, onClick: () => void): void {
|
|
|
|
|
const btn = window.Telegram?.WebApp?.MainButton
|
|
|
|
|
if (btn) {
|
|
|
|
|
btn.setText(text)
|
|
|
|
|
btn.show()
|
|
|
|
|
btn.onClick(onClick)
|
2026-07-09 17:19:33 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 14:58:20 +03:00
|
|
|
export function hideMainButton(): void {
|
|
|
|
|
window.Telegram?.WebApp?.MainButton?.hide()
|
2026-07-09 17:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-10 14:58:20 +03:00
|
|
|
export function showBackButton(onClick: () => void): void {
|
|
|
|
|
const btn = window.Telegram?.WebApp?.BackButton
|
|
|
|
|
if (btn) {
|
|
|
|
|
btn.show()
|
|
|
|
|
btn.onClick(onClick)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function hideBackButton(): void {
|
|
|
|
|
window.Telegram?.WebApp?.BackButton?.hide()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function closeMiniApp(): void {
|
|
|
|
|
window.Telegram?.WebApp?.close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply Telegram theme CSS variables
|
|
|
|
|
export function applyTheme(): void {
|
|
|
|
|
const params = getThemeParams()
|
2026-07-09 17:19:33 +03:00
|
|
|
const root = document.documentElement
|
2026-07-10 14:58:20 +03:00
|
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
root.style.setProperty(`--tg-${key.replaceAll('_', '-')}`, value)
|
|
|
|
|
}
|
2026-07-09 17:19:33 +03:00
|
|
|
})
|
2026-07-10 14:58:20 +03:00
|
|
|
const scheme = getColorScheme()
|
|
|
|
|
root.dataset.theme = scheme
|
2026-07-09 17:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-10 14:58:20 +03:00
|
|
|
// Augment Window for TypeScript
|
|
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
|
|
|
|
Telegram?: {
|
|
|
|
|
WebApp?: {
|
|
|
|
|
ready: () => void
|
|
|
|
|
expand: () => void
|
|
|
|
|
close: () => void
|
|
|
|
|
initData: string
|
|
|
|
|
colorScheme: 'light' | 'dark'
|
|
|
|
|
themeParams: Record<string, string>
|
|
|
|
|
HapticFeedback?: {
|
|
|
|
|
impactOccurred: (type: 'light' | 'medium' | 'heavy') => void
|
|
|
|
|
notificationOccurred: (type: 'error' | 'success' | 'warning') => void
|
|
|
|
|
selectionChanged: () => void
|
|
|
|
|
}
|
|
|
|
|
MainButton?: {
|
|
|
|
|
setText: (text: string) => void
|
|
|
|
|
show: () => void
|
|
|
|
|
hide: () => void
|
|
|
|
|
onClick: (cb: () => void) => void
|
|
|
|
|
offClick: (cb: () => void) => void
|
|
|
|
|
}
|
|
|
|
|
BackButton?: {
|
|
|
|
|
show: () => void
|
|
|
|
|
hide: () => void
|
|
|
|
|
onClick: (cb: () => void) => void
|
|
|
|
|
offClick: (cb: () => void) => void
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|