import { useState, useRef, useEffect } from 'react' import { useLocation } from 'react-router-dom' import { Bell, ChevronDown, LogOut, Shield, Lock, AlertCircle, CheckCircle } from 'lucide-react' import { useAuth } from '../contexts/AuthContext' import { getInitials, api } from '../utils/api' import Modal from './Modal' const pageTitles = { '/': 'Dashboard', '/posts': 'Post Production', '/assets': 'Assets', '/campaigns': 'Campaigns', '/finance': 'Finance', '/projects': 'Projects', '/tasks': 'My Tasks', '/team': 'Team', '/users': 'User Management', } const ROLE_INFO = { superadmin: { label: 'Superadmin', color: 'bg-purple-100 text-purple-700', icon: '👑' }, manager: { label: 'Manager', color: 'bg-blue-100 text-blue-700', icon: '📊' }, contributor: { label: 'Contributor', color: 'bg-green-100 text-green-700', icon: '✏️' }, } export default function Header() { const { user, logout } = useAuth() 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 (pageTitles[pathname]) return pageTitles[pathname] if (pathname.startsWith('/projects/')) return 'Project Details' if (pathname.startsWith('/campaigns/')) return 'Campaign Details' return '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('New passwords do not match') return } if (passwordForm.newPassword.length < 6) { setPasswordError('New password must be at least 6 characters') return } setPasswordSaving(true) try { await api.patch('/users/me/password', { currentPassword: passwordForm.currentPassword, newPassword: passwordForm.newPassword, }) setPasswordSuccess('Password updated successfully') setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' }) setTimeout(() => setShowPasswordModal(false), 1500) } catch (err) { setPasswordError(err.message || 'Failed to change password') } 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 */} {/* Notifications */} {/* 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} {roleInfo.label} {showDropdown && ( {/* User info */} {user?.name} {user?.email} {roleInfo.icon} {roleInfo.label} {/* 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-left" > User Management )} Change Password { 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" > Sign Out )} {/* Change Password Modal */} setShowPasswordModal(false)} title="Change Password" size="md"> Current Password { 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="••••••••" /> New Password { 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} /> Confirm New Password { 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" > Cancel {passwordSaving ? 'Saving...' : 'Update Password'} > ) }
{user?.name || 'User'}
{roleInfo.icon} {roleInfo.label}
{user?.name}
{user?.email}
{passwordError}
{passwordSuccess}