diff --git a/app/routes/app.tsx b/app/routes/app.tsx index 059f5cb..736b94a 100644 --- a/app/routes/app.tsx +++ b/app/routes/app.tsx @@ -4,10 +4,12 @@ import { useRouteError, isRouteErrorResponse, useLoaderData, - Link + Link, + useNavigate } from '@remix-run/react' import {useHotkeys} from 'react-hotkeys-hook' import {useState, useCallback} from 'react' +import {useMutation} from '@tanstack/react-query' import {AButton, Button} from '~/lib/components/button' import {Notificatons} from '~/lib/components/notifications' @@ -54,11 +56,43 @@ const SearchModal = ({close}: {close: () => void}) => { node.focus() } }, []) + const [searchTerm, setSearchTerm] = useState('') + const navigate = useNavigate() + + const search = useMutation< + Array<{label: string; link: string}>, + Error, + string + >({ + mutationFn: async (searchTerm: string) => { + const formData = new FormData() + formData.append('query', searchTerm) + + const response = await fetch('/app/search?_data=routes/app.search', { + method: 'POST', + body: formData + }) + + const json = await response.json() + + if (json.results.length === 1) { + navigate(json.results[0].link) + close() + } + + return json.results + } + }) return (