added real time search
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Header.tsx
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { useState, useEffect, useCallback, Suspense } from "react";
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { X, Search } from "lucide-react";
|
||||
@@ -103,12 +103,14 @@ export default function Header({ locale = "ru" }: HeaderProps) {
|
||||
</div> */}
|
||||
|
||||
{/* Desktop Search Bar */}
|
||||
<SearchBar
|
||||
isMobile={false}
|
||||
searchPlaceholder={t("common.search")}
|
||||
className="hidden flex-1 md:flex "
|
||||
locale={locale}
|
||||
/>
|
||||
<Suspense fallback={<div className="hidden flex-1 md:flex h-11 bg-gray-100 rounded-lg" />}>
|
||||
<SearchBar
|
||||
isMobile={false}
|
||||
searchPlaceholder={t("common.search")}
|
||||
className="hidden flex-1 md:flex "
|
||||
locale={locale}
|
||||
/>
|
||||
</Suspense>
|
||||
|
||||
{/* Action Buttons */}
|
||||
<ActionButtons
|
||||
@@ -121,13 +123,15 @@ export default function Header({ locale = "ru" }: HeaderProps) {
|
||||
|
||||
<CategoryMenu isOpen={isCategoryOpen} onClose={closeCategoryMenu} />
|
||||
|
||||
<SearchBar
|
||||
isMobile={true}
|
||||
isOpen={isMobileSearchOpen}
|
||||
onClose={() => setIsMobileSearchOpen(false)}
|
||||
searchPlaceholder={t("common.search")}
|
||||
locale={locale}
|
||||
/>
|
||||
<Suspense fallback={null}>
|
||||
<SearchBar
|
||||
isMobile={true}
|
||||
isOpen={isMobileSearchOpen}
|
||||
onClose={() => setIsMobileSearchOpen(false)}
|
||||
searchPlaceholder={t("common.search")}
|
||||
locale={locale}
|
||||
/>
|
||||
</Suspense>
|
||||
|
||||
<AuthDialog isOpen={isLoginOpen} onClose={() => setIsLoginOpen(false)} />
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useRouter, useSearchParams, usePathname } from "next/navigation";
|
||||
import { SearchIcon } from "@/components/icons";
|
||||
|
||||
interface SearchBarProps {
|
||||
@@ -31,8 +31,40 @@ export default function SearchBar({
|
||||
locale = "ru",
|
||||
}: SearchBarProps) {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const pathname = usePathname();
|
||||
const [searchValue, setSearchValue] = useState("");
|
||||
|
||||
React.useEffect(() => {
|
||||
// Sync URL param to input when URL changes
|
||||
if (pathname?.includes("/search")) {
|
||||
const q = searchParams?.get("q") || "";
|
||||
if (q !== searchValue.trim()) {
|
||||
setSearchValue(q);
|
||||
}
|
||||
} else {
|
||||
setSearchValue("");
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [searchParams, pathname]);
|
||||
|
||||
React.useEffect(() => {
|
||||
// Debounce updates to the URL query when typing on search page
|
||||
if (pathname?.includes("/search")) {
|
||||
const delayDebounceFn = setTimeout(() => {
|
||||
const q = searchParams?.get("q") || "";
|
||||
const curVal = searchValue.trim();
|
||||
if (curVal !== q && curVal !== "") {
|
||||
router.push(`/${locale}/search?q=${encodeURIComponent(curVal)}`);
|
||||
} else if (curVal === "" && q !== "") {
|
||||
router.push(`/${locale}/search`);
|
||||
}
|
||||
}, 500);
|
||||
|
||||
return () => clearTimeout(delayDebounceFn);
|
||||
}
|
||||
}, [searchValue, pathname, router, locale, searchParams]);
|
||||
|
||||
const performSearch = () => {
|
||||
if (searchValue.trim()) {
|
||||
router.push(
|
||||
|
||||
Reference in New Issue
Block a user