aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/hooks/useImportSessions.ts
blob: 133bb29bb1004864d16f9714612500dadbc3a5e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"use client";

import { toast } from "@/components/ui/sonner";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";

import { useTRPC } from "@karakeep/shared-react/trpc";

export function useCreateImportSession() {
  const api = useTRPC();
  const queryClient = useQueryClient();

  return useMutation(
    api.importSessions.createImportSession.mutationOptions({
      onSuccess: () => {
        queryClient.invalidateQueries(
          api.importSessions.listImportSessions.pathFilter(),
        );
      },
      onError: (error) => {
        toast({
          description: error.message || "Failed to create import session",
          variant: "destructive",
        });
      },
    }),
  );
}

export function useListImportSessions() {
  const api = useTRPC();
  return useQuery(
    api.importSessions.listImportSessions.queryOptions(
      {},
      {
        select: (data) => data.sessions,
      },
    ),
  );
}

export function useImportSessionStats(importSessionId: string) {
  const api = useTRPC();
  return useQuery(
    api.importSessions.getImportSessionStats.queryOptions(
      {
        importSessionId,
      },
      {
        refetchInterval: 5000, // Refetch every 5 seconds to show progress
        enabled: !!importSessionId,
      },
    ),
  );
}

export function useDeleteImportSession() {
  const api = useTRPC();
  const queryClient = useQueryClient();

  return useMutation(
    api.importSessions.deleteImportSession.mutationOptions({
      onSuccess: () => {
        queryClient.invalidateQueries(
          api.importSessions.listImportSessions.pathFilter(),
        );
        toast({
          description: "Import session deleted successfully",
          variant: "default",
        });
      },
      onError: (error) => {
        toast({
          description: error.message || "Failed to delete import session",
          variant: "destructive",
        });
      },
    }),
  );
}