import { useEffect, useState } from 'react' import { Link, Outlet, createRootRoute, useRouterState } from '@tanstack/react-router' import { useQuery } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { Menu, X } from 'lucide-react' import { useTheme, type Theme } from '@/theme/ThemeProvider' import { setLanguage } from '@/shared/lib/i18n' import { Toaster } from '@/shared/ui/toaster' import { Button } from '@/shared/ui/button' import { useAuthStore } from '@/features/auth/store' import { bootstrapSession, clearSession, logout } from '@/features/auth/api' import { TelegramLinkWarningBanner } from '@/features/telegram/TelegramLinkWarningBanner' import { getMyBillingStatus } from '@/features/billing/api' export const Route = createRootRoute({ component: RootLayout }) function RootLayout() { const { t, i18n } = useTranslation() const { theme, setTheme } = useTheme() const { user, isBootstrapping } = useAuthStore() const [mobileMenuOpen, setMobileMenuOpen] = useState(false) const pathname = useRouterState({ select: (s) => s.location.pathname }) // Гейтится наличием сессии и активации — та же логика, что и /billing (ActivationGate). Долгий // staleTime: это лишь решение "показывать ли пункт меню", а не источник актуального статуса оплаты. const billingStatusQuery = useQuery({ queryKey: ['my-billing-status'], queryFn: getMyBillingStatus, enabled: !isBootstrapping && !!user?.isActivated, staleTime: 5 * 60 * 1000, }) useEffect(() => { void bootstrapSession() }, []) // Закрываем мобильное меню при переходе на другую страницу. useEffect(() => { setMobileMenuOpen(false) }, [pathname]) const themes: Theme[] = ['light', 'dark', 'system'] const langs = ['ru', 'en'] const handleLogout = async () => { try { await logout() } finally { clearSession() } } const navLinks = !isBootstrapping && user && ( <> {t('nav.dashboard')} {user.isActivated && ( <> {t('nav.instructions')} {t('nav.news')} {t('nav.support')} {billingStatusQuery.data?.billingEnabled && ( {t('nav.billing')} )} )} {t('nav.settings')} {user.role === 'admin' && ( {t('nav.admin')} )} ) const selects = ( <> ) return (
{t('appName')}
{mobileMenuOpen && ( )}
) }