File size: 10,651 Bytes
b64de39
 
fc91a5e
b64de39
4995d62
 
b64de39
 
 
34af792
75fda81
b64de39
 
 
 
34af792
 
75fda81
b64de39
75fda81
34af792
75fda81
b64de39
fc91a5e
75fda81
b64de39
 
34af792
 
b64de39
75fda81
 
 
 
 
 
 
 
b64de39
 
 
 
 
 
 
 
34af792
 
 
 
 
 
b64de39
4995d62
fc91a5e
4995d62
 
b64de39
 
 
 
 
4995d62
b64de39
 
4995d62
75fda81
b64de39
 
 
 
 
 
 
 
 
 
4995d62
b64de39
 
 
 
 
 
 
 
 
4995d62
b64de39
fc91a5e
 
b64de39
 
 
 
 
 
 
 
 
 
4995d62
75fda81
 
 
 
 
 
b64de39
 
75fda81
34af792
 
 
75fda81
 
 
 
 
fc91a5e
 
75fda81
 
 
 
 
34af792
 
75fda81
 
 
fc91a5e
 
75fda81
 
34af792
 
 
 
 
 
 
fc91a5e
75fda81
 
34af792
 
 
 
 
 
 
 
 
75fda81
34af792
 
 
 
b64de39
 
 
 
fc91a5e
b64de39
 
75fda81
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc91a5e
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
fc91a5e
 
b64de39
 
 
 
 
 
 
34af792
 
fc91a5e
34af792
 
75fda81
 
 
 
 
 
fc91a5e
 
75fda81
 
 
 
 
34af792
 
 
 
 
 
fc91a5e
 
34af792
 
 
75fda81
34af792
 
 
 
fc91a5e
34af792
 
75fda81
34af792
 
 
 
 
 
 
 
75fda81
34af792
 
 
b64de39
 
 
 
 
 
 
 
 
75fda81
b64de39
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
 * Header - Top navigation bar with logo, navigation links,
 * language selector, and user profile.
 *
 * Features a subtle gradient background and responsive
 * hamburger menu on mobile.
 */

import { useState, useCallback } from "react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import PropTypes from "prop-types";
import {
  Bars3Icon,
  XMarkIcon,
  UserCircleIcon,
  ArrowRightOnRectangleIcon,
  Squares2X2Icon,
} from "@heroicons/react/24/outline";
import { ROUTES, APP_NAME } from "@utils/constants";
import { useAuth } from "@context/AuthContext";
import LanguageSelector from "./LanguageSelector";

