I can now see the songs that are in more than one file and delete the extra copies from Admin. - A table of the copies of each song (album, format, bitrate, size, length) with the copy to keep already chosen and changeable. - The check that reads the files, with a question first, progress, Stop, and the result of the last run; the groups are read again when it ends. - Delete the others for one song and Clean all suggestions for all of them, each after a question that lists the files and the space it frees. It stops at the first refusal and says what was already deleted. - With deleting off everything is visible and the delete buttons are disabled, with the reason. - English and Italian.
61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api } from './client'
|
|
import type { components } from './schema'
|
|
|
|
export type DuplicatesPage = components['schemas']['DuplicatesPage']
|
|
export type DuplicateGroup = components['schemas']['DuplicateGroup']
|
|
export type DuplicateFile = components['schemas']['DuplicateFile']
|
|
export type DuplicatesCheckStatus = components['schemas']['DuplicatesCheckStatus']
|
|
export type CleanDuplicates = components['schemas']['CleanDuplicates']
|
|
export type DuplicatesCleaned = components['schemas']['DuplicatesCleaned']
|
|
|
|
/** Groups read at once. The server gives at most 200 in a page. */
|
|
export const GROUPS_LIMIT = 200
|
|
|
|
const listKey = ['library', 'duplicates']
|
|
const checkKey = ['library', 'duplicates-check']
|
|
|
|
/** The groups of copies (a page of them), read from the index: quick, touches no file. */
|
|
export function useDuplicates() {
|
|
return useQuery({
|
|
queryKey: listKey,
|
|
queryFn: () => api.get<DuplicatesPage>(`/library/duplicates?limit=${GROUPS_LIMIT}`),
|
|
})
|
|
}
|
|
|
|
/** Whether the check runs, how far it is, and how the last one ended. While it runs it is read again every `intervalMs`. */
|
|
export function useDuplicatesCheck({ intervalMs = 2000 }: { intervalMs?: number } = {}) {
|
|
return useQuery({
|
|
queryKey: checkKey,
|
|
queryFn: () => api.get<DuplicatesCheckStatus>('/library/duplicates/check'),
|
|
refetchInterval: (query) => (query.state.data?.running ? intervalMs : false),
|
|
})
|
|
}
|
|
|
|
/** Starts the check in the background; the groups are read again when it ends (see `useRefreshWhenDone`). */
|
|
export function useStartDuplicatesCheck() {
|
|
const client = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ refresh }: { refresh: boolean }) =>
|
|
api.post<{ status: 'started' }>(refresh ? '/library/duplicates/check?refresh=true' : '/library/duplicates/check'),
|
|
onSettled: () => client.invalidateQueries({ queryKey: checkKey }),
|
|
})
|
|
}
|
|
|
|
/** Asks the check to end after the file it is on. */
|
|
export function useStopDuplicatesCheck() {
|
|
const client = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: () => api.del('/library/duplicates/check'),
|
|
onSettled: () => client.invalidateQueries({ queryKey: checkKey }),
|
|
})
|
|
}
|
|
|
|
/** Deletes the extra copies of one song. Whatever the answer, the groups are read again. */
|
|
export function useCleanDuplicates() {
|
|
const client = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: CleanDuplicates) => api.post<DuplicatesCleaned>('/library/duplicates/clean', body),
|
|
onSettled: () => client.invalidateQueries({ queryKey: listKey }),
|
|
})
|
|
}
|