Reading is a status, never an error. Sending is one POST for one artist; when it worked, or Lidarr did not answer in time, what Lidarr has is read again.
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api, ApiError } from './client'
|
|
import type { components } from './schema'
|
|
|
|
export type ArtistInLidarr = components['schemas']['ArtistInLidarr']
|
|
export interface SendResult {
|
|
status: 'added' | 'already_added'
|
|
}
|
|
|
|
const lidarrKey = (artistId: number) => ['artist', artistId, 'lidarr']
|
|
|
|
/**
|
|
* Whether the user's own Lidarr has the artist. Whatever stops the answer (provider off, no MusicBrainz id,
|
|
* no address or key, Lidarr down) is a `provider_status`, not an error. Admins only: others get a 403.
|
|
*/
|
|
export function useArtistInLidarr(artistId: number, opts: { enabled?: boolean } = {}) {
|
|
return useQuery({
|
|
queryKey: lidarrKey(artistId),
|
|
enabled: opts.enabled ?? true,
|
|
retry: false,
|
|
queryFn: () => api.get<ArtistInLidarr>(`/artists/${artistId}/lidarr`),
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Sends one artist to Lidarr, which then looks for and downloads the music by itself. It is only ever called
|
|
* for one artist, from a click the user confirmed. When it worked, or when Lidarr did not answer in time (the
|
|
* artist may have been added all the same), what Lidarr has is read again; a refusal changes nothing.
|
|
*/
|
|
export function useSendToLidarr(artistId: number) {
|
|
const client = useQueryClient()
|
|
const reread = () => client.invalidateQueries({ queryKey: lidarrKey(artistId) })
|
|
return useMutation({
|
|
mutationFn: () => api.post<SendResult>(`/artists/${artistId}/lidarr`),
|
|
onSuccess: reread,
|
|
onError: (error) => {
|
|
if (error instanceof ApiError && error.code === 'lidarr_unavailable') return reread()
|
|
},
|
|
})
|
|
}
|