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('/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(`/me/import/spotify?limit=${PAST_LIMIT}`), }) }