From 9a3fc044dec34dce13319f79acf6ed731a877a98 Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Wed, 14 Aug 2024 15:42:43 +0530 Subject: [PATCH] refactor: invalidate library related content queries --- src/library-authoring/data/apiHooks.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/library-authoring/data/apiHooks.ts b/src/library-authoring/data/apiHooks.ts index c7748f5e93..7096fd52bf 100644 --- a/src/library-authoring/data/apiHooks.ts +++ b/src/library-authoring/data/apiHooks.ts @@ -1,4 +1,6 @@ -import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; +import { + useQuery, useMutation, useQueryClient, Query, +} from '@tanstack/react-query'; import { type GetLibrariesV2CustomParams, @@ -121,7 +123,17 @@ export const useRevertLibraryChanges = () => { mutationFn: revertLibraryChanges, onSettled: (_data, _error, libraryId) => { queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(libraryId) }); - queryClient.invalidateQueries({ queryKey: ['content_search'] }); + queryClient.invalidateQueries({ + // Invalidate all content queries related to this library + predicate: (query: Query): boolean => { + // extraFilter contains library id + const extraFilter = query.queryKey[5]; + if (!(Array.isArray(extraFilter) || typeof extraFilter === 'string')) { + return false; + } + return query.queryKey[0] === 'content_search' && extraFilter?.includes(`context_key = "${libraryId}"`); + }, + }); }, }); };