List them, change one (only the fields given are sent), test a key, remove what a provider gave, and read what Lidarr offers only when asked.
68 lines
2.8 KiB
TypeScript
68 lines
2.8 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api } from './client'
|
|
import type { components } from './schema'
|
|
|
|
export type Provider = components['schemas']['Provider']
|
|
export type ProviderName = Provider['name']
|
|
export type ProviderTest = components['schemas']['ProviderTest']
|
|
export type ProviderPurge = components['schemas']['ProviderPurge']
|
|
export type LidarrOptions = components['schemas']['LidarrOptions']
|
|
export type LidarrOffers = components['schemas']['LidarrOffers']
|
|
|
|
/**
|
|
* What `PUT /providers/{name}` takes: any of these, and what is left out is kept. (The generated type of the
|
|
* body is an "at least one of" that the generator cannot express, so the fields are listed here.)
|
|
*/
|
|
export interface ProviderChanges {
|
|
enabled?: boolean
|
|
/** Write-only. An empty one clears the key, and turns off a provider that needs one. */
|
|
api_key?: string
|
|
contact?: string
|
|
/** Only Lidarr. An empty one clears the address and turns Lidarr off. */
|
|
url?: string
|
|
/** Only Lidarr. Replaces all its options at once. */
|
|
lidarr?: LidarrOptions
|
|
}
|
|
|
|
const PROVIDERS = ['providers']
|
|
|
|
/** Every external provider with whether it is on and what it would send. The key is never in the answer. */
|
|
export function useProviders() {
|
|
return useQuery({
|
|
queryKey: PROVIDERS,
|
|
queryFn: () => api.get<{ providers: Provider[] }>('/providers'),
|
|
select: (data) => data.providers,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Changes a provider: only the fields given are sent and the rest is kept. An `api_key` is write-only; it
|
|
* stays in the mutation's variables until `reset()` is called, so whoever sends one resets the mutation.
|
|
*/
|
|
export function useUpdateProvider() {
|
|
const client = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ name, ...changes }: { name: ProviderName } & ProviderChanges) => api.put<Provider>(`/providers/${name}`, changes),
|
|
onSuccess: () => client.invalidateQueries({ queryKey: PROVIDERS }),
|
|
})
|
|
}
|
|
|
|
/** Asks the provider one cheap question with the stored key. A "no" is an answer (`ok` false), not an error. */
|
|
export function useTestProvider() {
|
|
return useMutation({ mutationFn: (name: ProviderName) => api.post<ProviderTest>(`/providers/${name}/test`) })
|
|
}
|
|
|
|
/** Removes everything a provider told Sonda, and says how much that was. The provider is not switched off. */
|
|
export function usePurgeProvider() {
|
|
return useMutation({ mutationFn: (name: ProviderName) => api.del<ProviderPurge>(`/providers/${name}/data`) })
|
|
}
|
|
|
|
/** What Lidarr offers to choose from. It is asked only when `enabled`: it contacts the admin's own Lidarr. */
|
|
export function useLidarrOffers(enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: [...PROVIDERS, 'lidarr', 'options'],
|
|
enabled,
|
|
retry: false,
|
|
queryFn: () => api.get<LidarrOffers>('/providers/lidarr/options'),
|
|
})
|
|
}
|