From 675b41faf61d120434e679b41817253217ac0a8b Mon Sep 17 00:00:00 2001 From: gitfromwildan <> Date: Thu, 29 May 2025 19:44:39 +0700 Subject: [PATCH] v.1.13.0 improve: search result --- components/search.tsx | 40 ++++++++++++++++++--- contents/docs/changelog/version-1/index.mdx | 2 ++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/components/search.tsx b/components/search.tsx index 49bd70b..17eea5f 100644 --- a/components/search.tsx +++ b/components/search.tsx @@ -17,6 +17,23 @@ import { import Anchor from "./anchor"; import { advanceSearch, cn } from "@/lib/utils"; import { ScrollArea } from "@/components/ui/scroll-area"; +import { page_routes } from "@/lib/routes-config"; + +// Define the ContextInfo type to match the one in routes-config +type ContextInfo = { + icon: string; + description: string; + title?: string; +}; + +type SearchResult = { + title: string; + href: string; + noLink?: boolean; + items?: undefined; + score?: number; + context?: ContextInfo; +}; export default function Search() { const router = useRouter(); @@ -39,10 +56,25 @@ export default function Search() { }; }, []); - const filteredResults = useMemo( - () => advanceSearch(searchedInput.trim()), - [searchedInput] - ); + const filteredResults = useMemo(() => { + const trimmedInput = searchedInput.trim(); + + // If search input is empty or less than 3 characters, show initial suggestions + if (trimmedInput.length < 3) { + return page_routes + .filter((route: { href: string }) => !route.href.endsWith('/')) // Filter out directory routes + .slice(0, 6) // Limit to 6 posts + .map((route: { title: string; href: string; noLink?: boolean; context?: ContextInfo }) => ({ + title: route.title, + href: route.href, + noLink: route.noLink, + context: route.context + })); + } + + // For search with 3 or more characters, use the advance search + return advanceSearch(trimmedInput) as unknown as SearchResult[]; + }, [searchedInput]); useEffect(() => { setSelectedIndex(0); diff --git a/contents/docs/changelog/version-1/index.mdx b/contents/docs/changelog/version-1/index.mdx index 8a84149..9506844 100644 --- a/contents/docs/changelog/version-1/index.mdx +++ b/contents/docs/changelog/version-1/index.mdx @@ -22,6 +22,8 @@ date: 24-05-2025 - improve docu.json with context menu - improve leftbar with context menu - improve docs-menu with context menu + - improve search dialog limit result to 6 post per suggestion + - improve search result typing 3 characters to show suggestion