// import type React from "react"; import Fuse from "fuse.js"; import { useEffect, useState } from "react"; import Card from "@components/Card"; import type { Frontmatter } from "@utils/types"; interface Props { searchList: { title: string; description: string; frontmatter: Frontmatter; slug: string; }[]; } interface SearchResult { item: { title: string; description: string; frontmatter: Frontmatter; slug: string; }; refIndex: number; } export default function SearchBar({ searchList }: Props) { const [inputVal, setInputVal] = useState(""); const [searchResults, setSearchResults] = useState( null ); const handleChange = (e: React.FormEvent) => { setInputVal(e.currentTarget.value); }; const fuse = new Fuse(searchList, { keys: ["title", "description"], includeMatches: true, minMatchCharLength: 2, threshold: 0.5, }); useEffect(() => { setSearchResults(fuse!.search!(inputVal!)); }, [inputVal]); return ( <> {inputVal.length > 1 && (
Found {searchResults?.length} {searchResults?.length && searchResults?.length > 1 ? " results" : " result"}{" "} for '{inputVal}'
)} ); }