From 08efce4e343066b5deb4c46e37061f4c6a652ce4 Mon Sep 17 00:00:00 2001 From: Arman Date: Sat, 19 Oct 2024 15:59:04 -0400 Subject: [PATCH] Autofill search bar based on path --- src/components/search-dropdown.tsx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/components/search-dropdown.tsx b/src/components/search-dropdown.tsx index 829b8ce..ebac77c 100644 --- a/src/components/search-dropdown.tsx +++ b/src/components/search-dropdown.tsx @@ -9,6 +9,7 @@ import { cn } from "@/lib/utils"; import { Product } from "../db/schema"; import { searchProducts } from "../lib/actions"; import Link from "next/link"; +import { useParams } from "next/navigation"; type SearchResult = Product & { href: string }; @@ -29,6 +30,16 @@ export function SearchDropdownComponent() { search(); }, [searchTerm]); + const params = useParams(); + useEffect(() => { + if (!params.product) { + const subcategory = params.subcategory; + setSearchTerm( + typeof subcategory === "string" ? subcategory.replaceAll("-", " ") : "", + ); + } + }, [params]); + return (