Skip to content

Commit ac09a57

Browse files
committed
fix(web): preserve composer drafts and restore pending discard
1 parent 31f0c31 commit ac09a57

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* summary as a comment or the reverse.
1010
*/
1111
import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
12-
import { MessageSquareIcon, XIcon } from "lucide-react";
12+
import { MessageSquareIcon, Trash2Icon, XIcon } from "lucide-react";
1313
import { useRef, useState } from "react";
1414

1515
import { Button } from "../ui/button";
@@ -48,6 +48,7 @@ export function PullRequestComposer({
4848
const commentRef = useRef<HTMLTextAreaElement>(null);
4949
const reviewRef = useRef<HTMLTextAreaElement>(null);
5050
const pendingComments = usePendingReviewComments(reference);
51+
const clearComments = usePullRequestReviewStore((store) => store.clear);
5152
// A summary typed but not sent is review work too, and it outlives the popover. Selected as a
5253
// boolean rather than the text, so typing one does not re-render the composer per keystroke.
5354
const reviewKey = pullRequestReviewKey(reference);
@@ -104,6 +105,7 @@ export function PullRequestComposer({
104105
) : null}
105106
</PopoverTrigger>
106107
<PopoverPopup
108+
keepMounted
107109
side="top"
108110
align="end"
109111
sideOffset={8}
@@ -132,16 +134,28 @@ export function PullRequestComposer({
132134
{mode === "review" ? "Review pull request" : "Comment on pull request"}
133135
</PopoverTitle>
134136
)}
135-
<PopoverClose
136-
render={<Button size="icon-xs" variant="ghost" />}
137-
aria-label="Close composer"
138-
>
139-
<XIcon className="size-3.5" />
140-
</PopoverClose>
137+
<div className="flex items-center gap-1">
138+
{mode === "review" && pendingComments.length > 0 ? (
139+
<Button
140+
size="icon-xs"
141+
variant="ghost"
142+
aria-label="Discard pending line comments"
143+
title="Discard pending line comments"
144+
onClick={() => clearComments(reviewKey)}
145+
>
146+
<Trash2Icon className="size-3.5" />
147+
</Button>
148+
) : null}
149+
<PopoverClose
150+
render={<Button size="icon-xs" variant="ghost" />}
151+
aria-label="Close composer"
152+
>
153+
<XIcon className="size-3.5" />
154+
</PopoverClose>
155+
</div>
141156
</div>
142-
{/* Both panes stay mounted and the inactive one is only hidden. Unmounting would drop
143-
the comment draft on every toggle, and reset the submit guard of a form whose request
144-
is still in flight — enough to send the same review twice. */}
157+
{/* Keep both forms mounted across toggles and dismissal so drafts and in-flight
158+
submit guards survive closing and reopening the composer. */}
145159
{verdicts.length > 0 ? (
146160
<div hidden={mode !== "review"}>
147161
<PullRequestReviewForm

‎apps/web/src/components/ui/popover.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function PopoverPopup({
2525
sideOffset = 4,
2626
alignOffset = 0,
2727
tooltipStyle = false,
28+
keepMounted = false,
2829
anchor,
2930
...props
3031
}: PopoverPrimitive.Popup.Props & {
@@ -34,10 +35,14 @@ function PopoverPopup({
3435
sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
3536
alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
3637
tooltipStyle?: boolean;
38+
keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
3739
anchor?: PopoverPrimitive.Positioner.Props["anchor"];
3840
}) {
41+
// Viewport rekeys its children when the active trigger clears on close. Persistent
42+
// single-trigger forms need a stable container to retain drafts and submit guards.
43+
const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
3944
return (
40-
<PopoverPrimitive.Portal>
45+
<PopoverPrimitive.Portal keepMounted={keepMounted}>
4146
<PopoverPrimitive.Positioner
4247
align={align}
4348
alignOffset={alignOffset}
@@ -59,7 +64,7 @@ function PopoverPopup({
5964
data-slot="popover-popup"
6065
{...props}
6166
>
62-
<PopoverPrimitive.Viewport
67+
<Viewport
6368
className={cn(
6469
"relative size-full max-h-(--available-height) overflow-clip px-(--viewport-inline-padding) py-4 [--viewport-inline-padding:--spacing(4)] has-data-[slot=calendar]:p-2 data-instant:transition-none **:data-current:data-ending-style:opacity-0 **:data-current:data-starting-style:opacity-0 **:data-previous:data-ending-style:opacity-0 **:data-previous:data-starting-style:opacity-0 **:data-current:w-[calc(var(--popup-width)-2*var(--viewport-inline-padding)-2px)] **:data-previous:w-[calc(var(--popup-width)-2*var(--viewport-inline-padding)-2px)] **:data-current:opacity-100 **:data-previous:opacity-100 **:data-current:transition-opacity **:data-previous:transition-opacity",
6570
tooltipStyle
@@ -70,7 +75,7 @@ function PopoverPopup({
7075
data-slot="popover-viewport"
7176
>
7277
{children}
73-
</PopoverPrimitive.Viewport>
78+
</Viewport>
7479
</PopoverPrimitive.Popup>
7580
</PopoverPrimitive.Positioner>
7681
</PopoverPrimitive.Portal>

0 commit comments

Comments
 (0)