import { Link, useLocation, useNavigate } from "@remix-run/react"; import { useState, useRef, useEffect, type FormEvent } from "react"; import { Search, Heart, ShoppingBag, User, Store } from "lucide-react"; import { useRootData } from "~/hooks/use-root-data"; import { useCartCount } from "~/requestHandler/use-cart-hooks"; import { useMyStores } from "~/requestHandler/use-seller-hooks"; import { useSearchAutocomplete } from "~/requestHandler/use-search-hooks"; import { useDebounce } from "~/hooks/useDebounce"; import { SunMoonToggle } from "~/components/SunMoonToggle"; import { AutocompleteSuggestionTypeEnum } from "../../../src/api/types"; import logo from "~/assets/logo/SVG-07.svg"; const NAV = [ { label: "خانه", to: "/" }, { label: "کاوش", to: "/explore" }, { label: "برندها", to: "/sellers" }, { label: "کالکشن‌ها", to: "/collections" }, ]; /** * Desktop-only top chrome (sticky header with brand, nav, search and actions). * Hidden below the `lg` breakpoint; the mobile experience is unchanged. */ const DesktopHeader = () => { const location = useLocation(); const navigate = useNavigate(); const { user, theme } = useRootData(); const cartCount = useCartCount(); const { data: myStores } = useMyStores(); // Mirror the mobile "ورود به فروشگاه من" flow: prefer a store the user owns, // fall back to the first store they staff. If they don't manage any store, // the CTA is hidden. const targetStore = myStores?.find((s) => s.role === "owner") ?? myStores?.[0]; const [q, setQ] = useState(""); const [open, setOpen] = useState(false); const searchRef = useRef(null); const isLoggedIn = !!user?.access_token; const debouncedQ = useDebounce(q.trim(), 400); const { data: autocompleteData, isLoading: isSuggesting } = useSearchAutocomplete(debouncedQ); const suggestions = autocompleteData?.suggestions ?? []; const showSuggestions = open && q.trim().length > 0; // Close the suggestions dropdown when clicking outside the search box. useEffect(() => { const onClickOutside = (e: MouseEvent) => { if (searchRef.current && !searchRef.current.contains(e.target as Node)) { setOpen(false); } }; document.addEventListener("mousedown", onClickOutside); return () => document.removeEventListener("mousedown", onClickOutside); }, []); const isActive = (to: string) => to === "/" ? location.pathname === "/" : location.pathname.startsWith(to); const goToSearch = (term: string) => { const t = term.trim(); if (!t) return; setOpen(false); navigate(`/search/${encodeURIComponent(t)}`); }; const onSearch = (e: FormEvent) => { e.preventDefault(); goToSearch(q); }; const onSelectSuggestion = (sug: { type: AutocompleteSuggestionTypeEnum; text?: string; name?: string; id?: string; }) => { if (sug.type === AutocompleteSuggestionTypeEnum.Seller && sug.id) { setOpen(false); navigate(`/seller/${sug.id}`); return; } goToSearch(sug.text || sug.name || ""); }; return (
ویترون ویترون
{ setQ(e.target.value); setOpen(true); }} onFocus={() => setOpen(true)} onKeyDown={(e) => { if (e.key === "Escape") setOpen(false); }} name="q" autoComplete="off" placeholder="جستجوی محصول، برند یا فروشگاه…" aria-label="جستجو" className="w-full bg-transparent outline-none text-[14.5px] text-BLACK placeholder:text-GRAY" /> {showSuggestions ? (
{isSuggesting && suggestions.length === 0 ? (

در حال جستجو...

) : suggestions.length > 0 ? ( suggestions.map((sug, index) => { const isSeller = sug.type === AutocompleteSuggestionTypeEnum.Seller; const label = isSeller ? sug.name : sug.text; if (!label) return null; return ( ); }) ) : ( )}
) : null}
{isLoggedIn ? ( <> {targetStore ? ( فروشگاه من ) : null} {cartCount > 0 ? ( {cartCount} ) : null} ) : ( ورود / ثبت نام )}
); }; export default DesktopHeader;