-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathusePrActions.ts
More file actions
48 lines (45 loc) · 1.63 KB
/
Copy pathusePrActions.ts
File metadata and controls
48 lines (45 loc) · 1.63 KB
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
import {
getOptimisticPrState,
PR_ACTION_LABELS,
} from "@posthog/core/git-interaction/prStatus";
import { useHostTRPC } from "@posthog/host-router/react";
import type { PrActionType } from "@posthog/shared";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "../../primitives/toast";
export function usePrActions(prUrl: string | null) {
const trpc = useHostTRPC();
const queryClient = useQueryClient();
const mutation = useMutation({
...trpc.git.updatePrByUrl.mutationOptions(),
onSuccess: (data, variables) => {
if (data.success) {
toast.success(PR_ACTION_LABELS[variables.action]);
queryClient.setQueryData(
trpc.git.getPrDetailsByUrl.queryKey({ prUrl: variables.prUrl }),
getOptimisticPrState(variables.action),
);
// The inbox Pulls list reads PR status from the batched
// `getPrDiffStatsBatch` query (separate cache, 5-min staleTime), so
// patching the detail cache above isn't enough — invalidate the batch
// so the list badge reflects the new state on next view.
void queryClient.invalidateQueries(
trpc.git.getPrDiffStatsBatch.pathFilter(),
);
} else {
toast.error("Failed to update PR", { description: data.message });
}
},
onError: (error) => {
toast.error("Failed to update PR", {
description: error instanceof Error ? error.message : "Unknown error",
});
},
});
return {
execute: (action: PrActionType) => {
if (!prUrl) return;
mutation.mutate({ prUrl, action });
},
isPending: mutation.isPending,
};
}