import { ContentContainer, Page } from "@/components/utils/Layout"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Toaster } from "@/components/ui/toaster"; import { useToast } from "@/components/ui/use-toast"; import { Footer } from "@/components/utils/Footer"; import { NavBar } from "@/components/utils/NavBar"; import { SEO } from "@/components/utils/SEO"; import { Config } from "@/data/config"; import { isEmptyString } from "@/lib/utils"; import type { TSearchResultItem } from "@/types/search-result"; import axios from "axios"; import { isArray } from "lodash"; import { nanoid } from "nanoid"; import type { GetServerSideProps } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import { type ChangeEvent, type KeyboardEvent, useEffect, useState } from "react"; type SearchPageProps = { query: string | null }; export default function SearchPage(props: SearchPageProps) { const [searchText, setSearchText] = useState(props.query ?? ""); const [searchResult, setSearchResult] = useState([]); const [isLoading, setIsLoading] = useState(false); const { toast } = useToast(); const router = useRouter(); useEffect(() => { if (!isEmptyString(searchText)) { handleMakeSearch(); } }, []); const fetchSearchAPI = (param: string): Promise => { return axios.get(`/api/search/${param}`).then((response) => response.data); }; const handleInputSearchText = (event: ChangeEvent) => { setSearchText(event.target.value); }; const handleEnterKeySearch = (event: KeyboardEvent) => { if (event.key === "Go" || event.key === "Enter") { handleMakeSearch(); } }; const handleMakeSearch = () => { const searchQuery = searchText; if (isEmptyString(searchQuery)) { toast({ title: "Enter a Keyword", description: "Please enter one keyword at least." }); return; } if (searchQuery && searchQuery.length < 4) { toast({ title: "Keywords too short", description: "Keyword length must be at least 5." }); return; } router.push({ pathname: router.pathname, query: { ...router.query, q: searchQuery }, }); setIsLoading(true); fetchSearchAPI(searchQuery) .then((data) => { setSearchResult(data); if (data.length === 0) { toast({ title: "Empty Result", description: "No results were found for this keyword. Try another keyword.", }); } }) .catch(() => { toast({ title: "Network Error", description: "Please try it later." }); }) .finally(() => { setIsLoading(false); }); }; return (

{"SEARCH POSTS"}

{searchResult.map((item, index) => (
{item.title}
{item.summary &&
{item.summary}
}
{item.tags?.map((tagitem) => (
{tagitem}
))}
))}

{"For search efficiency, only the first 20 results are displayed."}