~bigbes/lethe

ref: 54353ce8b74807ef9fd757854f41de7caf502058 lethe/web/src/features/settings/useSavedSearches.ts -rw-r--r-- 2.7 KiB
54353ce8 — Eugene Blikh chore: update TODO and dist after search UI merge 24 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import type { UseQueryResult, UseMutationResult } from '@tanstack/react-query'
import { apiFetch, apiFetchVoid } from '../../api/client'
import { adaptSavedSearch } from '../../api/adapters'
import type { SavedSearch, SavedSearchDTO } from '../../api/adapters'

interface SavedSearchesResponse {
  saved_searches: SavedSearchDTO[]
}

export interface UseSavedSearchesOptions {
  enabled?: boolean
  staleTime?: number
}

export function useSavedSearches(options?: UseSavedSearchesOptions): UseQueryResult<SavedSearch[]> {
  return useQuery({
    queryKey: ['saved-searches'],
    queryFn: async () => {
      const data = await apiFetch<SavedSearchesResponse>('/api/v1/saved-searches')
      return data.saved_searches.map(adaptSavedSearch)
    },
    enabled: options?.enabled,
    staleTime: options?.staleTime ?? 30_000,
  })
}

export function useCreateSavedSearch(): UseMutationResult<SavedSearch, Error, { name: string; query: string }> {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async ({ name, query }) => {
      const dto = await apiFetch<SavedSearchDTO>('/api/v1/saved-searches', {
        method: 'POST',
        body: JSON.stringify({ name, query }),
        headers: { 'Content-Type': 'application/json' },
      })
      return adaptSavedSearch(dto)
    },
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ['saved-searches'] })
    },
  })
}

export function useUpdateSavedSearch(): UseMutationResult<
  SavedSearch,
  Error,
  { oldName: string; name?: string; query?: string }
> {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async ({ oldName, name, query }) => {
      const body: Record<string, string> = {}
      if (name !== undefined) body['name'] = name
      if (query !== undefined) body['query'] = query

      const dto = await apiFetch<SavedSearchDTO>(
        `/api/v1/saved-searches/${encodeURIComponent(oldName)}`,
        {
          method: 'PUT',
          body: JSON.stringify(body),
          headers: { 'Content-Type': 'application/json' },
        },
      )
      return adaptSavedSearch(dto)
    },
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ['saved-searches'] })
    },
  })
}

export function useDeleteSavedSearch(): UseMutationResult<void, Error, { name: string }> {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async ({ name }) => {
      await apiFetchVoid(`/api/v1/saved-searches/${encodeURIComponent(name)}`, {
        method: 'DELETE',
      })
    },
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ['saved-searches'] })
    },
  })
}