aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/hooks/useImportSessions.ts
blob: cee99bbc6ebcb7ad71008739c52789fc4088219b (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
"use client";

import { toast } from "@/components/ui/use-toast";

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

export function useCreateImportSession() {
  const apiUtils = api.useUtils();

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

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

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

export function useDeleteImportSession() {
  const apiUtils = api.useUtils();

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