When uploading a library archive file during the creation of a new library, the code prior to this commit did not properly handle the "In Progress" state, which is when the celery task doing the archive processing is actively running. Note that this is distinct from the "Pending" state, which is when the task is waiting in the queue to be run (which in practice should almost never happen unless there is an operational issue). Since celery tasks run in-process during local development, the task was always finished by the time that the browser made a call to check on the status. The problem only happened on slower sandboxes, where processing truly runs asynchronously and might take a few seconds. Because this case wasn't handled, the frontend would never poll for updates either, so the upload was basically lost as far as the user was concerned.
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import {
|
|
useMutation,
|
|
useQuery,
|
|
useQueryClient,
|
|
} from '@tanstack/react-query';
|
|
|
|
import { createLibraryV2, createLibraryRestore, getLibraryRestoreStatus } from './api';
|
|
import { libraryAuthoringQueryKeys } from '../../data/apiHooks';
|
|
import {
|
|
CreateLibraryRestoreResponse,
|
|
GetLibraryRestoreStatusResponse,
|
|
libraryRestoreQueryKeys,
|
|
LibraryRestoreStatus,
|
|
} from './restoreConstants';
|
|
|
|
/**
|
|
* Hook that provides a "mutation" that can be used to create a new content library.
|
|
*/
|
|
export const useCreateLibraryV2 = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: createLibraryV2,
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibraryList() });
|
|
},
|
|
});
|
|
};
|
|
|
|
/**
|
|
* React Query hook to fetch restore status for a specific task
|
|
*
|
|
* @param taskId - The unique identifier of the restore task
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const { data, isLoading, isError } = useGetLibraryRestoreStatus('task:456abc');
|
|
* ```
|
|
*/
|
|
export const useGetLibraryRestoreStatus = (taskId: string) => useQuery<GetLibraryRestoreStatusResponse, Error>({
|
|
queryKey: libraryRestoreQueryKeys.restoreStatus(taskId),
|
|
queryFn: () => getLibraryRestoreStatus(taskId),
|
|
enabled: !!taskId, // Only run the query if taskId is provided
|
|
refetchInterval: (query) => (
|
|
(query.state.data?.state === LibraryRestoreStatus.Pending
|
|
|| query.state.data?.state === LibraryRestoreStatus.InProgress
|
|
) ? 2000 : false),
|
|
});
|
|
|
|
export const useCreateLibraryRestore = () => useMutation<CreateLibraryRestoreResponse, Error, File>({
|
|
mutationKey: libraryRestoreQueryKeys.restoreMutation(),
|
|
mutationFn: createLibraryRestore,
|
|
});
|