File size: 12,333 Bytes
5c05829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React, { useState, useEffect, useRef } from 'react';
import { motion } from 'framer-motion';
import { ShoppingCart, User, X, Menu as MenuIcon, Home, Package, Phone, ChevronDown, Leaf, Utensils as SpicesIcon, Box, Star, LogOut, Info } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { Link, useNavigate, useLocation } from 'react-router-dom';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  DropdownMenuSeparator
} from "@/components/ui/dropdown-menu";
import { supabase } from '@/lib/supabaseClient';
import { useToast } from "@/components/ui/use-toast";

const Header = ({ logoUrl, cartItemCount, onCategorySelect, headerHeight, session, setSession }) => {
  const [isMenuOpen, setIsMenuOpen] = useState(false);
  const navigate = useNavigate();
  const location = useLocation();
  const menuRef = useRef(null);
  const triggerRef = useRef(null);
  const { toast } = useToast();

  const handleMenuLinkClick = (path, sectionId) => {
    setIsMenuOpen(false);
    if (path) {
      navigate(path);
    }
    if (sectionId) {
      setTimeout(() => {
        const section = document.getElementById(sectionId);
        if (section) {
          section.scrollIntoView({ behavior: 'smooth' });
        } else if (path === '/') { 
          navigate('/'); 
          setTimeout(() => {
            const homeSection = document.getElementById(sectionId);
            if (homeSection) homeSection.scrollIntoView({ behavior: 'smooth' });
          }, 150);
        }
      }, 100); 
    }
  };
  
  useEffect(() => {
    const handleClickOutside = (event) => {
      if (menuRef.current && !menuRef.current.contains(event.target) && triggerRef.current && !triggerRef.current.contains(event.target)) {
        setIsMenuOpen(false);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => document.removeEventListener("mousedown", handleClickOutside);
  }, [menuRef, triggerRef]);

  const handleCategoryClick = (category) => {
    onCategorySelect(category);
    setIsMenuOpen(false);
    if (location.pathname !== '/products') {
      navigate('/products');
    }
  };

  const handleLogout = async () => {
    setIsMenuOpen(false);
    const { error } = await supabase.auth.signOut();
    if (error) {
      toast({
        title: "Logout Failed",
        description: error.message,
        variant: "destructive",
        className: "toast-destructive-theme",
      });
    } else {
      setSession(null);
      toast({
        title: "👋 Logged Out",
        description: "You have been successfully logged out.",
        className: "toast-info-theme",
      });
      navigate('/');
    }
  };

  const menuItems = [
    { label: "Home", path: "/", icon: <Home size={22} className="mr-3 text-amber-400 md:size-6" />, action: () => handleMenuLinkClick('/') },
    { label: "About Us", path: "/about-us", icon: <Info size={22} className="mr-3 text-amber-400 md:size-6" />, action: () => handleMenuLinkClick('/about-us') },
    { label: "Contact Us", path: "/contact", icon: <Phone size={22} className="mr-3 text-amber-400 md:size-6" />, action: () => handleMenuLinkClick('/contact') },
  ];

  const productCategories = [
    { label: "Spices", icon: <SpicesIcon size={20} className="mr-2 text-amber-300 md:size-5" />, action: () => handleCategoryClick('Spices') },
    { label: "Dry Fruits", icon: <Leaf size={20} className="mr-2 text-amber-300 md:size-5" />, action: () => handleCategoryClick('Dry Fruits') },
    { label: "Taste Rider Specials", icon: <Star size={20} className="mr-2 text-amber-300 md:size-5" />, action: () => handleCategoryClick('Taste Rider Specials') },
  ];
  
  return (
    <>
      <header 
        className="px-2 md:px-6 fixed left-0 right-0 z-40 bg-stone-800/90 backdrop-blur-lg shadow-xl"
        style={{ top: `0px`, height: `${headerHeight}px` }} 
      >
        {/* Trust badges banner removed */}

        <div className="container mx-auto flex justify-between items-center relative h-full">
          
          <div className="flex items-center flex-1 justify-start">
            <div className="sm:hidden">
              <DropdownMenu open={isMenuOpen} onOpenChange={setIsMenuOpen}>
                <DropdownMenuTrigger asChild ref={triggerRef}>
                  <Button 
                    variant="ghost" 
                    size="icon" 
                    className="text-amber-100 hover:bg-white/20 p-2.5" 
                  >
                    {isMenuOpen ? <X className="h-8 w-8" /> : <MenuIcon className="h-8 w-8" />}
                  </Button>
                </DropdownMenuTrigger>
                <DropdownMenuContent 
                  ref={menuRef}
                  className="bg-stone-800/95 border-stone-700 text-amber-100 shadow-2xl rounded-md w-64 sm:w-72"
                  style={{
                    maxHeight: 'calc(80vh - 70px)',
                    zIndex: 50
                  }}
                  sideOffset={0} // align directly below the icon
                  align="start"
                >
                  <div className="p-2 space-y-1 overflow-y-auto">
                    {menuItems.map(item => (
                      <DropdownMenuItem 
                        key={item.label}
                        onClick={item.action}
                        className="hover:bg-stone-700 focus:bg-stone-700 py-3 text-lg flex items-center cursor-pointer rounded-md transition-all duration-300 hover:text-amber-300 hover:tracking-wide"
                      >
                        {item.icon} {item.label}
                      </DropdownMenuItem>
                    ))}
                    <Separator className="bg-stone-700 my-2" />
                    {session ? (
                      <>
                        <DropdownMenuItem asChild>
                          <Link to="/profile" onClick={() => setIsMenuOpen(false)} className="hover:bg-stone-700 focus:bg-stone-700 py-3 text-lg flex items-center w-full cursor-pointer rounded-md">
                            <User className="mr-3 h-5 w-5 text-amber-400" /> My Profile
                          </Link>
                        </DropdownMenuItem>
                        <DropdownMenuItem
                          onClick={handleLogout}
                          className="hover:bg-stone-700 focus:bg-stone-700 py-3 text-lg flex items-center w-full cursor-pointer rounded-md text-red-400 hover:text-red-300"
                        >
                          <LogOut className="mr-3 h-5 w-5" /> Logout
                        </DropdownMenuItem>
                      </>
                    ) : (
                      <DropdownMenuItem asChild>
                        <Link to="/login" onClick={() => setIsMenuOpen(false)} className="hover:bg-stone-700 focus:bg-stone-700 py-3 text-lg flex items-center w-full cursor-pointer rounded-md">
                          <User className="mr-3 h-5 w-5 text-amber-400" /> Login
                        </Link>
                      </DropdownMenuItem>
                    )}
                  </div>
                </DropdownMenuContent>
              </DropdownMenu>
            </div>

            <div className="hidden sm:flex items-center space-x-1 md:space-x-2">
              {menuItems.map(item => (
                <Button 
                    key={item.label}
                    onClick={item.action}
                    variant="ghost"
                    className="text-amber-100 hover:bg-white/10 hover:text-amber-300 hover:tracking-wide transition-all duration-300 px-3.5 py-2.5 text-base md:text-lg"
                >
                    <item.icon.type {...item.icon.props} size={20} className="mr-2 md:size-6" /> {item.label}
                </Button>
              ))}
            </div>
          </div>

          <div className="absolute left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2">
            <Link
              to="/"
              className="flex items-center"
              onClick={(e) => {
                if (location.pathname === '/') {
                  e.preventDefault();
                  window.scrollTo({ top: 0, behavior: 'smooth' });
                }
              }}
            >
              <img src={logoUrl} alt="Taste Rider Logo" className="h-16 sm:h-20 md:h-24 transition-transform hover:scale-105" />
            </Link>
          </div>
          
          <div className="flex items-center flex-1 justify-end">
            <div className="hidden sm:flex items-center space-x-1 md:space-x-1.5">
              {/* Products button now navigates directly to categories */}
              <Button
                variant="ghost"
                className="text-amber-100 hover:bg-white/10 hover:text-amber-50 transition-colors px-3.5 py-2.5 text-base md:text-lg"
                onClick={() => navigate('/product-categories')}
              >
                <Package size={20} className="mr-2 md:size-6" /> Products
              </Button>
              {/* Wishlist button for desktop - removed */}

              <Link to="/cart">
                <Button 
                  variant="ghost" 
                  size="icon"
                  className="text-amber-100 hover:bg-white/20 transition-all duration-300 transform hover:scale-110 relative p-2.5"
                >
                  <ShoppingCart className="h-6 w-6 md:h-7 md:w-7" />
                  {cartItemCount > 0 && (
                    <span className="absolute -top-1.5 -right-1.5 bg-red-600 text-white text-sm font-bold rounded-full h-5 w-5 md:h-6 md:w-6 flex items-center justify-center text-[11px] md:text-xs">
                      {cartItemCount}
                    </span>
                  )}
                </Button>
              </Link>
              {session ? (
                <>
                  <Link to="/profile">
                    <Button 
                      variant="ghost" 
                      size="icon"
                      className="text-amber-100 hover:bg-white/20 transition-all duration-300 transform hover:scale-110 p-2.5"
                    >
                      <User className="h-6 w-6 md:h-7 md:w-7" />
                    </Button>
                  </Link>
                  <Button 
                    variant="ghost" 
                    size="icon"
                    onClick={handleLogout}
                    className="text-red-400 hover:bg-red-500/20 hover:text-red-300 transition-all duration-300 transform hover:scale-110 p-2.5"
                  >
                    <LogOut className="h-6 w-6 md:h-7 md:w-7" />
                  </Button>
                </>
              ) : (
                <Link to="/login">
                  <Button 
                    variant="ghost" 
                    size="icon"
                    className="text-amber-100 hover:bg-white/20 transition-all duration-300 transform hover:scale-110 p-2.5"
                  >
                    <User className="h-6 w-6 md:h-7 md:w-7" />
                  </Button>
                </Link>
              )}
            </div>

            <div className="sm:hidden flex items-center space-x-1">
              <Link to="/product-categories">
                <Button
                  variant="ghost"
                  size="icon" 
                  className="text-amber-100 hover:bg-white/20 p-2.5" 
                >
                  <Box className="h-8 w-8" />
                </Button>
              </Link>
              <Link to="/cart">
                <Button 
                  variant="ghost" 
                  size="icon"
                  className="text-amber-100 hover:bg-white/20 transition-all duration-300 transform hover:scale-110 relative p-2.5"
                >
                  <ShoppingCart className="h-8 w-8" />
                  {cartItemCount > 0 && (
                    <span className="absolute top-0.5 right-0.5 bg-red-600 text-white text-xs font-bold rounded-full h-4 w-4 flex items-center justify-center text-[10px]">
                      {cartItemCount}
                    </span>
                  )}
                </Button>
              </Link>
            </div>
          </div>
        </div>
      </header>
    </>
  );
};

export default Header;