Wire Meshkee catalog: products, blog, mega menu, and product search.
Catalog-only (no cart); search targets store items and products pages. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
cfa21cbee9
commit
8e14748b48
@@ -5,15 +5,16 @@ import Link from "next/link";
|
||||
import { ChevronDown, Menu, UserRound, X } from "lucide-react";
|
||||
import { Container } from "@/components/Container";
|
||||
import { Placeholder } from "@/components/Placeholder";
|
||||
import { HeaderSearch } from "@/components/layout/HeaderSearch";
|
||||
import { InstantSearch } from "@/components/layout/InstantSearch";
|
||||
import { MegaMenu } from "@/components/layout/MegaMenu";
|
||||
import { customerPortalUrl } from "@/lib/config";
|
||||
import { applianceGroups, categories, navLinks, site } from "@/lib/data";
|
||||
import { navLinks, site } from "@/lib/data";
|
||||
import type { NavCategory } from "@/lib/categories";
|
||||
|
||||
export function Header() {
|
||||
export function Header({ categories = [] }: { categories?: NavCategory[] }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [partsOpen, setPartsOpen] = useState(false);
|
||||
const [appliancesOpen, setAppliancesOpen] = useState(false);
|
||||
const [mobileParts, setMobileParts] = useState(false);
|
||||
const [megaOpen, setMegaOpen] = useState(false);
|
||||
const [mobileCats, setMobileCats] = useState(false);
|
||||
|
||||
return (
|
||||
<header className="sticky top-0 z-40 border-b border-line bg-white/95 backdrop-blur">
|
||||
@@ -38,83 +39,26 @@ export function Header() {
|
||||
|
||||
<div
|
||||
className="relative"
|
||||
onMouseEnter={() => setAppliancesOpen(true)}
|
||||
onMouseLeave={() => setAppliancesOpen(false)}
|
||||
onMouseEnter={() => setMegaOpen(true)}
|
||||
onMouseLeave={() => setMegaOpen(false)}
|
||||
>
|
||||
<button type="button" className="flex items-center gap-1 px-3 py-2 text-[14px] hover:text-brand">
|
||||
لوازم برقی
|
||||
<ChevronDown className="size-3.5" />
|
||||
</button>
|
||||
{appliancesOpen ? (
|
||||
<div className="absolute right-0 top-full w-[560px] border border-line bg-white p-6 shadow-[0_20px_50px_rgba(0,0,0,0.08)]">
|
||||
<div className="grid grid-cols-2 gap-8">
|
||||
{applianceGroups.map((group) => (
|
||||
<div key={group.title}>
|
||||
<p className="mb-3 border-r-2 border-brand pr-2 text-[13px] font-bold">{group.title}</p>
|
||||
<ul className="space-y-1.5 text-[13px] text-muted">
|
||||
{group.items.map((item) => (
|
||||
<li key={item}>
|
||||
<Link href="/#parts" className="hover:text-brand">
|
||||
{item}
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div
|
||||
className="relative"
|
||||
onMouseEnter={() => setPartsOpen(true)}
|
||||
onMouseLeave={() => setPartsOpen(false)}
|
||||
>
|
||||
<Link href="/#parts" className="flex items-center gap-1 px-3 py-2 text-[14px] hover:text-brand">
|
||||
قطعات
|
||||
<Link href="/products" className="flex items-center gap-1 px-3 py-2 text-[14px] hover:text-brand">
|
||||
محصولات
|
||||
<ChevronDown className="size-3.5" />
|
||||
</Link>
|
||||
{partsOpen ? (
|
||||
<div className="absolute right-0 top-full w-[720px] border border-line bg-white p-5 shadow-[0_20px_50px_rgba(0,0,0,0.08)]">
|
||||
<div className="grid grid-cols-4 gap-3">
|
||||
{categories.map((category) => (
|
||||
<Link key={category.id} href="/#parts" className="group flex items-center gap-2 p-1.5 hover:bg-paper">
|
||||
<Placeholder
|
||||
src={category.image}
|
||||
alt=""
|
||||
className="size-11 shrink-0 object-cover"
|
||||
/>
|
||||
<span className="min-w-0">
|
||||
<span className="block truncate text-[12px] font-medium leading-5 group-hover:text-brand">
|
||||
{category.title.replace("قطعات ", "")}
|
||||
</span>
|
||||
<span className="font-latin block text-[9px] tracking-[0.16em] text-muted uppercase">
|
||||
{category.english}
|
||||
</span>
|
||||
</span>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<MegaMenu open={megaOpen} categories={categories} />
|
||||
</div>
|
||||
|
||||
{navLinks
|
||||
.filter((link) => link.href !== "/" && link.href !== "/#parts")
|
||||
.filter((link) => link.href !== "/" && link.href !== "/products")
|
||||
.map((link) => (
|
||||
<Link
|
||||
key={link.href}
|
||||
href={link.href}
|
||||
className="px-3 py-2 text-[14px] hover:text-brand"
|
||||
>
|
||||
<Link key={link.href} href={link.href} className="px-3 py-2 text-[14px] hover:text-brand">
|
||||
{link.label}
|
||||
</Link>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
<HeaderSearch className="ms-auto hidden max-w-xs flex-1 md:block" />
|
||||
<InstantSearch className="ms-auto hidden max-w-xs flex-1 md:block" />
|
||||
|
||||
<a
|
||||
href={customerPortalUrl}
|
||||
@@ -139,30 +83,40 @@ export function Header() {
|
||||
</button>
|
||||
</div>
|
||||
<div className="flex flex-1 flex-col gap-1 overflow-y-auto p-4 text-[15px]">
|
||||
<HeaderSearch className="mb-3" />
|
||||
<InstantSearch compact className="mb-3 w-full" />
|
||||
{navLinks.map((link) => (
|
||||
<Link key={link.href} href={link.href} className="py-2" onClick={() => setOpen(false)}>
|
||||
{link.label}
|
||||
</Link>
|
||||
))}
|
||||
<Link href="/products" className="py-2" onClick={() => setOpen(false)}>
|
||||
محصولات
|
||||
</Link>
|
||||
<button
|
||||
type="button"
|
||||
className="flex items-center justify-between py-2"
|
||||
onClick={() => setMobileParts((value) => !value)}
|
||||
onClick={() => setMobileCats((value) => !value)}
|
||||
>
|
||||
دستهبندی قطعات
|
||||
<ChevronDown className={`size-4 transition ${mobileParts ? "rotate-180" : ""}`} />
|
||||
دستهبندیها
|
||||
<ChevronDown className={`size-4 transition ${mobileCats ? "rotate-180" : ""}`} />
|
||||
</button>
|
||||
{mobileParts
|
||||
{mobileCats
|
||||
? categories.map((category) => (
|
||||
<Link
|
||||
key={category.id}
|
||||
href="/#parts"
|
||||
className="py-1.5 pe-2 text-[13px] text-muted"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
{category.title}
|
||||
</Link>
|
||||
<div key={category.id} className="space-y-1 pe-2">
|
||||
<Link href={category.href} className="block py-1.5 text-[13px] font-medium" onClick={() => setOpen(false)}>
|
||||
{category.title}
|
||||
</Link>
|
||||
{category.children.map((child) => (
|
||||
<Link
|
||||
key={child.id}
|
||||
href={child.href}
|
||||
className="block py-1 text-[12px] text-muted"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
{child.title}
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
))
|
||||
: null}
|
||||
<a href={customerPortalUrl} className="mt-4 rounded-xl bg-brand px-4 py-3 text-center text-white">
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { FormEvent } from "react";
|
||||
import { Search } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export function HeaderSearch({ className = "" }: { className?: string }) {
|
||||
const router = useRouter();
|
||||
|
||||
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
router.push("/#parts");
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={onSubmit} className={`relative ${className}`}>
|
||||
<input
|
||||
name="q"
|
||||
placeholder="جستجوی قطعه..."
|
||||
className="h-11 w-full rounded-xl bg-paper pe-4 ps-10 text-[13px] outline-none placeholder:text-muted/70 focus:bg-white focus:ring-1 focus:ring-brand"
|
||||
/>
|
||||
<Search className="pointer-events-none absolute top-1/2 right-3 size-4 -translate-y-1/2 text-muted" />
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
"use client";
|
||||
|
||||
import { FormEvent, useEffect, useRef, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Search } from "lucide-react";
|
||||
import { Placeholder } from "@/components/Placeholder";
|
||||
import { formatPrice } from "@/lib/format";
|
||||
import { variantTitle, type StoreVariant } from "@/lib/store";
|
||||
|
||||
export function InstantSearch({ compact = false, className = "" }: { compact?: boolean; className?: string }) {
|
||||
const router = useRouter();
|
||||
const [query, setQuery] = useState("");
|
||||
const [items, setItems] = useState<StoreVariant[]>([]);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const box = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const onClick = (event: MouseEvent) => {
|
||||
if (!box.current?.contains(event.target as Node)) setOpen(false);
|
||||
};
|
||||
document.addEventListener("mousedown", onClick);
|
||||
return () => document.removeEventListener("mousedown", onClick);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const value = query.trim();
|
||||
if (value.length <= 2) {
|
||||
setItems([]);
|
||||
setOpen(false);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const timer = setTimeout(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await fetch(`/api/search?q=${encodeURIComponent(value)}`);
|
||||
const data = (await response.json()) as { items?: StoreVariant[] };
|
||||
setItems(data.items ?? []);
|
||||
setOpen(true);
|
||||
} catch {
|
||||
setItems([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, 250);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [query]);
|
||||
|
||||
const goToProducts = (event?: FormEvent) => {
|
||||
event?.preventDefault();
|
||||
const value = query.trim();
|
||||
if (value.length <= 2) return;
|
||||
setOpen(false);
|
||||
router.push(`/products?q=${encodeURIComponent(value)}`);
|
||||
};
|
||||
|
||||
return (
|
||||
<div ref={box} className={`relative ${className || (compact ? "w-full" : "hidden min-w-0 flex-1 md:block")}`}>
|
||||
<form onSubmit={goToProducts}>
|
||||
<input
|
||||
type="search"
|
||||
value={query}
|
||||
onChange={(event) => setQuery(event.target.value)}
|
||||
onFocus={() => {
|
||||
if (query.trim().length > 2) setOpen(true);
|
||||
}}
|
||||
placeholder={compact ? "جستجوی محصول..." : "جستجوی محصول و قطعه..."}
|
||||
className="h-11 w-full rounded-xl bg-paper pe-4 ps-10 text-[13px] outline-none placeholder:text-muted/70 focus:bg-white focus:ring-1 focus:ring-brand"
|
||||
/>
|
||||
<Search className="pointer-events-none absolute top-1/2 right-3 size-4 -translate-y-1/2 text-muted" />
|
||||
</form>
|
||||
|
||||
{open ? (
|
||||
<div className="absolute inset-x-0 top-[calc(100%+8px)] z-50 overflow-hidden rounded-2xl border border-line bg-white shadow-[0_16px_40px_rgba(0,0,0,0.1)]">
|
||||
{loading ? (
|
||||
<p className="px-4 py-3 text-[13px] text-muted">در حال جستجو...</p>
|
||||
) : items.length === 0 ? (
|
||||
<p className="px-4 py-3 text-[13px] text-muted">نتیجهای پیدا نشد.</p>
|
||||
) : (
|
||||
<>
|
||||
<ul className="max-h-[360px] overflow-y-auto py-2">
|
||||
{items.map((item) => {
|
||||
const price = item.discountedPrice ?? item.price;
|
||||
return (
|
||||
<li key={item.id}>
|
||||
<Link
|
||||
href={`/products/${encodeURIComponent(item.id)}`}
|
||||
className="flex items-center gap-3 px-3 py-2 hover:bg-paper"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
<span className="grid size-12 shrink-0 overflow-hidden rounded-lg bg-paper">
|
||||
<Placeholder
|
||||
src={item.productImage || "/placeholders/product.svg"}
|
||||
className="size-full object-contain"
|
||||
/>
|
||||
</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="line-clamp-1 text-[13px] text-ink">{variantTitle(item)}</span>
|
||||
<span className="mt-0.5 block text-[12px] text-muted">
|
||||
{price != null ? `${formatPrice(price)} تومان` : "تماس بگیرید"}
|
||||
</span>
|
||||
</span>
|
||||
</Link>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => goToProducts()}
|
||||
className="block w-full border-t border-line px-4 py-3 text-center text-[13px] font-medium text-brand hover:bg-paper"
|
||||
>
|
||||
مشاهده همه نتایج در محصولات
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { Placeholder } from "@/components/Placeholder";
|
||||
import type { NavCategory } from "@/lib/categories";
|
||||
|
||||
export function MegaMenu({ open, categories }: { open: boolean; categories: NavCategory[] }) {
|
||||
if (!open) return null;
|
||||
|
||||
return (
|
||||
<div className="absolute top-full right-0 z-50 mt-1 w-[min(920px,calc(100vw-2rem))] border border-line bg-white p-5 shadow-[0_20px_50px_rgba(0,0,0,0.08)]">
|
||||
{categories.length === 0 ? (
|
||||
<p className="text-[13px] text-muted">دستهبندیها بهزودی اضافه میشوند.</p>
|
||||
) : (
|
||||
<div className="grid grid-cols-2 gap-5 md:grid-cols-3 lg:grid-cols-4">
|
||||
{categories.map((category) => (
|
||||
<div key={category.id}>
|
||||
<Link href={category.href} className="mb-3 flex items-center gap-2 text-[13.5px] font-semibold text-ink hover:text-brand">
|
||||
<span className="grid size-9 place-items-center overflow-hidden rounded-xl bg-paper">
|
||||
<Placeholder src={category.image || "/placeholders/category.svg"} className="size-full object-cover" />
|
||||
</span>
|
||||
{category.title}
|
||||
</Link>
|
||||
{category.children.length ? (
|
||||
<ul className="space-y-1.5 pr-1">
|
||||
{category.children.map((child) => (
|
||||
<li key={child.id}>
|
||||
<Link
|
||||
href={child.href}
|
||||
className="block rounded-lg px-2 py-1 text-[12.5px] text-muted transition hover:bg-paper hover:text-brand"
|
||||
>
|
||||
{child.title}
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user