'use client'; import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { Button } from '@/components/ui/Button'; import { Dropdown, DropdownItem } from '@/components/ui/Dropdown'; import { Footer } from '@/components/ui/Footer'; import { useTranslation } from '@/hooks/useTranslation'; interface User { id: string; name: string | null; email: string; plan: string | null; } export default function AppLayout({ children, }: { children: React.ReactNode; }) { const pathname = usePathname(); const router = useRouter(); const { t } = useTranslation(); const [sidebarOpen, setSidebarOpen] = useState(false); const [user, setUser] = useState(null); // Fetch user data on mount useEffect(() => { const fetchUser = async () => { try { const response = await fetch('/api/user'); if (response.ok) { const userData = await response.json(); setUser(userData); } } catch (error) { console.error('Error fetching user:', error); } }; fetchUser(); }, []); const handleSignOut = async () => { // Track logout event before clearing data try { const { trackEvent, resetUser } = await import('@/components/PostHogProvider'); trackEvent('user_logout'); resetUser(); // Reset PostHog user session } catch (error) { console.error('PostHog tracking error:', error); } // Clear all cookies document.cookie.split(";").forEach(c => { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); }); // Clear localStorage localStorage.clear(); // Redirect to home router.push('/'); }; // Get user initials for avatar (e.g., "Timo Schmidt" -> "TS") const getUserInitials = () => { if (!user) return 'U'; if (user.name) { const names = user.name.trim().split(' '); if (names.length >= 2) { return (names[0][0] + names[names.length - 1][0]).toUpperCase(); } return user.name.substring(0, 2).toUpperCase(); } // Fallback to email return user.email.substring(0, 1).toUpperCase(); }; // Get display name (first name or full name) const getDisplayName = () => { if (!user) return 'User'; if (user.name) { return user.name; } // Fallback to email without domain return user.email.split('@')[0]; }; const navigation = [ { name: t('nav.dashboard'), href: '/dashboard', icon: ( ), }, { name: t('nav.create_qr'), href: '/create', icon: ( ), }, { name: t('nav.bulk_creation'), href: '/bulk-creation', icon: ( ), }, { name: t('nav.analytics'), href: '/analytics', icon: ( ), }, { name: t('nav.pricing'), href: '/pricing', icon: ( ), }, { name: t('nav.settings'), href: '/settings', icon: ( ), }, ]; return (
{/* Mobile sidebar backdrop */} {sidebarOpen && (
setSidebarOpen(false)} /> )} {/* Sidebar */} {/* Main content */}
{/* Top bar */}
{/* User Menu */}
{getUserInitials()}
{getDisplayName()} } > Sign Out
{/* Page content */}
{children}
{/* Footer */}
); }