2021-12-08 03:38:31 +00:00
|
|
|
import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
import Fuzzy from './_fuzzy';
|
|
|
|
import pages from '../public/pages.json';
|
|
|
|
import style from '../styles/fuzzy.module.css';
|
2022-04-27 08:07:40 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import posts from '../public/posts.json' // I do not like this
|
2021-12-08 03:38:31 +00:00
|
|
|
|
|
|
|
function FuzzyBar(): JSX.Element {
|
|
|
|
const searchField = useRef<any>(null);
|
|
|
|
const [searchValue, setSearchValue] = useState('');
|
|
|
|
const [resultsValue, setResultsValue] = useState(<></>);
|
|
|
|
let [metaKey, setMetaKey] = useState('Ctrl');
|
|
|
|
|
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
|
|
|
|
let fuzz: Fuzzy | null = null;
|
|
|
|
|
2022-02-14 20:32:58 +00:00
|
|
|
let entries = [...pages];
|
|
|
|
|
|
|
|
for (const [k,v] of posts.entries()) {
|
2022-04-24 04:40:26 +00:00
|
|
|
const item = v as any;
|
|
|
|
if (item.title && item.slug)
|
|
|
|
entries.push({title: item.title, link: `posts/${item.slug}`});
|
2022-02-14 20:32:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 03:38:31 +00:00
|
|
|
try {
|
|
|
|
fuzz = new Fuzzy({
|
2022-02-14 20:32:58 +00:00
|
|
|
pages: entries,
|
2021-12-08 03:38:31 +00:00
|
|
|
searchField: searchField,
|
|
|
|
searchValue: searchValue,
|
|
|
|
resultsValue: resultsValue,
|
|
|
|
setResultsValue: setResultsValue,
|
|
|
|
maxResults: 5
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchInput(e: ChangeEvent<HTMLInputElement>) {
|
|
|
|
setSearchValue(e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleSearch = useCallback(() => {
|
|
|
|
setShow(!show);
|
|
|
|
fuzz?.showSearchResults();
|
|
|
|
searchField.current?.focus();
|
|
|
|
}, [fuzz, show]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (window.navigator.userAgent.match(/mac[\s]?os/i))
|
|
|
|
setMetaKey('⌘ Cmd');
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const event = (e: KeyboardEvent) => {
|
|
|
|
if (e.code === 'KeyK' && (e.ctrlKey || e.metaKey)) {
|
2021-12-08 07:00:34 +00:00
|
|
|
e.preventDefault();
|
2021-12-08 03:38:31 +00:00
|
|
|
toggleSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.code === 'KeyP' && (e.ctrlKey || e.metaKey) && e.shiftKey) {
|
2021-12-08 07:00:34 +00:00
|
|
|
e.preventDefault();
|
2021-12-08 03:38:31 +00:00
|
|
|
alert('not really sure what to do here lol');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show)
|
|
|
|
searchField.current?.focus();
|
2021-12-08 07:00:34 +00:00
|
|
|
|
|
|
|
return false;
|
2021-12-08 03:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('keydown', event);
|
|
|
|
|
|
|
|
return () => { window.removeEventListener('keydown', event); }
|
|
|
|
}, [fuzz, show, toggleSearch]);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{
|
|
|
|
show ?
|
|
|
|
<div className={`fuzzynav ${style.container}`}>
|
|
|
|
<input type='text'
|
|
|
|
className={style.search}
|
|
|
|
value={searchValue}
|
|
|
|
placeholder='Go to ...'
|
|
|
|
ref={searchField}
|
|
|
|
onChange={searchInput}
|
|
|
|
onKeyUp={(e) => { fuzz?.searchKeyUpListener(e) }} />
|
|
|
|
<div
|
|
|
|
id='results'
|
|
|
|
className={style.results}>{resultsValue}</div>
|
|
|
|
</div>
|
|
|
|
: <></>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
<a className={style.searchBar} onClick={toggleSearch}>
|
|
|
|
<span className={style.searchTerm}>Search</span>
|
|
|
|
<div className={style.keybind}>
|
|
|
|
<span className={style.key}>{metaKey}</span> <span className={style.key}>K</span>
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FuzzyBar;
|