'use client'; import Image from 'next/image'; import Link from 'next/link'; import { useEffect, useRef, useState } from 'react'; import { FiSearch, FiUser } from 'react-icons/fi'; import { HiShoppingBag } from 'react-icons/hi2'; // import logoWhite from '/public/images/logo-white.svg'; import logoBlack from '/public/images/logo-black.svg'; import styles from './navbar.module.css'; import { useCustomer } from '@/providers/customer'; // components/Navbar.tsx export function Navbar() { const { customer, logout } = useCustomer(); const prevScrollY = useRef(0); const [navBarStyle, setNavBarStyle] = useState('initial'); const [isVisible, setIsVisible] = useState('false'); const [isExpanded, setIsExpanded] = useState(false); // New piece of state for the profile dropdown const [isProfileDropdownOpen, setIsProfileDropdownOpen] = useState(false); const dropdownRef = useRef(null); useEffect(() => { const handleScroll = () => { const currentScrollY = window.scrollY; if (currentScrollY > prevScrollY.current && currentScrollY > 100) { // Scrolling down and scrolled past 100px setNavBarStyle('initial'); } else if (currentScrollY < prevScrollY.current && currentScrollY > 100) { // Scrolling up and scrolled past 100px setNavBarStyle('scrolledUp'); } else if (currentScrollY <= 100) { setNavBarStyle('initial'); } prevScrollY.current = currentScrollY; }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, []); // Close dropdown if you click outside of it useEffect(() => { function handleClickOutside(event: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsProfileDropdownOpen(false); } } document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); // const navClasses = `${styles.navBar} ${ // navBarStyle === 'scrolledUp' // ? styles.navBarScrolledUp // : styles.navBarInitial // }`; //Hack for now const navClasses = `${styles.navBar} ${ navBarStyle === 'scrolledUp' ? styles.navBarScrolledUp : styles.navBarScrolledUp }`; function toggleMenu() { if (isVisible === 'true') { setIsVisible('false'); setIsExpanded(false); } else { setIsVisible('true'); setIsExpanded(true); } } // You can define your logout function here or in the customer provider const handleLogout = () => { logout(); console.log('User logged out'); // Optionally redirect or handle client-side state cleanup }; return (
); }