Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

useSearchMutation.ts

31 lines782 Bytes

1import { useMutation } from '@tanstack/react-query'
2import { useNavigate } from '@tanstack/react-router'
3​
4function useSearchMutation() {
5 const navigate = useNavigate()
6​
7 return useMutation({
8 mutationFn: async (content: string) => {
9 const response = await fetch('/api/search', {
10 method: 'POST',
11 headers: { 'Content-Type': 'application/json' },
12 body: JSON.stringify({ content }),
13 })
14​
15 if (!response.ok) {
16 throw new Error('Search request failed')
17 }
18​
19 return response.json()
20 },
21 onSuccess: async (json) => {
22 const { name, parameters } = json?.data ?? {}
23​
24 if (name && parameters) {
25 await navigate({ to: `/${name}`, search: parameters })
26 }
27 },
28 })
30​
31export default useSearchMutation