From 344a42c680e8236294fdfc0b95d7d717c5218ff6 Mon Sep 17 00:00:00 2001 From: Ethan Niser Date: Sun, 20 Oct 2024 17:00:39 -0400 Subject: [PATCH 1/2] add transition to search --- src/components/search-dropdown.tsx | 101 ++++++++++++++++------------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/src/components/search-dropdown.tsx b/src/components/search-dropdown.tsx index d1a6dba..60a6216 100644 --- a/src/components/search-dropdown.tsx +++ b/src/components/search-dropdown.tsx @@ -1,9 +1,9 @@ "use client"; -import { useEffect, useState, useRef } from "react"; +import { useEffect, useState, useRef, useTransition } from "react"; import { Input } from "@/components/ui/input"; import { ScrollArea } from "@/components/ui/scroll-area"; -import { Search, X } from "lucide-react"; +import { Search, X, Loader2 } from "lucide-react"; import Image from "next/image"; import { cn } from "@/lib/utils"; import { Product } from "../db/schema"; @@ -15,22 +15,26 @@ type SearchResult = Product & { href: string }; export function SearchDropdownComponent() { const [searchTerm, setSearchTerm] = useState(""); + const [committedSearchTerm, setCommittedSearchTerm] = useState(""); const [filteredItems, setFilteredItems] = useState([]); const [isOpen, setIsOpen] = useState(false); const [highlightedIndex, setHighlightedIndex] = useState(-1); + const [isPending, startTransition] = useTransition(); const router = useRouter(); const inputRef = useRef(null); useEffect(() => { - const search = async () => { - if (searchTerm.length === 0) setFilteredItems([]); - else { - const results = await searchProducts(searchTerm); - setFilteredItems(results); - } - }; - - search(); + if (searchTerm.length === 0) { + setFilteredItems([]); + setCommittedSearchTerm(""); + } else { + startTransition(() => { + searchProducts(searchTerm).then((results) => { + setFilteredItems(results); + setCommittedSearchTerm(searchTerm); + }); + }); + } }, [searchTerm]); const params = useParams(); @@ -77,7 +81,11 @@ export function SearchDropdownComponent() { onKeyDown={handleKeyDown} className="font-sans font-medium sm:w-[300px] md:w-[375px]" /> - + {isPending ? ( + + ) : ( + + )} - {isOpen && filteredItems.length > 0 && ( -
- - {filteredItems.map((item, index) => ( - -
setHighlightedIndex(index)} - onClick={() => { - setSearchTerm(item.name); - setIsOpen(false); - inputRef.current?.blur(); // Close the keyboard on mobile - }} - > - - {item.name} -
- - ))} -
-
- )} + {isOpen && + filteredItems.length > 0 && + committedSearchTerm === searchTerm && ( +
+ + {filteredItems.map((item, index) => ( + +
setHighlightedIndex(index)} + onClick={() => { + setSearchTerm(item.name); + setIsOpen(false); + inputRef.current?.blur(); + }} + > + + {item.name} +
+ + ))} +
+
+ )} ); From 6b1a7fd79e10121ff302d81a024a7d28a8d7a51e Mon Sep 17 00:00:00 2001 From: Ethan Niser Date: Sun, 20 Oct 2024 17:01:37 -0400 Subject: [PATCH 2/2] formatting --- src/app/api/debug/route.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app/api/debug/route.ts b/src/app/api/debug/route.ts index 6bbd84a..3c972c6 100644 --- a/src/app/api/debug/route.ts +++ b/src/app/api/debug/route.ts @@ -1,17 +1,21 @@ -import { cookies } from "next/headers" +import { cookies } from "next/headers"; export async function GET(request: Request) { - // dump all the request headers, cookies, and body, infinite deep json stringify - const stringifyHeaders = JSON.stringify(Object.fromEntries(request.headers), null, 2) - const cookieStore = (await cookies()).getAll() - const stringifyCookies = JSON.stringify(cookieStore, null, 2) + // dump all the request headers, cookies, and body, infinite deep json stringify + const stringifyHeaders = JSON.stringify( + Object.fromEntries(request.headers), + null, + 2, + ); + const cookieStore = (await cookies()).getAll(); + const stringifyCookies = JSON.stringify(cookieStore, null, 2); - console.log("headers", stringifyHeaders) - console.log("cookies", stringifyCookies) - const responseObj = { - headers: stringifyHeaders, - cookies: stringifyCookies, - } - - return Response.json(responseObj) - } \ No newline at end of file + console.log("headers", stringifyHeaders); + console.log("cookies", stringifyCookies); + const responseObj = { + headers: stringifyHeaders, + cookies: stringifyCookies, + }; + + return Response.json(responseObj); +}