Skip to content
Source
Sign inSign up
Open History to see the last commit

PullRequestResolveThreadButton.tsx

66 lines1.9 KB

1import type {
2 EnvironmentId,
3 PullRequestDetailView,
4 PullRequestRef,
5 PullRequestConversationThread,
6} from "@t3tools/contracts";
7import { canResolvePullRequestThread } from "@t3tools/client-runtime/pull-request-editing";
8import { useState } from "react";
9​
10import { pullRequestEnvironment } from "~/state/pullRequests";
11import { useAtomCommand } from "~/state/use-atom-command";
12​
13import { Button } from "../ui/button";
14import { toastManager } from "../ui/toast";
15​
16/**
17 * Resolves or unresolves a thread from the conversation, so threads without a diff line (such as
18 * GitLab overview threads) can be resolved too. Callers put it on the thread's first comment only.
19 */
20export function PullRequestResolveThreadButton({
21 environmentId,
22 reference,
23 detail,
24 thread,
25 onRefresh,
26}: {
27 environmentId: EnvironmentId;
28 reference: PullRequestRef;
29 detail: PullRequestDetailView;
30 thread: Pick<PullRequestConversationThread, "id" | "isResolved" | "canResolve">;
31 onRefresh: () => void;
32}) {
33 const [pending, setPending] = useState(false);
34 const setThreadResolution = useAtomCommand(pullRequestEnvironment.setThreadResolution, {
35 reportFailure: false,
36 });
37 if (!detail.capabilities.review.resolve || !canResolvePullRequestThread(detail, thread)) {
38 return null;
39 }
40​
41 const toggle = async () => {
42 setPending(true);
43 const result = await setThreadResolution({
44 environmentId,
45 input: { ...reference, threadId: thread.id, resolved: !thread.isResolved },
46 });
47 setPending(false);
48 if (result._tag === "Failure") {
49 toastManager.add({ type: "error", title: "The conversation could not be updated" });
50 return;
51 }
52 onRefresh();
53 };
54​
55 return (
56 <Button
57 size="xs"
58 variant="ghost"
59 className="shrink-0"
60 disabled={pending}
61 onClick={() => void toggle()}
62 >
63 {thread.isResolved ? "Unresolve" : "Resolve"}
64 </Button>
65 );