A social knowledge tool for researchers built on ATProto
1import { ApiClient } from '@/api-client/ApiClient';
2import { createClientTokenManager } from '@/services/auth';
3import { useMutation, useQueryClient } from '@tanstack/react-query';
4
5export default function useCreateCollection() {
6 const apiClient = new ApiClient(
7 process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3000',
8 createClientTokenManager(),
9 );
10
11 const queryClient = useQueryClient();
12
13 const mutation = useMutation({
14 mutationFn: (newCollection: { name: string; description: string }) => {
15 return apiClient.createCollection(newCollection);
16 },
17
18 // Do things that are absolutely necessary and logic related (like query invalidation) in the useMutation callbacks
19 // Do UI related things like redirects or showing toast notifications in mutate callbacks. If the user navigated away from the current screen before the mutation finished, those will purposefully not fire
20 // https://tkdodo.eu/blog/mastering-mutations-in-react-query#some-callbacks-might-not-fire
21 onSuccess: () => {
22 queryClient.invalidateQueries({ queryKey: ['collections'] });
23 },
24 });
25
26 return mutation;
27}