Documentation site of NextGraph.org
https://docs.nextgraph.org
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.7 KiB
110 lines
2.7 KiB
/** @jsxImportSource react */
|
|
import { useState, useCallback, useRef } from 'react';
|
|
import { ALGOLIA } from '../../config';
|
|
import 'typesense-docsearch-react';
|
|
import './Search.css';
|
|
|
|
import { createPortal } from 'react-dom';
|
|
import * as docSearchReact from 'typesense-docsearch-react';
|
|
|
|
/** FIXME: This is still kinda nasty, but DocSearch is not ESM ready. */
|
|
const DocSearchModal =
|
|
docSearchReact.DocSearchModal || (docSearchReact as any).default.DocSearchModal;
|
|
const useDocSearchKeyboardEvents =
|
|
docSearchReact.useDocSearchKeyboardEvents ||
|
|
(docSearchReact as any).default.useDocSearchKeyboardEvents;
|
|
|
|
export default function Search() {
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const searchButtonRef = useRef<HTMLButtonElement>(null);
|
|
const [initialQuery, setInitialQuery] = useState('');
|
|
|
|
const onOpen = useCallback(() => {
|
|
setIsOpen(true);
|
|
}, [setIsOpen]);
|
|
|
|
const onClose = useCallback(() => {
|
|
setIsOpen(false);
|
|
}, [setIsOpen]);
|
|
|
|
const onInput = useCallback(
|
|
(e) => {
|
|
setIsOpen(true);
|
|
setInitialQuery(e.key);
|
|
},
|
|
[setIsOpen, setInitialQuery]
|
|
);
|
|
|
|
useDocSearchKeyboardEvents({
|
|
isOpen,
|
|
onOpen,
|
|
onClose,
|
|
onInput,
|
|
searchButtonRef,
|
|
});
|
|
|
|
const serverConfig = {
|
|
apiKey: ALGOLIA.apiKey,
|
|
nodes: [
|
|
{
|
|
host: 'search.nextgraph.org',
|
|
port: 443,
|
|
protocol: 'https',
|
|
}]};
|
|
|
|
const searchParams = {};
|
|
|
|
return (
|
|
<>
|
|
<button type="button" ref={searchButtonRef} onClick={onOpen} className="search-input">
|
|
<svg width="24" height="24" fill="none">
|
|
<path
|
|
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
|
stroke="currentColor"
|
|
strokeWidth="2"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
/>
|
|
</svg>
|
|
|
|
<span>Search</span>
|
|
|
|
<span className="search-hint">
|
|
<span className="sr-only">Press </span>
|
|
|
|
<kbd>/</kbd>
|
|
|
|
<span className="sr-only"> to search</span>
|
|
</span>
|
|
</button>
|
|
|
|
{isOpen &&
|
|
createPortal(
|
|
<DocSearchModal
|
|
initialQuery={initialQuery}
|
|
initialScrollY={window.scrollY}
|
|
onClose={onClose}
|
|
typesenseCollectionName={ALGOLIA.indexName}
|
|
typesenseServerConfig={serverConfig}
|
|
//typesenseSearchParameters={searchParams}
|
|
// appId={ALGOLIA.appId}
|
|
// apiKey={ALGOLIA.apiKey}
|
|
transformItems={(items) => {
|
|
return items.map((item) => {
|
|
// We transform the absolute URL into a relative URL to
|
|
// work better on localhost, preview URLS.
|
|
const a = document.createElement('a');
|
|
a.href = item.url;
|
|
const hash = a.hash === '#overview' ? '' : a.hash;
|
|
return {
|
|
...item,
|
|
url: `${a.pathname}${hash}`,
|
|
};
|
|
});
|
|
}}
|
|
/>,
|
|
document.body
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|