aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared-react/hooks/highlights.ts
blob: e642f878d853a74066fdd18f8cf1b9ddee7f3242 (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
import { api } from "../trpc";

export function useCreateHighlight(
  ...opts: Parameters<typeof api.highlights.create.useMutation>
) {
  const apiUtils = api.useUtils();
  return api.highlights.create.useMutation({
    ...opts[0],
    onSuccess: (res, req, meta, context) => {
      apiUtils.highlights.getForBookmark.invalidate({
        bookmarkId: req.bookmarkId,
      });
      apiUtils.highlights.getAll.invalidate();
      return opts[0]?.onSuccess?.(res, req, meta, context);
    },
  });
}

export function useUpdateHighlight(
  ...opts: Parameters<typeof api.highlights.update.useMutation>
) {
  const apiUtils = api.useUtils();
  return api.highlights.update.useMutation({
    ...opts[0],
    onSuccess: (res, req, meta, context) => {
      apiUtils.highlights.getForBookmark.invalidate({
        bookmarkId: res.bookmarkId,
      });
      apiUtils.highlights.getAll.invalidate();
      return opts[0]?.onSuccess?.(res, req, meta, context);
    },
  });
}

export function useDeleteHighlight(
  ...opts: Parameters<typeof api.highlights.delete.useMutation>
) {
  const apiUtils = api.useUtils();
  return api.highlights.delete.useMutation({
    ...opts[0],
    onSuccess: (res, req, meta, context) => {
      apiUtils.highlights.getForBookmark.invalidate({
        bookmarkId: res.bookmarkId,
      });
      apiUtils.highlights.getAll.invalidate();
      return opts[0]?.onSuccess?.(res, req, meta, context);
    },
  });
}