The report of an import used to be shown once in Settings and lost. Each import is now stored (migration 0030, spotify_imports) as the report in JSON, and GET /me/import/spotify lists the past ones, newest first. POST /me/import/spotify answers with the same shape plus id and created_at. The web app gets an Import page (menu, More tab) with the form and one card for each past import: plays read, added, already there, not in the library, the playlists it made and the artists that are missing. Settings only links to it. An import that finds nothing is not kept, and a report that cannot be stored is still returned to the person who asked.
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api } from '@/api/client'
|
|
import type { components } from '@/api/schema'
|
|
|
|
export type SpotifyImportReport = components['schemas']['SpotifyImportReport']
|
|
|
|
/**
|
|
* Sends the files of a Spotify export (the zip, or the JSON files in it) to this server, which matches them to the
|
|
* library and records the listens and the playlists. When it is done everything that is made of listens or playlists is
|
|
* read again.
|
|
*/
|
|
export function useImportSpotify() {
|
|
const client = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (files: File[]) => {
|
|
const form = new FormData()
|
|
for (const file of files) form.append('files', file)
|
|
return api.upload<SpotifyImportReport>('/me/import/spotify', form)
|
|
},
|
|
onSuccess: () => client.invalidateQueries(),
|
|
})
|
|
}
|
|
|
|
/** How many past imports the page reads: they are rare, so the page shows them all. */
|
|
const PAST_LIMIT = 100
|
|
|
|
/** What each import did, newest first. */
|
|
export function useSpotifyImports() {
|
|
return useQuery({
|
|
queryKey: ['imports', 'spotify'],
|
|
queryFn: () => api.get<components['schemas']['SpotifyImportPage']>(`/me/import/spotify?limit=${PAST_LIMIT}`),
|
|
})
|
|
}
|