Skip to content

Commit d7c71f9

Browse files
authored
feat(web): float the pull request comment composer (#11531)
Co-authored-by: maria-rcks <[email protected]>
1 parent 21d53ca commit d7c71f9

4 files changed

Lines changed: 200 additions & 145 deletions

File tree

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

Lines changed: 14 additions & 4 deletions
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"
@@ -984,10 +991,13 @@ function PullRequestCodeTab({
984991
/>
985992
</div>
986993
) : (
987-
// Bottom-right, clear of the vertical scrollbar the diff view keeps to its own right
988-
// edge.
989994
<Button
990-
className="pointer-events-auto absolute right-4 bottom-3 rounded-full shadow-lg"
995+
className={cn(
996+
"pointer-events-auto absolute bottom-3 rounded-full shadow-lg",
997+
detail.capabilities.comment && detail.viewerPermissions.comment
998+
? "right-16"
999+
: "right-4",
1000+
)}
9911001
onClick={() => setReviewOpen(true)}
9921002
size="compact"
9931003
variant="glass"
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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={
88+
<Button
89+
size="icon"
90+
variant="glass"
91+
className="rounded-full shadow-lg [--glass-opacity:50%]"
92+
/>
93+
}
94+
aria-label="Comment on pull request"
95+
>
96+
<MessageSquareIcon className="size-4" />
97+
</PopoverTrigger>
98+
<PopoverPopup
99+
side="top"
100+
align="end"
101+
sideOffset={8}
102+
className="w-96 max-w-[calc(100vw-2rem)]"
103+
initialFocus={textareaRef}
104+
>
105+
<div className="mb-3 flex items-center justify-between gap-2">
106+
<PopoverTitle className="text-sm">Comment on pull request</PopoverTitle>
107+
<PopoverClose
108+
render={<Button size="icon-xs" variant="ghost" />}
109+
aria-label="Close comment composer"
110+
>
111+
<XIcon className="size-3.5" />
112+
</PopoverClose>
113+
</div>
114+
<div className="space-y-2">
115+
<Textarea
116+
ref={textareaRef}
117+
className="[&_textarea]:max-h-64"
118+
// Locked while posting: the body is cleared on success, which would otherwise throw
119+
// away a new draft typed while the request was still in flight.
120+
disabled={submitting !== null || actionPending}
121+
value={body}
122+
rows={3}
123+
placeholder="Leave a comment"
124+
aria-label="Comment on this pull request"
125+
onChange={(event) => setBody(event.target.value)}
126+
/>
127+
<div className="flex flex-wrap justify-end gap-2">
128+
{followUpAction === null ? null : (
129+
<Button
130+
size="xs"
131+
variant={followUpAction === "close" ? "destructive-outline" : "outline"}
132+
disabled={body.trim().length === 0 || submitting !== null || actionPending}
133+
onClick={() => void submit(followUpAction)}
134+
>
135+
{followUpAction === "close" ? (
136+
<GitPullRequestClosedIcon className="size-3.5" />
137+
) : (
138+
<RotateCcwIcon className="size-3.5" />
139+
)}
140+
{submitting === followUpAction
141+
? followUpAction === "close"
142+
? "Closing..."
143+
: "Reopening..."
144+
: followUpAction === "close"
145+
? "Close with comment"
146+
: "Reopen with comment"}
147+
</Button>
148+
)}
149+
<Button
150+
size="xs"
151+
variant="outline"
152+
disabled={body.trim().length === 0 || submitting !== null || actionPending}
153+
onClick={() => void submit("comment")}
154+
>
155+
<SendIcon className="size-3.5" />
156+
{submitting === "comment" ? "Posting..." : "Comment"}
157+
</Button>
158+
</div>
159+
</div>
160+
</PopoverPopup>
161+
</Popover>
162+
);
163+
}

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

Lines changed: 23 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,27 @@ export function PullRequestDetailPanel({
26632662
) : null}
26642663
</div>
26652664

2665+
{/* Float over the content; do not reserve a footer or padding in the PR tabs. */}
2666+
{detail?.capabilities.comment && detail.viewerPermissions.comment ? (
2667+
<div className="absolute right-4 bottom-3 z-20">
2668+
<PullRequestCommentComposer
2669+
key={JSON.stringify([
2670+
environmentId,
2671+
reference.projectId,
2672+
reference.host,
2673+
reference.repository,
2674+
reference.number,
2675+
])}
2676+
environmentId={environmentId}
2677+
reference={reference}
2678+
detail={detail}
2679+
actionPending={actionPending}
2680+
onCommentAction={performCommentAction}
2681+
onCommented={refreshDetail}
2682+
/>
2683+
</div>
2684+
) : null}
2685+
26662686
<AlertDialog
26672687
open={confirmation.open}
26682688
onOpenChange={(open) => setConfirmation((current) => ({ ...current, open }))}

0 commit comments

Comments
 (0)