feat(discover): wire A4 search + A5 downloads to backend
Adds DownloadJob/ExternalSearchResult/SourceInfo contract types + mappers, the downloads + search RTKQ endpoints, and the SearchDownloadPage (search external sources, per-result download states) and DownloadsManagerPage (active/history, progress, retry/cancel, poll-while-active). en/ru i18n. Snapshot also bundles in-progress queue/metadata-editor/storage UI work. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,30 +1,88 @@
|
||||
import { api } from '../index';
|
||||
import type { DownloadJob } from '../types';
|
||||
import {
|
||||
toDownloadJob,
|
||||
toPage,
|
||||
type RawDownloadJob,
|
||||
type RawPaged,
|
||||
} from '../mappers';
|
||||
import type {
|
||||
DownloadJob,
|
||||
DownloadRequestResult,
|
||||
PaginatedResponse,
|
||||
} from '../types';
|
||||
|
||||
// NOTE: the backend `/downloads` routes are still unimplemented stubs (they
|
||||
// return no body / no schema). The request shapes below are provisional and the
|
||||
// responses will need the same snake→camel mapper treatment as library/playlists
|
||||
// (see `mappers.ts`) once the backend defines DownloadJob's wire format. Do not
|
||||
// wire these into the UI until then.
|
||||
interface ListParams {
|
||||
status?: DownloadJob['status'];
|
||||
/** Only the current user's jobs (backend `mine=true`). */
|
||||
mine?: boolean;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
interface RawCreateResponse {
|
||||
already_in_library: boolean;
|
||||
track_id: string | null;
|
||||
job: RawDownloadJob | null;
|
||||
}
|
||||
|
||||
export const downloadsApi = api.injectEndpoints({
|
||||
endpoints: (build) => ({
|
||||
getDownloads: build.query<
|
||||
DownloadJob[],
|
||||
{ status?: DownloadJob['status'] } | void
|
||||
PaginatedResponse<DownloadJob>,
|
||||
ListParams | void
|
||||
>({
|
||||
query: (params) => ({ url: '/downloads', params: params ?? {} }),
|
||||
providesTags: ['Download'],
|
||||
query: (params) => {
|
||||
const p = params ?? {};
|
||||
const size = p.pageSize ?? 50;
|
||||
return {
|
||||
url: '/downloads',
|
||||
params: {
|
||||
status: p.status,
|
||||
mine: p.mine ? true : undefined,
|
||||
limit: size,
|
||||
offset: ((p.page ?? 1) - 1) * size,
|
||||
},
|
||||
};
|
||||
},
|
||||
transformResponse: (raw: RawPaged<RawDownloadJob>) =>
|
||||
toPage(raw, toDownloadJob),
|
||||
providesTags: (result) =>
|
||||
result
|
||||
? [
|
||||
...result.items.map(({ id }) => ({
|
||||
type: 'Download' as const,
|
||||
id,
|
||||
})),
|
||||
'Download',
|
||||
]
|
||||
: ['Download'],
|
||||
}),
|
||||
addDownload: build.mutation<
|
||||
DownloadJob,
|
||||
{
|
||||
url: string;
|
||||
metadata?: { title?: string; artist?: string; album?: string };
|
||||
}
|
||||
getDownload: build.query<DownloadJob, string>({
|
||||
query: (id) => `/downloads/${id}`,
|
||||
transformResponse: (raw: RawDownloadJob) => toDownloadJob(raw),
|
||||
providesTags: (_r, _e, id) => [{ type: 'Download', id }],
|
||||
}),
|
||||
/** Request a download of a discovered item (§A4 "Download to library"). */
|
||||
createDownload: build.mutation<
|
||||
DownloadRequestResult,
|
||||
{ source: string; sourceId: string; query?: string }
|
||||
>({
|
||||
query: (body) => ({ url: '/downloads', method: 'POST', body }),
|
||||
invalidatesTags: ['Download'],
|
||||
query: (body) => ({
|
||||
url: '/downloads',
|
||||
method: 'POST',
|
||||
body: {
|
||||
source: body.source,
|
||||
source_id: body.sourceId,
|
||||
query: body.query,
|
||||
},
|
||||
}),
|
||||
transformResponse: (raw: RawCreateResponse): DownloadRequestResult => ({
|
||||
alreadyInLibrary: raw.already_in_library,
|
||||
trackId: raw.track_id ?? undefined,
|
||||
job: raw.job ? toDownloadJob(raw.job) : undefined,
|
||||
}),
|
||||
// A completed dedup can surface an existing library track; refresh both.
|
||||
invalidatesTags: ['Download', 'Track'],
|
||||
}),
|
||||
cancelDownload: build.mutation<void, string>({
|
||||
query: (id) => ({ url: `/downloads/${id}`, method: 'DELETE' }),
|
||||
@@ -32,7 +90,8 @@ export const downloadsApi = api.injectEndpoints({
|
||||
}),
|
||||
retryDownload: build.mutation<DownloadJob, string>({
|
||||
query: (id) => ({ url: `/downloads/${id}/retry`, method: 'POST' }),
|
||||
invalidatesTags: ['Download'],
|
||||
transformResponse: (raw: RawDownloadJob) => toDownloadJob(raw),
|
||||
invalidatesTags: (_r, _e, id) => [{ type: 'Download', id }, 'Download'],
|
||||
}),
|
||||
}),
|
||||
overrideExisting: false,
|
||||
@@ -40,7 +99,8 @@ export const downloadsApi = api.injectEndpoints({
|
||||
|
||||
export const {
|
||||
useGetDownloadsQuery,
|
||||
useAddDownloadMutation,
|
||||
useGetDownloadQuery,
|
||||
useCreateDownloadMutation,
|
||||
useCancelDownloadMutation,
|
||||
useRetryDownloadMutation,
|
||||
} = downloadsApi;
|
||||
|
||||
Reference in New Issue
Block a user