function Header({ language, onLanguageChange }) {
  const { t } = useTranslation();
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
  const location = useLocation();
  const navigate = useNavigate();
  const { isAuthenticated, user, logout } = useAuth();

  const NAV_ITEMS = [
    { label: t("nav.home"), path: ROUTES.HOME },
    { label: t("nav.diseaseDetection"), path: ROUTES.DISEASE_DETECTION },
    { label: t("nav.weather"), path: ROUTES.WEATHER },
    { label: t("nav.apmcPrice"), path: ROUTES.APMC },
    { label: t("nav.schemes"), path: ROUTES.SCHEMES },
  ];

  const toggleMobile = useCallback(() => {
    setMobileMenuOpen((prev) => !prev);
  }, []);

  const closeMobile = useCallback(() => {
    setMobileMenuOpen(false);
  }, []);

  const handleLogout = useCallback(() => {
    logout();
    navigate(ROUTES.HOME);
    closeMobile();
  }, [logout, navigate, closeMobile]);

  return (
    <header
      className="sticky top-0 z-30 border-b border-neutral-200 bg-white/95 backdrop-blur-sm transition-colors duration-300"
      id="app-header"
    >
      <div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
        <div className="flex h-16 items-center justify-between">
          {/* Logo */}
          <Link
            to={ROUTES.HOME}
            className="flex items-center gap-2 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 rounded-lg"
            aria-label={`${APP_NAME} home`}
          >
            <span className="inline-flex items-center justify-center h-8 w-8 rounded-lg bg-gradient-header text-white font-display font-bold text-sm shadow-sm">
              KN
            </span>
            <span className="text-lg font-display font-bold text-primary-700 hidden sm:block">
              {APP_NAME}
            </span>
          </Link>

          {/* Desktop Navigation */}
          <nav
            className="hidden md:flex items-center gap-1"
            aria-label="Main navigation"
            id="main-nav"
          >
            {NAV_ITEMS.map((item) => {
              const isActive = location.pathname === item.path;
              return (
                <Link
                  key={item.path}
                  to={item.path}
                  className={[
                    "px-3 py-2 rounded-lg text-sm font-medium transition-colors",
                    "focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500",
                    isActive
                      ? "bg-primary-50 text-primary-700"
                      : "text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100",
                  ].join(" ")}
                  aria-current={isActive ? "page" : undefined}
                >
                  {item.label}
                </Link>
              );
            })}
          </nav>

          {/* Right actions */}
          <div className="flex items-center gap-1.5">
            {/* Language selector */}
            {onLanguageChange && (
              <LanguageSelector
                currentLanguage={language}
                onLanguageChange={onLanguageChange}
              />
            )}

            {/* User Dashboard / Profile / Login */}
            <div className="hidden md:flex items-center gap-1.5">
              {isAuthenticated && user ? (
                <>
                  <Link
                    to={ROUTES.DASHBOARD}
                    className={[
                      "inline-flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500",
                      location.pathname === ROUTES.DASHBOARD
                        ? "bg-primary-50 text-primary-700"
                        : "text-neutral-600 hover:bg-neutral-100",
                    ].join(" ")}
                  >
                    <Squares2X2Icon className="h-5 w-5" aria-hidden="true" />
                    {t("nav.dashboard")}
                  </Link>
                  <Link
                    to={ROUTES.PROFILE}
                    className={[
                      "inline-flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500",
                      location.pathname === ROUTES.PROFILE
                        ? "bg-primary-50 text-primary-700"
                        : "text-neutral-600 hover:bg-neutral-100",
                    ].join(" ")}
                    title={t("nav.profile")}
                  >
                    <UserCircleIcon className="h-5 w-5" aria-hidden="true" />
                    <span className="max-w-[100px] truncate">{user.name?.split(" ")[0]}</span>
                  </Link>
                  <button
                    type="button"
                    onClick={handleLogout}
                    className="inline-flex items-center justify-center rounded-lg p-2 text-neutral-500 hover:bg-neutral-100 hover:text-neutral-700 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500"
                    aria-label={t("nav.logout")}
                    title={t("nav.logout")}
                  >
                    <ArrowRightOnRectangleIcon className="h-5 w-5" aria-hidden="true" />
                  </button>
                </>
              ) : (
                <Link
                  to={ROUTES.LOGIN}
                  className="inline-flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm font-medium bg-primary-600 text-white hover:bg-primary-700 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500"
                >
                  {t("nav.login")}
                </Link>
              )}
            </div>

            {/* Mobile menu button */}
            <button
              type="button"
              onClick={toggleMobile}
              className="md:hidden inline-flex items-center justify-center rounded-lg p-2 text-neutral-600 hover:bg-neutral-100 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500"
              aria-expanded={mobileMenuOpen}
              aria-controls="mobile-menu"
              aria-label={t("header.toggleMenu")}
            >
              {mobileMenuOpen ? (
                <XMarkIcon className="h-6 w-6" aria-hidden="true" />
              ) : (
                <Bars3Icon className="h-6 w-6" aria-hidden="true" />
              )}
            </button>
          </div>
        </div>
      </div>

      {/* Mobile Navigation */}
      {mobileMenuOpen && (
        <nav
          id="mobile-menu"
          className="md:hidden border-t border-neutral-200 bg-white animate-slide-down"
          aria-label="Mobile navigation"
        >
          <div className="px-4 py-3 space-y-1">
            {NAV_ITEMS.map((item) => {
              const isActive = location.pathname === item.path;
              return (
                <Link
                  key={item.path}
                  to={item.path}
                  onClick={closeMobile}
                  className={[
                    "block px-3 py-2 rounded-lg text-sm font-medium transition-colors",
                    isActive
                      ? "bg-primary-50 text-primary-700"
                      : "text-neutral-600 hover:bg-neutral-100",
                  ].join(" ")}
                  aria-current={isActive ? "page" : undefined}
                >
                  {item.label}
                </Link>
              );
            })}

            {/* Mobile Auth Links */}
            <div className="border-t border-neutral-200 mt-2 pt-2">
              {isAuthenticated && user ? (
                <>
                  <Link
                    to={ROUTES.DASHBOARD}
                    onClick={closeMobile}
                    className={[
                      "flex items-center gap-2 px-3 py-2 rounded-lg text-sm font-medium transition-colors",
                      location.pathname === ROUTES.DASHBOARD
                        ? "bg-primary-50 text-primary-700"
                        : "text-neutral-600 hover:bg-neutral-100",
                    ].join(" ")}
                  >
                    <Squares2X2Icon className="h-5 w-5" />
                    {t("nav.dashboard")}
                  </Link>
                  <Link
                    to={ROUTES.PROFILE}
                    onClick={closeMobile}
                    className={[
                      "flex items-center gap-2 px-3 py-2 rounded-lg text-sm font-medium transition-colors",
                      location.pathname === ROUTES.PROFILE
                        ? "bg-primary-50 text-primary-700"
                        : "text-neutral-600 hover:bg-neutral-100",
                    ].join(" ")}
                  >
                    <UserCircleIcon className="h-5 w-5" />
                    {t("nav.profile")}
                  </Link>
                  <button
                    type="button"
                    onClick={handleLogout}
                    className="flex items-center gap-2 w-full px-3 py-2 rounded-lg text-sm font-medium text-neutral-600 hover:bg-neutral-100 transition-colors"
                  >
                    <ArrowRightOnRectangleIcon className="h-5 w-5" />
                    {t("nav.logout")}
                  </button>
                </>
              ) : (
                <Link
                  to={ROUTES.LOGIN}
                  onClick={closeMobile}
                  className="block px-3 py-2 rounded-lg text-sm font-medium bg-primary-600 text-white text-center hover:bg-primary-700 transition-colors"
                >
                  {t("nav.login")}
                </Link>
              )}
            </div>
          </div>
        </nav>
      )}
    </header>
  );
}

Header.propTypes = {
  language: PropTypes.string,
  onLanguageChange: PropTypes.func,
};

export default Header;