File size: 7,613 Bytes
f4854a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { useState, useEffect } from 'react';
import { Link, useLocation, useNavigate } from 'react-router-dom';
import { motion, AnimatePresence } from 'framer-motion';
import { HiMenu, HiX, HiChevronDown, HiHeart, HiUserCircle } from 'react-icons/hi';
import { useAuth } from '../../contexts/AuthContext';
import { NAV_LINKS, FOUNDATION_INFO } from '../../utils/constants';
import './Header.css';

export default function Header() {
  const [isScrolled, setIsScrolled] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);
  const [dropdownOpen, setDropdownOpen] = useState(null);
  const { currentUser, userProfile, logout } = useAuth();
  const location = useLocation();
  const navigate = useNavigate();

  useEffect(() => {
    const handleScroll = () => setIsScrolled(window.scrollY > 20);
    window.addEventListener('scroll', handleScroll);
    return () => window.removeEventListener('scroll', handleScroll);
  }, []);

  // Close mobile menu on route change
  useEffect(() => {
    setMobileOpen(false);
    setDropdownOpen(null);
  }, [location]);

  const handleLogout = async () => {
    try {
      await logout();
      navigate('/');
    } catch (error) {
      console.error('Logout error:', error);
    }
  };

  const isActive = (path) => location.pathname === path;

  return (
    <header className={`header ${isScrolled ? 'header--scrolled' : ''}`}>
      <div className="header__container container">
        {/* Logo */}
        <Link to="/" className="header__logo">
          <img src="/logo.png" alt={FOUNDATION_INFO.name} className="header__logo-img" />
          <div className="header__logo-text">
            <span className="header__logo-name">Social Share</span>
            <span className="header__logo-sub">& Care Foundation</span>
          </div>
        </Link>

        {/* Desktop Navigation */}
        <nav className="header__nav">
          {NAV_LINKS.map((link) => (
            <div
              key={link.path}
              className="header__nav-item"
              onMouseEnter={() => link.children && setDropdownOpen(link.path)}
              onMouseLeave={() => setDropdownOpen(null)}
            >
              <Link
                to={link.path}
                className={`header__nav-link ${isActive(link.path) ? 'header__nav-link--active' : ''}`}
              >
                {link.label}
                {link.children && <HiChevronDown className="header__chevron" />}
              </Link>

              {/* Dropdown */}
              <AnimatePresence>
                {link.children && dropdownOpen === link.path && (
                  <motion.div
                    className="header__dropdown"
                    initial={{ opacity: 0, y: 10 }}
                    animate={{ opacity: 1, y: 0 }}
                    exit={{ opacity: 0, y: 10 }}
                    transition={{ duration: 0.2 }}
                  >
                    {link.children.map((child) => (
                      <Link
                        key={child.path}
                        to={child.path}
                        className="header__dropdown-link"
                      >
                        {child.label}
                      </Link>
                    ))}
                  </motion.div>
                )}
              </AnimatePresence>
            </div>
          ))}
        </nav>

        {/* Right Actions */}
        <div className="header__actions">
          <Link to="/donate" className="btn btn--primary btn--sm header__donate-btn">
            <HiHeart /> Donate
          </Link>

          {currentUser ? (
            <div
              className="header__user"
              onMouseEnter={() => setDropdownOpen('user')}
              onMouseLeave={() => setDropdownOpen(null)}
            >
              <button className="header__user-btn">
                {userProfile?.profileImage ? (
                  <img src={userProfile.profileImage} alt="" className="header__avatar" referrerPolicy="no-referrer" />
                ) : (
                  <HiUserCircle size={40} />
                )}
              </button>

              <AnimatePresence>
                {dropdownOpen === 'user' && (
                  <motion.div
                    className="header__dropdown header__dropdown--right"
                    initial={{ opacity: 0, y: 10 }}
                    animate={{ opacity: 1, y: 0 }}
                    exit={{ opacity: 0, y: 10 }}
                  >
                    <div className="header__dropdown-user">
                      <strong>{userProfile?.displayName}</strong>
                      <span>{userProfile?.email}</span>
                    </div>
                    <Link to="/dashboard" className="header__dropdown-link">Dashboard</Link>
                    <Link to="/dashboard/profile" className="header__dropdown-link">Profile</Link>
                    <Link to="/donations/history" className="header__dropdown-link">My Donations</Link>
                    <button onClick={handleLogout} className="header__dropdown-link header__dropdown-link--danger">
                      Sign Out
                    </button>
                  </motion.div>
                )}
              </AnimatePresence>
            </div>
          ) : (
            <Link to="/login" className="btn btn--outline btn--sm">
              Sign In
            </Link>
          )}

          {/* Mobile Toggle */}
          <button
            className="header__mobile-toggle"
            onClick={() => setMobileOpen(!mobileOpen)}
            aria-label="Toggle menu"
          >
            {mobileOpen ? <HiX size={24} /> : <HiMenu size={24} />}
          </button>
        </div>
      </div>

      {/* Mobile Menu */}
      <AnimatePresence>
        {mobileOpen && (
          <motion.div
            className="header__mobile"
            initial={{ height: 0, opacity: 0 }}
            animate={{ height: 'auto', opacity: 1 }}
            exit={{ height: 0, opacity: 0 }}
            transition={{ duration: 0.3 }}
          >
            <nav className="header__mobile-nav">
              {NAV_LINKS.map((link) => (
                <div key={link.path}>
                  <Link
                    to={link.path}
                    className={`header__mobile-link ${isActive(link.path) ? 'header__mobile-link--active' : ''}`}
                  >
                    {link.label}
                  </Link>
                  {link.children && (
                    <div className="header__mobile-sub">
                      {link.children.map((child) => (
                        <Link key={child.path} to={child.path} className="header__mobile-sub-link">
                          {child.label}
                        </Link>
                      ))}
                    </div>
                  )}
                </div>
              ))}
              {!currentUser && (
                <Link to="/login" className="btn btn--primary" style={{ marginTop: '1rem' }}>
                  Sign In
                </Link>
              )}
              {currentUser && (
                <>
                  <Link to="/dashboard" className="header__mobile-link">Dashboard</Link>
                  <Link to="/donations/history" className="header__mobile-link">My Donations</Link>
                  <button onClick={handleLogout} className="header__mobile-link" style={{ color: '#EF4444' }}>
                    Sign Out
                  </button>
                </>
              )}
            </nav>
          </motion.div>
        )}
      </AnimatePresence>
    </header>
  );
}