Skip to content

Commit 6811dca

Browse files
committed
feat(web): float the pull request comment composer
1 parent 3138f57 commit 6811dca

4 files changed

Lines changed: 188 additions & 143 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,14 @@ function PullRequestCodeTab({
961961
review.verdicts.length === 0 ? null : (
962962
<div className="pointer-events-none absolute inset-x-0 bottom-0 z-10">
963963
{reviewOpen ? (
964-
<div className="surface-glass pointer-events-auto absolute inset-x-3 bottom-3 rounded-xl border border-border/60 shadow-lg">
964+
<div
965+
className={cn(
966+
"surface-glass pointer-events-auto absolute inset-x-3 rounded-xl border border-border/60 shadow-lg",
967+
detail.capabilities.comment && detail.viewerPermissions.comment
968+
? "bottom-16"
969+
: "bottom-3",
970+
)}
971+
>
965972
<Button
966973
type="button"
967974
size="icon-sm"
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
2+
import {
3+
GitPullRequestClosedIcon,
4+
MessageSquareIcon,
5+
RotateCcwIcon,
6+
SendIcon,
7+
XIcon,
8+
} from "lucide-react";
9+
import { useRef, useState } from "react";
10+
11+
import { useAtomCommand } from "~/state/use-atom-command";
12+
import { pullRequestEnvironment } from "~/state/pullRequests";
13+
14+
import { Button } from "../ui/button";
15+
import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
16+
import { Textarea } from "../ui/textarea";
17+
import { toastManager } from "../ui/toast";
18+
19+
export function PullRequestCommentComposer({
20+
environmentId,
21+
reference,
22+
detail,
23+
actionPending,
24+
onCommentAction,
25+
onCommented,
26+
}: {
27+
environmentId: EnvironmentId;
28+
reference: PullRequestRef;
29+
detail: PullRequestDetailView;
30+
actionPending: boolean;
31+
onCommentAction: (
32+
body: string,
33+
action: "close" | "reopen",
34+
) => Promise<{ readonly commentPosted: boolean }>;
35+
onCommented: () => void;
36+
}) {
37+
const [open, setOpen] = useState(false);
38+
const [body, setBody] = useState("");
39+
const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
40+
const textareaRef = useRef<HTMLTextAreaElement>(null);
41+
const postComment = useAtomCommand(pullRequestEnvironment.comment, { reportFailure: false });
42+
const followUpAction =
43+
detail.state === "open" &&
44+
detail.capabilities.actions.includes("close") &&
45+
detail.viewerPermissions.actions.includes("close")
46+
? ("close" as const)
47+
: detail.state === "closed" &&
48+
detail.capabilities.actions.includes("reopen") &&
49+
detail.viewerPermissions.actions.includes("reopen")
50+
? ("reopen" as const)
51+
: null;
52+
53+
const submit = async (action: "comment" | "close" | "reopen") => {
54+
const trimmed = body.trim();
55+
if (trimmed.length === 0 || submitting !== null || actionPending) return;
56+
setSubmitting(action);
57+
if (action !== "comment") {
58+
const result = await onCommentAction(trimmed, action);
59+
if (result.commentPosted) {
60+
setBody("");
61+
setOpen(false);
62+
}
63+
setSubmitting(null);
64+
return;
65+
}
66+
const result = await postComment({
67+
environmentId,
68+
input: {
69+
...reference,
70+
body: trimmed,
71+
},
72+
});
73+
if (result._tag === "Failure") {
74+
setSubmitting(null);
75+
toastManager.add({ type: "error", title: "Could not post the comment" });
76+
return;
77+
}
78+
setBody("");
79+
setSubmitting(null);
80+
setOpen(false);
81+
onCommented();
82+
};
83+
84+
return (
85+
<Popover open={open} onOpenChange={setOpen}>
86+
<PopoverTrigger
87+
render={<Button size="icon" variant="glass" className="rounded-full shadow-lg" />}
88+
aria-label="Comment on pull request"
89+
>
90+
<MessageSquareIcon className="size-4" />
91+
</PopoverTrigger>
92+
<PopoverPopup
93+
side="top"
94+
align="start"
95+
sideOffset={8}
96+
className="w-96 max-w-[calc(100vw-2rem)]"
97+
initialFocus={textareaRef}
98+
>
99+
<div className="mb-3 flex items-center justify-between gap-2">
100+
<PopoverTitle className="text-sm">Comment on pull request</PopoverTitle>
101+
<PopoverClose
102+
render={<Button size="icon-xs" variant="ghost" />}
103+
aria-label="Close comment composer"
104+
>
105+
<XIcon className="size-3.5" />
106+
</PopoverClose>
107+
</div>
108+
<div className="space-y-2">
109+
<Textarea
110+
ref={textareaRef}
111+
className="[&_textarea]:max-h-64"
112+
// Locked while posting: the body is cleared on success, which would otherwise throw
113+
// away a new draft typed while the request was still in flight.
114+
disabled={submitting !== null || actionPending}
115+
value={body}
116+
rows={3}
117+
placeholder="Leave a comment"
118+
aria-label="Comment on this pull request"
119+
onChange={(event) => setBody(event.target.value)}
120+
/>
121+
<div className="flex flex-wrap justify-end gap-2">
122+
{followUpAction === null ? null : (
123+
<Button
124+
size="xs"
125+
variant={followUpAction === "close" ? "destructive-outline" : "outline"}
126+
disabled={body.trim().length === 0 || submitting !== null || actionPending}
127+
onClick={() => void submit(followUpAction)}
128+
>
129+
{followUpAction === "close" ? (
130+
<GitPullRequestClosedIcon className="size-3.5" />
131+
) : (
132+
<RotateCcwIcon className="size-3.5" />
133+
)}
134+
{submitting === followUpAction
135+
? followUpAction === "close"
136+
? "Closing..."
137+
: "Reopening..."
138+
: followUpAction === "close"
139+
? "Close with comment"
140+
: "Reopen with comment"}
141+
</Button>
142+
)}
143+
<Button
144+
size="xs"
145+
variant="outline"
146+
disabled={body.trim().length === 0 || submitting !== null || actionPending}
147+
onClick={() => void submit("comment")}
148+
>
149+
<SendIcon className="size-3.5" />
150+
{submitting === "comment" ? "Posting..." : "Comment"}
151+
</Button>
152+
</div>
153+
</div>
154+
</PopoverPopup>
155+
</Popover>
156+
);
157+
}

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState";
116116
import type { PullRequestAgentSelectionInput } from "./PullRequestCodeTab";
117117
import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLinkContextMenu";
118118
import { PullRequestMarkdownContext } from "./PullRequestMarkdown";
119+
import { PullRequestCommentComposer } from "./PullRequestCommentComposer";
119120
import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
120121
import { PullRequestTimelineTab } from "./PullRequestTimelineTab";
121122
import {
@@ -1480,7 +1481,7 @@ export function PullRequestDetailPanel({
14801481
}
14811482

14821483
return (
1483-
<div className="flex h-full min-h-0 w-full flex-col bg-background">
1484+
<div className="relative flex h-full min-h-0 w-full flex-col bg-background">
14841485
{threadPickerOpen && detail ? (
14851486
<PullRequestThreadLinks
14861487
key={`${environmentId}:${detail.url}`}
@@ -2612,8 +2613,6 @@ export function PullRequestDetailPanel({
26122613
fixFindingLabel={handoffLabels.fixFinding}
26132614
fixCheckLabel={handoffLabels.fixCheck}
26142615
onFixFinding={startFixFinding}
2615-
actionPending={actionPending}
2616-
onCommentAction={performCommentAction}
26172616
onRefresh={refreshDetail}
26182617
/>
26192618
</div>
@@ -2663,6 +2662,26 @@ export function PullRequestDetailPanel({
26632662
) : null}
26642663
</div>
26652664

2665+
{detail?.capabilities.comment && detail.viewerPermissions.comment ? (
2666+
<div className="absolute bottom-3 left-4 z-20">
2667+
<PullRequestCommentComposer
2668+
key={JSON.stringify([
2669+
environmentId,
2670+
reference.projectId,
2671+
reference.host,
2672+
reference.repository,
2673+
reference.number,
2674+
])}
2675+
environmentId={environmentId}
2676+
reference={reference}
2677+
detail={detail}
2678+
actionPending={actionPending}
2679+
onCommentAction={performCommentAction}
2680+
onCommented={refreshDetail}
2681+
/>
2682+
</div>
2683+
) : null}
2684+
26662685
<AlertDialog
26672686
open={confirmation.open}
26682687
onOpenChange={(open) => setConfirmation((current) => ({ ...current, open }))}

0 commit comments

Comments
 (0)