Files
frontend-app-authoring/src/library-authoring/create-library/data/apiHooks.ts
Muhammad Waleed abcef4a502 fix: refresh search token after library (#2924)
creation for course creator access

  When a course creator creates a new library, the cached JWT token for
  Meilisearch needs to be refreshed to include the new library's access_id.
  Without this, newly added components won't appear in search results until
  the page is refreshed.

  This works in conjunction with the backend fix that creates SearchAccess
  records immediately on library creation.

  The fix invalidates the content_search query, triggering React Query to
  refetch the token with updated access permissions.
2026-03-05 14:17:27 +05:00

56 lines
1.8 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() });
// Invalidate the search token to refresh with the new library's access_id
queryClient.invalidateQueries({ queryKey: ['content_search'] });
},
});
};
/**
* 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,
});