This repository has no description
1.5 kB
41 lines
1import { useMutation, useQueryClient } from '@tanstack/react-query';
2import { removeCardFromLibrary } from '../dal';
3import { cardKeys } from '../cardKeys';
4import { collectionKeys } from '@/features/collections/lib/collectionKeys';
5import { noteKeys } from '@/features/notes/lib/noteKeys';
6import { feedKeys } from '@/features/feeds/lib/feedKeys';
7import { sembleKeys } from '@/features/semble/lib/sembleKeys';
8
9export default function useRemoveCardFromLibrary() {
10 const queryClient = useQueryClient();
11
12 const mutation = useMutation({
13 mutationFn: (cardId: string) => {
14 return removeCardFromLibrary(cardId);
15 },
16
17 onSuccess: () => {
18 queryClient.invalidateQueries({ queryKey: cardKeys.all() });
19 queryClient.invalidateQueries({ queryKey: noteKeys.all() });
20 queryClient.invalidateQueries({ queryKey: feedKeys.all() });
21 queryClient.invalidateQueries({ queryKey: collectionKeys.all() });
22 queryClient.invalidateQueries({ queryKey: sembleKeys.all() });
23 // Invalidate all URL metadata queries with stats to update tab counts
24 queryClient.invalidateQueries({
25 predicate: (query): boolean => {
26 const key = query.queryKey as unknown[];
27 return !!(
28 key[0] === 'cards' &&
29 key[1] === 'metadata' &&
30 key[3] &&
31 typeof key[3] === 'object' &&
32 'includeStats' in key[3] &&
33 key[3].includeStats === true
34 );
35 },
36 });
37 },
38 });
39
40 return mutation;
41}