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 */} setShowDropdown(!showDropdown)} className="flex items-center gap-3 px-3 py-2 rounded-lg hover:bg-surface-tertiary transition-colors" > {getInitials(user?.name)} {user?.name || 'User'} {roleInfo.icon} {t(roleInfo.labelKey)} {showDropdown && ( {/* User info */} {user?.name} {user?.email} {roleInfo.icon} {t(roleInfo.labelKey)} {/* Menu items */} {user?.role === 'superadmin' && ( { setShowDropdown(false) window.location.href = '/users' }} className="w-full flex items-center gap-3 px-4 py-2.5 hover:bg-surface-secondary transition-colors text-start" > {t('header.userManagement')} )} {t('header.changePassword')} { setShowDropdown(false) logout() }} className="w-full flex items-center gap-3 px-4 py-2.5 hover:bg-red-50 transition-colors text-left group" > {t('header.signOut')} )} {/* Change Password Modal */} setShowPasswordModal(false)} title={t('header.changePassword')} size="md"> {t('header.currentPassword')} { 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="••••••••" /> {t('header.newPassword')} { 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} /> {t('header.confirmNewPassword')} { 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} )} setShowPasswordModal(false)} className="px-4 py-2 text-sm font-medium text-text-secondary hover:bg-surface-tertiary rounded-lg" > {t('common.cancel')} {passwordSaving ? t('header.saving') : t('header.updatePassword')} > ) }
{user?.name || 'User'}
{roleInfo.icon} {t(roleInfo.labelKey)}
{user?.name}
{user?.email}
{passwordError}
{passwordSuccess}