import { useState, useRef, useEffect } from 'react' import { useLocation } from 'react-router-dom' import { ChevronDown, LogOut, Shield, Lock, AlertCircle, CheckCircle } from 'lucide-react' import { useAuth } from '../contexts/AuthContext' import { getInitials, api } from '../utils/api' import { useLanguage } from '../i18n/LanguageContext' import Modal from './Modal' import ThemeToggle from './ThemeToggle' const PAGE_TITLE_KEYS = { '/': 'header.dashboard', '/posts': 'header.posts', '/calendar': 'header.calendar', '/assets': 'header.assets', '/artefacts': 'header.artefacts', '/campaigns': 'header.campaigns', '/brands': 'header.brands', '/finance': 'header.finance', '/budgets': 'header.budgets', '/projects': 'header.projects', '/tasks': 'header.tasks', '/issues': 'header.issues', '/team': 'header.team', '/settings': 'header.settings', } const ROLE_INFO = { superadmin: { labelKey: 'header.superadmin', color: 'bg-purple-100 text-purple-700', icon: '👑' }, manager: { labelKey: 'header.manager', color: 'bg-blue-100 text-blue-700', icon: '📊' }, contributor: { labelKey: 'header.contributor', color: 'bg-green-100 text-green-700', icon: '✏️' }, } export default function Header() { const { user, logout } = useAuth() const { t } = useLanguage() const [showDropdown, setShowDropdown] = useState(false) const [showPasswordModal, setShowPasswordModal] = useState(false) const [passwordForm, setPasswordForm] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }) const [passwordError, setPasswordError] = useState('') const [passwordSuccess, setPasswordSuccess] = useState('') const [passwordSaving, setPasswordSaving] = useState(false) const dropdownRef = useRef(null) const location = useLocation() function getPageTitle(pathname) { if (PAGE_TITLE_KEYS[pathname]) return t(PAGE_TITLE_KEYS[pathname]) if (pathname.startsWith('/projects/')) return t('header.projectDetails') if (pathname.startsWith('/campaigns/')) return t('header.campaignDetails') return t('header.page') } const pageTitle = getPageTitle(location.pathname) useEffect(() => { const handleClickOutside = (e) => { if (dropdownRef.current && !dropdownRef.current.contains(e.target)) { setShowDropdown(false) } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, []) const handlePasswordChange = async () => { setPasswordError('') setPasswordSuccess('') if (passwordForm.newPassword !== passwordForm.confirmPassword) { setPasswordError(t('header.passwordMismatch')) return } if (passwordForm.newPassword.length < 6) { setPasswordError(t('header.passwordMinLength')) return } setPasswordSaving(true) try { await api.patch('/users/me/password', { currentPassword: passwordForm.currentPassword, newPassword: passwordForm.newPassword, }) setPasswordSuccess(t('header.passwordUpdateSuccess')) setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' }) setTimeout(() => setShowPasswordModal(false), 1500) } catch (err) { setPasswordError(err.message || t('header.passwordUpdateFailed')) } finally { setPasswordSaving(false) } } const openPasswordModal = () => { setShowDropdown(false) setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' }) setPasswordError('') setPasswordSuccess('') setShowPasswordModal(true) } const roleInfo = ROLE_INFO[user?.role] || ROLE_INFO.contributor return ( <>
{/* Page title */}

{pageTitle}

{/* Right side */}
{/* Theme toggle */} {/* User menu */}
{showDropdown && (
{/* User info */}

{user?.name}

{user?.email}

{roleInfo.icon} {t(roleInfo.labelKey)}
{/* Menu items */}
{user?.role === 'superadmin' && ( )}
)}
{/* Change Password Modal */} setShowPasswordModal(false)} title={t('header.changePassword')} size="md">
{ setPasswordForm(f => ({ ...f, currentPassword: e.target.value })); setPasswordError('') }} className="w-full px-3 py-2 text-sm border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-brand-primary/20 focus:border-brand-primary" placeholder="••••••••" />
{ setPasswordForm(f => ({ ...f, newPassword: e.target.value })); setPasswordError('') }} className="w-full px-3 py-2 text-sm border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-brand-primary/20 focus:border-brand-primary" placeholder="••••••••" minLength={6} />
{ setPasswordForm(f => ({ ...f, confirmPassword: e.target.value })); setPasswordError('') }} className="w-full px-3 py-2 text-sm border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-brand-primary/20 focus:border-brand-primary" placeholder="••••••••" minLength={6} />
{passwordError && (

{passwordError}

)} {passwordSuccess && (

{passwordSuccess}

)}
) }