/** * Telegram interaction policy hooks. * Policy defaults: keep vertical swipes enabled; enable closing confirmation only * for stateful flows where user input can be lost. */ "use client"; import { useEffect } from "react"; function getTelegramWebApp(): { enableVerticalSwipes?: () => void; disableVerticalSwipes?: () => void; enableClosingConfirmation?: () => void; disableClosingConfirmation?: () => void; } | null { if (typeof window === "undefined") return null; return (window as unknown as { Telegram?: { WebApp?: unknown } }).Telegram ?.WebApp as { enableVerticalSwipes?: () => void; disableVerticalSwipes?: () => void; enableClosingConfirmation?: () => void; disableClosingConfirmation?: () => void; } | null; } /** * Keep Telegram vertical swipes enabled by default. * Disable only for screens with conflicting in-app gestures. */ export function useTelegramVerticalSwipePolicy(disableVerticalSwipes: boolean) { useEffect(() => { const webApp = getTelegramWebApp(); if (!webApp) return; try { if (disableVerticalSwipes) { webApp.disableVerticalSwipes?.(); } else { webApp.enableVerticalSwipes?.(); } } catch { // Ignore unsupported clients. } return () => { if (!disableVerticalSwipes) return; try { webApp.enableVerticalSwipes?.(); } catch { // Ignore unsupported clients. } }; }, [disableVerticalSwipes]); } /** * Enable confirmation before closing Mini App for stateful flows. */ export function useTelegramClosingConfirmation(enabled: boolean) { useEffect(() => { const webApp = getTelegramWebApp(); if (!webApp) return; try { if (enabled) { webApp.enableClosingConfirmation?.(); } else { webApp.disableClosingConfirmation?.(); } } catch { // Ignore unsupported clients. } return () => { if (!enabled) return; try { webApp.disableClosingConfirmation?.(); } catch { // Ignore unsupported clients. } }; }, [enabled]); }