import { useState, useEffect } from 'react';
import Fuse from 'fuse.js';
import Navbar from '../components/navbar';
import Tags from '../components/tag'
import SearchOutlinedIcon from '@material-ui/icons/SearchOutlined';
function SearchBar(props: { searchFunction: () => void }) {
return
props.searchFunction()}
spellCheck="false"
autoComplete="off"/>
}
interface Post {
title: string
subtitle: string
author: string
date: string
urlname: string
tags: Array
}
interface PostsInfo {
valid_tags: Array
posts: Array
}
function Post(props: { post: Post }) {
return
{props.post.title}
{props.post.subtitle && {props.post.subtitle}
}
Written by {props.post.author} on {new Date(props.post.date).toLocaleString("en-us", {
month: "long", day: "numeric"
})}
;
}
function Posts(props: { posts: Array }) {
return
{
props.posts.map(post =>
)
}
;
}
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;
var posts = await fetch("/posts.json");
var postsJson: PostsInfo = await posts.json();
postsJson.posts = postsJson.posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime())
setPosts(postsJson);
setQuery(query);
})()}, []);
useEffect(() => {
if(query.length == 0) {
setVisiblePosts(posts.posts)
} else {
setVisiblePosts(fuse.search(query).map(res => res.item))
}
}, [query]);
return
Search for posts
{
setTimeout(() => setQuery((document.getElementById("searchInput") as HTMLInputElement).value));
}}/>
}