()
| 9 | |
| 10 | /** Full text search route */ |
| 11 | export function Search(): JSX.Element { |
| 12 | const [query] = useSearchQuery(); |
| 13 | const [selectedIndex, setSelected] = useState(0); |
| 14 | const { results, loading, error } = useServerSearch(query, { |
| 15 | debounce: 0, |
| 16 | }); |
| 17 | const history = useHistory(); |
| 18 | const htmlElRef = useRef(null); |
| 19 | |
| 20 | /** Moves the viewport to the card at the selected index */ |
| 21 | function moveTo(index: number) { |
| 22 | setSelected(index); |
| 23 | const currentElm = htmlElRef?.current?.children[index]; |
| 24 | currentElm?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); |
| 25 | } |
| 26 | |
| 27 | useHotkeys( |
| 28 | 'enter', |
| 29 | e => { |
| 30 | e.preventDefault(); |
| 31 | const subject = |
| 32 | htmlElRef?.current?.children[selectedIndex]?.getAttribute('about'); |
| 33 | if (subject) { |
| 34 | //@ts-ignore blur does exist though |
| 35 | document?.activeElement?.blur(); |
| 36 | history.push(openURL(subject)); |
| 37 | } |
| 38 | }, |
| 39 | { enableOnTags: ['INPUT'] }, |
| 40 | ); |
| 41 | useHotkeys( |
| 42 | 'up', |
| 43 | e => { |
| 44 | e.preventDefault(); |
| 45 | const newSelected = selectedIndex > 0 ? selectedIndex - 1 : 0; |
| 46 | moveTo(newSelected); |
| 47 | }, |
| 48 | { enableOnTags: ['INPUT'] }, |
| 49 | ); |
| 50 | useHotkeys( |
| 51 | 'down', |
| 52 | e => { |
| 53 | e.preventDefault(); |
| 54 | const newSelected = |
| 55 | selectedIndex == results.length - 1 |
| 56 | ? results.length - 1 |
| 57 | : selectedIndex + 1; |
| 58 | moveTo(newSelected); |
| 59 | }, |
| 60 | { enableOnTags: ['INPUT'] }, |
| 61 | ); |
| 62 | |
| 63 | let message = 'No hits'; |
| 64 | if (query.length == 0) { |
| 65 | message = 'Enter a search query'; |
| 66 | } |
| 67 | if (loading) { |
| 68 | message = 'Loading results...'; |
nothing calls this directly
no test coverage detected