From d2d813c04ce37cdb4a283dcd60b7986882970be5 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 1 Apr 2021 14:18:51 +0200 Subject: simple fuzzy search working --- pages/search.tsx | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'pages/search.tsx') diff --git a/pages/search.tsx b/pages/search.tsx index 8053d4a..af9bfeb 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -1,21 +1,20 @@ import { useState, useEffect } from 'react'; +import Fuse from 'fuse.js'; import Navbar from '../components/navbar'; -import { FormEvent } from 'react'; -import { ArticleMeta } from './post/[id]'; import Tags from '../components/tag' import SearchOutlinedIcon from '@material-ui/icons/SearchOutlined'; -function SearchBar(props: { - searchFunction: (event?: FormEvent) => void; -}) { +function SearchBar(props: { searchFunction: () => void }) { return
-
- - - -
+ props.searchFunction()} + autoComplete="off"/> +
} @@ -54,12 +53,39 @@ function Posts(props: { posts: Array }) { export default function SearchPage() { var [posts, setPosts] = useState({ posts: [], valid_tags: [] }); + var [query, setQuery] = useState(""); + var [visiblePosts, setVisiblePosts] = useState>([]); + + var fuse = new Fuse(posts.posts, { + keys: [ + "title", + "subtitle", + "author", + "date", + "urlname", + "tags" + ], + isCaseSensitive: false + }) useEffect(() => {(async () => { + var query = new URLSearchParams(window.location.search).get("q") || ""; + if(query) + (document.getElementById("searchInput") as HTMLInputElement).value = query; + setQuery(query); + var posts = await fetch("/posts.json"); setPosts(await posts.json()); })()}, []); + useEffect(() => { + if(query.length == 0) { + setVisiblePosts(posts.posts) + } else { + setVisiblePosts(fuse.search(query).map(res => res.item)) + } + }, [query]); + return
@@ -71,10 +97,10 @@ export default function SearchPage() {
- ) => { - event?.preventDefault(); + { + setTimeout(() => setQuery((document.getElementById("searchInput") as HTMLInputElement).value)); }}/> - +
-- cgit v1.2.3