import React, { useState, useEffect, Suspense, lazy } from 'react'; import Header from './components/landing/Header'; import Hero from './components/landing/Hero'; import CoachHighlight from './components/landing/CoachHighlight'; import HowItWorks from './components/landing/HowItWorks'; import Features from './components/landing/Features'; import Testimonials from './components/landing/Testimonials'; import Pricing from './components/landing/Pricing'; import FAQ from './components/landing/FAQ'; import Footer from './components/landing/Footer'; import RegistrationModal from './components/modals/RegistrationModal'; import CalculatorsModal from './components/modals/CalculatorsModal'; import { LanguageProvider } from './contexts/LanguageContext'; import { UserProvider, useUser } from './contexts/UserContext'; import { Loader2 } from 'lucide-react'; const Dashboard = lazy(() => import('./pages/Dashboard')); const AdminPanel = lazy(() => import('./pages/AdminPanel')); const ProfessionalDashboard = lazy(() => import('./pages/ProfessionalDashboard')); const FAQPage = lazy(() => import('./pages/FAQPage')); const PrivacyPolicy = lazy(() => import('./pages/legal/PrivacyPolicy')); const TermsOfService = lazy(() => import('./pages/legal/TermsOfService')); const DataDeletion = lazy(() => import('./pages/legal/DataDeletion')); export type ViewState = 'home' | 'faq' | 'privacy' | 'terms' | 'data-deletion'; const AppContent: React.FC = () => { const [isModalOpen, setIsModalOpen] = useState(false); const [isToolsOpen, setIsToolsOpen] = useState(false); const [authMode, setAuthMode] = useState<'login' | 'register'>('register'); const [selectedPlan, setSelectedPlan] = useState('starter'); // Custom simple router state based on URL const [currentPath, setCurrentPath] = useState(window.location.pathname); useEffect(() => { const handleLocationChange = () => setCurrentPath(window.location.pathname); window.addEventListener('popstate', handleLocationChange); return () => window.removeEventListener('popstate', handleLocationChange); }, []); // Helper mapping from path to ViewState internally if needed const getCurrentView = (): ViewState => { const normalizedPath = currentPath.replace(/\/$/, '') || '/'; if (normalizedPath.includes('/faq')) return 'faq'; if (normalizedPath.includes('/privacidade')) return 'privacy'; if (normalizedPath.includes('/termos')) return 'terms'; if (normalizedPath.includes('/exclusao-de-dados')) return 'data-deletion'; return 'home'; }; const currentView = getCurrentView(); // Consume UserContext const { user, loading, isAdminView, isProfessionalView, isCompletingProfile, toggleAdminView, setIsProfessionalView, logout, refreshProfile } = useUser(); // Effect to handle "Complete Profile" flow automatically useEffect(() => { if (isCompletingProfile) { setAuthMode('register'); setIsModalOpen(true); } }, [isCompletingProfile]); const handleOpenRegister = (plan: string = 'starter') => { setSelectedPlan(plan); setAuthMode('register'); setIsModalOpen(true); }; const handleOpenLogin = (context?: 'user' | 'professional') => { if (context === 'professional') { localStorage.setItem('login_intent', 'professional'); } else { localStorage.setItem('login_intent', 'user'); } setAuthMode('login'); setIsModalOpen(true); }; const handleAuthSuccess = async () => { setIsModalOpen(false); await refreshProfile(); // Se acabou de fazer o cadastro clicando em um plano pago (monthly), leva direto pro Stripe! if (authMode === 'register' && selectedPlan === 'monthly') { try { const { data: { session } } = await supabase.auth.getSession(); if (session) { const res = await fetch(`${import.meta.env.VITE_SUPABASE_URL}/functions/v1/stripe-checkout`, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${session.access_token}`, }, body: JSON.stringify({ plan: "mensal" }) }); const { url, error } = await res.json(); if (!error && url) { window.location.href = url; // Redireciona pro Checkout return; } } } catch (e) { console.error("Falha ao redirecionar para o checkout:", e); } } // Login intent logic handled inside context or simply by state update localStorage.removeItem('login_intent'); }; // Helper function for navigating with real URLs const handleNavigate = (view: ViewState) => { let path = '/'; if (view === 'faq') path = '/faq'; if (view === 'privacy') path = '/privacidade'; if (view === 'terms') path = '/termos'; if (view === 'data-deletion') path = '/exclusao-de-dados'; window.history.pushState({}, '', path); setCurrentPath(path); window.scrollTo({ top: 0, behavior: 'smooth' }); }; if (loading) { return (
); } // Rota Admin if (user && isAdminView && user.is_admin) { return ( }> ); } // Rota Profissional if (user && isProfessionalView) { return ( }> setIsProfessionalView(false)} onLogout={logout} /> ); } // Rota Dashboard Usuário if (user) { return ( }> setIsProfessionalView(true)} initialTab={currentPath.includes('/meu-plano') ? 'coach' : 'overview'} /> ); } // Rota Pública (Landing Page ou FAQ Page) return (
handleOpenRegister('starter')} onLogin={handleOpenLogin} onOpenTools={() => setIsToolsOpen(true)} onNavigate={handleNavigate} />
{currentView === 'home' ? ( <> handleOpenRegister('starter')} /> handleOpenRegister('starter')} /> ) : currentView === 'privacy' ? (
}> handleNavigate('home')} /> ) : currentView === 'terms' ? ( }> handleNavigate('home')} /> ) : currentView === 'data-deletion' ? ( }> handleNavigate('home')} /> ) : ( }> handleNavigate('home')} /> )}