diff --git a/src/components/Search.tsx b/src/components/Search.tsx new file mode 100644 index 0000000..4b7d164 --- /dev/null +++ b/src/components/Search.tsx @@ -0,0 +1,91 @@ +// 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, + threshold: 0.3, + }); + + useEffect(() => { + setSearchResults(fuse!.search!(inputVal!)); + }, [inputVal]); + + return ( + <> + + + {searchResults && searchResults.length > 0 && ( +
+ Found {searchResults?.length} + {searchResults?.length && searchResults?.length > 1 + ? " results" + : " result"}{" "} + for '{inputVal}' +
+ )} + + + + ); +}