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

PullRequestResolveThreadButton.tsx

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