diff --git a/source/src/pages/Search.tsx b/source/src/pages/Search.tsx index 12574b7..c83ea79 100644 --- a/source/src/pages/Search.tsx +++ b/source/src/pages/Search.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react" +import { useEffect, useState, useRef } from "react" import styled from "styled-components" import { useLocation, useHistory } from "react-router-dom" import { Helmet } from "react-helmet-async" @@ -29,16 +29,16 @@ const StyledSearch = styled.div` const StyledSearchContainer = styled.div` display: flex; - align-items: center; + align-items: flex-start; @media screen and (max-width: ${theming.size.screen_size2}) { flex-direction: column-reverse; } ` -const StyledSearchControlContainer = styled.div` +const StyledSearchControlContainer = styled.form` width: 100%; - margin: 0 0 0 1rem; + margin-left: 1rem; @media screen and (max-width: ${theming.size.screen_size2}) { margin-top: 2rem; @@ -87,9 +87,7 @@ export default function Search() { function _Search() { const [index, setIndex] = useState({} as elasticlunr.Index) - - // load search index only once - useEffect(() => setIndex(elasticlunr.Index.load(searchIndex as never)), []) + const inputRef = useRef(null) const _history = useHistory() const _location = useLocation() @@ -124,6 +122,64 @@ function _Search() { const [searchInput, setSearchInput] = useState(query.query) + function doSearch() { + if (inputRef.current) inputRef.current.blur() + + _history.push({ + pathname: "/search", + search: queryString.stringify({ + ...(query.from && { + from: query.from, + }), + ...(query.to && { + to: query.to, + }), + ...(query.tags.length > 0 && { + tags: query.tags.join(","), + }), + query: searchInput, + }), + }) + + try { + const _postCards: unknown[] = [] + for (const res of index.search(searchInput)) { + const postData = map.posts[res.ref] + + // if post data exists, + // date is within range, + // and if post include tags + if ( + postData && + isDateInRange(postData.date, query.from, query.to) && + true + ) { + _postCards.push( + + ) + } + // apply search result + setPostCards(_postCards) + } + // eslint-disable-next-line no-empty + } catch (err) {} + } + + // load search index only once + useEffect(() => { + setIndex(elasticlunr.Index.load(searchIndex as never)) + }, []) + + useEffect(() => { + doSearch() + }, [dateRange]) + return ( <> @@ -173,70 +229,21 @@ function _Search() { }} /> - + { + event.preventDefault() + doSearch() + }} + > setSearchInput(event.target.value) } /> - +

@@ -251,7 +258,8 @@ function _Search() {
date to: {query.to}
- + value="Search test 1" + /> | - + value="Search test 2" + />
- + value="clear date" + />