'use client' import { useState, useEffect } from 'react' import { useRouter, usePathname } from 'next/navigation' import { useStore } from '@/lib/store' import { LoginForm } from '@/components/auth/login-form' import { Book, MessageCircle, Heart, Search, User, LogOut } from 'lucide-react' export function Navigation() { const [showLogin, setShowLogin] = useState(false) const [activeTab, setActiveTab] = useState('bible') const { user, setUser } = useStore() const router = useRouter() const pathname = usePathname() // Sync navigation state with current route useEffect(() => { if (pathname === '/') { setActiveTab('bible') } else if (pathname.includes('/dashboard')) { // Extract tab from URL or local storage const savedTab = localStorage.getItem('activeTab') if (savedTab) { setActiveTab(savedTab) } } }, [pathname]) // Initialize user from localStorage on component mount useEffect(() => { const token = localStorage.getItem('authToken') if (token && !user) { // Validate token and get user info fetch('/api/auth/me', { headers: { 'Authorization': `Bearer ${token}` } }) .then(res => res.json()) .then(data => { if (data.user) { setUser(data.user) } else { localStorage.removeItem('authToken') } }) .catch(() => { localStorage.removeItem('authToken') }) } }, [user, setUser]) const handleLogout = () => { setUser(null) localStorage.removeItem('authToken') localStorage.removeItem('activeTab') router.push('/') } const handleTabChange = (tabId: string) => { setActiveTab(tabId) localStorage.setItem('activeTab', tabId) // Navigate to dashboard if not already there if (pathname !== '/dashboard') { router.push('/dashboard') } // Emit custom event for tab change window.dispatchEvent(new CustomEvent('tabChange', { detail: { tab: tabId } })) } const navItems = [ { id: 'bible', label: 'Biblia', icon: Book }, { id: 'chat', label: 'Chat AI', icon: MessageCircle }, { id: 'prayers', label: 'Rugăciuni', icon: Heart }, { id: 'search', label: 'Căutare', icon: Search }, ] return ( ) }