This repository has no description
1.5 kB
49 lines
1import { useMutation, useQueryClient } from '@tanstack/react-query';
2import { createConnection } from '../dal';
3import { connectionKeys } from '../connectionKeys';
4import { ConnectionType } from '@semble/types';
5import { cardKeys } from '@/features/cards/lib/cardKeys';
6
7export default function useCreateConnection() {
8 const queryClient = useQueryClient();
9
10 const mutation = useMutation({
11 mutationFn: (params: {
12 sourceUrl: string;
13 targetUrl: string;
14 connectionType?: ConnectionType;
15 note?: string;
16 }) => {
17 return createConnection(params);
18 },
19
20 onSuccess: (_data, variables) => {
21 // Invalidate all connection queries
22 queryClient.invalidateQueries({ queryKey: connectionKeys.all() });
23
24 // Invalidate forward connections for source URL
25 queryClient.invalidateQueries({
26 queryKey: connectionKeys.forwardForUrl(variables.sourceUrl),
27 });
28
29 // Invalidate backward connections for target URL
30 queryClient.invalidateQueries({
31 queryKey: connectionKeys.backwardForUrl(variables.targetUrl),
32 });
33
34 // Invalidate URL metadata with stats for both source and target URLs to update tab counts
35 queryClient.invalidateQueries({
36 queryKey: cardKeys.urlMetadata(variables.sourceUrl, {
37 includeStats: true,
38 }),
39 });
40 queryClient.invalidateQueries({
41 queryKey: cardKeys.urlMetadata(variables.targetUrl, {
42 includeStats: true,
43 }),
44 });
45 },
46 });
47
48 return mutation;
49}