feat(web): merge the comment and review buttons into one composer (#12945)
Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
1 parent
9a609a4e44
commit
5423ba0fd0
7 files changed
+413
-301
No files matched your search
@@ -18,13 +18,11 @@ import {
|
||||
Columns2Icon,
|
||||
FolderTreeIcon,
|
||||
InfoIcon,
|
||||
MessageSquareIcon,
|
||||
MessageSquareOffIcon,
|
||||
PilcrowIcon,
|
||||
Rows3Icon,
|
||||
TextWrapIcon,
|
||||
TriangleAlertIcon,
|
||||
XIcon,
|
||||
} from "lucide-react";
|
||||
import { useAtomRefresh } from "@effect/atom-react";
|
||||
import * as Schema from "effect/Schema";
|
||||
@@ -78,7 +76,6 @@ import { toastManager } from "../ui/toast";
|
||||
import { Toggle, ToggleGroup } from "../ui/toggle-group";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
import { PendingReviewCommentCard, ReviewThreadCard } from "./PullRequestReviewAnnotation";
|
||||
import { PullRequestReviewBar } from "./PullRequestReviewBar";
|
||||
import {
|
||||
isFileDiffCollapsed,
|
||||
isLineInFileDiff,
|
||||
@@ -247,9 +244,6 @@ function PullRequestCodeTab({
|
||||
const [draft, setDraft] = useState<DraftAnchor | null>(null);
|
||||
const [threadPending, setThreadPending] = useState(false);
|
||||
const [orphansOpen, setOrphansOpen] = useState(false);
|
||||
// Closed by default so the review form does not permanently eat vertical space below the
|
||||
// diff; opened on demand as a floating overlay instead.
|
||||
const [reviewOpen, setReviewOpen] = useState(false);
|
||||
// Which pull request the slices belong to travels with them, so a render taken before the
|
||||
// reset below cannot read the previous one's slices — or send its cursor to the host.
|
||||
const [sliceState, setSliceState] = useState<{
|
||||
@@ -378,7 +372,6 @@ function PullRequestCodeTab({
|
||||
inlineComment: hostReview.inlineComment && viewer.comment,
|
||||
reply: hostReview.reply && viewer.comment,
|
||||
resolve: hostReview.resolve && viewer.resolve,
|
||||
verdicts: hostReview.verdicts.filter((verdict) => viewer.verdicts.includes(verdict)),
|
||||
};
|
||||
}, [detail.capabilities.review, detail.viewerPermissions]);
|
||||
// A comment is posted against the pull request's head diff, so a line number taken from one
|
||||
@@ -1054,70 +1047,6 @@ function PullRequestCodeTab({
|
||||
],
|
||||
);
|
||||
|
||||
/**
|
||||
* The review overlay belongs to the pull request, not to the patch: a change whose diff
|
||||
* cannot be structured — or read at all — is still one a reviewer can approve or reject, so
|
||||
* it survives every branch below. It floats over the scroll area rather than sitting in the
|
||||
* layout flow, so the diff keeps the full height instead of permanently losing a strip to a
|
||||
* footer most reviews never touch. Hidden entirely where the host offers no verdicts, same as
|
||||
* the bar it wraps did.
|
||||
*/
|
||||
const reviewOverlay =
|
||||
review.verdicts.length === 0 ? null : (
|
||||
<div className="pointer-events-none absolute inset-x-0 bottom-0 z-10">
|
||||
{reviewOpen ? (
|
||||
<div
|
||||
className={cn(
|
||||
"surface-glass pointer-events-auto absolute inset-x-3 rounded-xl border border-border/60 shadow-lg",
|
||||
detail.capabilities.comment && detail.viewerPermissions.comment
|
||||
? "bottom-16"
|
||||
: "bottom-3",
|
||||
)}
|
||||
>
|
||||
<Button
|
||||
type="button"
|
||||
size="icon-sm"
|
||||
variant="ghost"
|
||||
aria-label="Close review"
|
||||
className="absolute right-2 top-2"
|
||||
onClick={() => setReviewOpen(false)}
|
||||
>
|
||||
<XIcon className="size-3.5" />
|
||||
</Button>
|
||||
<PullRequestReviewBar
|
||||
environmentId={environmentId}
|
||||
reference={reference}
|
||||
verdicts={review.verdicts}
|
||||
requestChangesSummaryRequired={detail.provider === "forgejo"}
|
||||
onSubmitted={() => {
|
||||
onRefresh();
|
||||
setReviewOpen(false);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<Button
|
||||
className={cn(
|
||||
"pointer-events-auto absolute bottom-3 rounded-full shadow-lg",
|
||||
detail.capabilities.comment && detail.viewerPermissions.comment
|
||||
? "right-16"
|
||||
: "right-4",
|
||||
)}
|
||||
onClick={() => setReviewOpen(true)}
|
||||
size="compact"
|
||||
variant="glass"
|
||||
>
|
||||
<MessageSquareIcon className="size-3.5" />
|
||||
Review
|
||||
{pendingComments.length > 0 ? (
|
||||
<span className="flex size-4 items-center justify-center rounded-full bg-accent text-[10px] tabular-nums text-accent-foreground">
|
||||
{pendingComments.length}
|
||||
</span>
|
||||
) : null}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
// A rebase or a force-push can take the scoped commit out of the change. Its diff may still
|
||||
// be reachable on the host, but it is no longer part of what is being reviewed, so the scope
|
||||
// goes back to the whole change rather than sitting under a name nothing matches.
|
||||
@@ -1392,29 +1321,23 @@ function PullRequestCodeTab({
|
||||
);
|
||||
// The toolbar rides above every branch below, not just the one with a patch in it: a commit
|
||||
// whose diff is empty or unreadable still needs the scope dropdown that got the reader there.
|
||||
const withReviewBar = (body: ReactNode) => (
|
||||
const withToolbar = (body: ReactNode) => (
|
||||
<div className="flex h-full min-h-0 flex-col">
|
||||
{toolbar}
|
||||
{/* The overlay is anchored to this wrapper, not the scroller: absolute positioning
|
||||
inside an overflowing element tracks the content's bottom edge, which would carry
|
||||
the trigger away with the first scroll. */}
|
||||
<div className="relative min-h-0 flex-1">
|
||||
<div className="h-full overflow-auto">{body}</div>
|
||||
{reviewOverlay}
|
||||
</div>
|
||||
<div className="min-h-0 flex-1 overflow-auto">{body}</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
// Under the toolbar rather than in place of it, so choosing a commit does not take the
|
||||
// dropdown that was just used off the screen while its diff loads.
|
||||
if (diffQuery.isPending && loadedSlices.length === 0) {
|
||||
return withReviewBar(<DiffPanelLoadingState label="Loading pull request diff..." />);
|
||||
return withToolbar(<DiffPanelLoadingState label="Loading pull request diff..." />);
|
||||
}
|
||||
|
||||
// A slice that fails once there are files on screen is reported at the end of them instead:
|
||||
// the diff already read is worth more than the error that stopped it growing.
|
||||
if (diffQuery.error && loadedSlices.length === 0) {
|
||||
return withReviewBar(
|
||||
return withToolbar(
|
||||
<p className="px-4 py-5 text-sm text-muted-foreground">{diffQuery.error}</p>,
|
||||
);
|
||||
}
|
||||
@@ -1428,7 +1351,7 @@ function PullRequestCodeTab({
|
||||
? parsedSlices.flatMap((parsed) => (parsed?.kind === "raw" ? [parsed] : []))
|
||||
: [];
|
||||
if (files.length === 0 && rawSlices.length > 0) {
|
||||
return withReviewBar(
|
||||
return withToolbar(
|
||||
<div className="space-y-4 px-4 py-5">
|
||||
{rawSlices.map((slice) => (
|
||||
<div key={`${slice.reason}:${slice.text.slice(0, 64)}`} className="space-y-2">
|
||||
@@ -1441,7 +1364,7 @@ function PullRequestCodeTab({
|
||||
}
|
||||
|
||||
if (items.length === 0 && nextCursor === null) {
|
||||
return withReviewBar(
|
||||
return withToolbar(
|
||||
<p className="px-4 py-5 text-sm text-muted-foreground">
|
||||
{commit === null
|
||||
? "This pull request has no file changes."
|
||||
@@ -1595,7 +1518,6 @@ function PullRequestCodeTab({
|
||||
renderAnnotation={renderAnnotation}
|
||||
unsafeCSSExtra={REPLACE_FILE_COUNTS_CSS}
|
||||
/>
|
||||
{reviewOverlay}
|
||||
</div>
|
||||
{fileTreeOpen ? (
|
||||
<aside className="flex w-[min(20rem,40%)] min-w-48 shrink-0 border-l border-border/60">
|
||||
|
||||
@@ -1,171 +0,0 @@
|
||||
import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
|
||||
import { MessageSquareIcon, SendIcon, XIcon } from "lucide-react";
|
||||
import { useRef, useState } from "react";
|
||||
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
import { pullRequestEnvironment } from "~/state/pullRequests";
|
||||
|
||||
import { Button } from "../ui/button";
|
||||
import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
|
||||
import { Textarea } from "../ui/textarea";
|
||||
import { toastManager } from "../ui/toast";
|
||||
import { PullRequestGlyph } from "./pullRequestIcons";
|
||||
|
||||
export function PullRequestCommentComposer({
|
||||
environmentId,
|
||||
reference,
|
||||
detail,
|
||||
actionPending,
|
||||
onCommentAction,
|
||||
onCommented,
|
||||
}: {
|
||||
environmentId: EnvironmentId;
|
||||
reference: PullRequestRef;
|
||||
detail: PullRequestDetailView;
|
||||
actionPending: boolean;
|
||||
onCommentAction: (
|
||||
body: string,
|
||||
action: "close" | "reopen",
|
||||
) => Promise<{ readonly commentPosted: boolean }>;
|
||||
onCommented: () => void;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [body, setBody] = useState("");
|
||||
const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
const postComment = useAtomCommand(pullRequestEnvironment.comment, { reportFailure: false });
|
||||
const followUpAction =
|
||||
detail.state === "open" &&
|
||||
detail.capabilities.actions.includes("close") &&
|
||||
detail.viewerPermissions.actions.includes("close")
|
||||
? ("close" as const)
|
||||
: detail.state === "closed" &&
|
||||
detail.capabilities.actions.includes("reopen") &&
|
||||
detail.viewerPermissions.actions.includes("reopen")
|
||||
? ("reopen" as const)
|
||||
: null;
|
||||
|
||||
const submit = async (action: "comment" | "close" | "reopen") => {
|
||||
const trimmed = body.trim();
|
||||
if (trimmed.length === 0 || submitting !== null || actionPending) return;
|
||||
setSubmitting(action);
|
||||
if (action !== "comment") {
|
||||
const result = await onCommentAction(trimmed, action);
|
||||
if (result.commentPosted) {
|
||||
setBody("");
|
||||
setOpen(false);
|
||||
}
|
||||
setSubmitting(null);
|
||||
return;
|
||||
}
|
||||
const result = await postComment({
|
||||
environmentId,
|
||||
input: {
|
||||
...reference,
|
||||
body: trimmed,
|
||||
},
|
||||
});
|
||||
if (result._tag === "Failure") {
|
||||
setSubmitting(null);
|
||||
toastManager.add({ type: "error", title: "Could not post the comment" });
|
||||
return;
|
||||
}
|
||||
setBody("");
|
||||
setSubmitting(null);
|
||||
setOpen(false);
|
||||
onCommented();
|
||||
};
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger
|
||||
render={
|
||||
<Button
|
||||
size="icon"
|
||||
variant="glass"
|
||||
className="rounded-full shadow-lg [--glass-opacity:50%]"
|
||||
/>
|
||||
}
|
||||
aria-label="Comment on pull request"
|
||||
>
|
||||
<MessageSquareIcon className="size-4" />
|
||||
</PopoverTrigger>
|
||||
<PopoverPopup
|
||||
side="top"
|
||||
align="end"
|
||||
sideOffset={8}
|
||||
className="w-96 max-w-[calc(100vw-2rem)]"
|
||||
initialFocus={textareaRef}
|
||||
>
|
||||
<div className="mb-3 flex items-center justify-between gap-2">
|
||||
<PopoverTitle className="text-sm">Comment on pull request</PopoverTitle>
|
||||
<PopoverClose
|
||||
render={<Button size="icon-xs" variant="ghost" />}
|
||||
aria-label="Close comment composer"
|
||||
>
|
||||
<XIcon className="size-3.5" />
|
||||
</PopoverClose>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Textarea
|
||||
ref={textareaRef}
|
||||
className="[&_textarea]:max-h-64"
|
||||
// Locked while posting: the body is cleared on success, which would otherwise throw
|
||||
// away a new draft typed while the request was still in flight.
|
||||
disabled={submitting !== null || actionPending}
|
||||
value={body}
|
||||
rows={3}
|
||||
placeholder="Leave a comment"
|
||||
aria-label="Comment on this pull request"
|
||||
onChange={(event) => setBody(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.nativeEvent.isComposing || event.keyCode === 229) return;
|
||||
if (
|
||||
event.key === "Enter" &&
|
||||
(event.metaKey || event.ctrlKey) &&
|
||||
!event.shiftKey &&
|
||||
!event.altKey
|
||||
) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (!event.repeat) void submit("comment");
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
{followUpAction === null ? null : (
|
||||
<Button
|
||||
size="xs"
|
||||
variant={followUpAction === "close" ? "destructive-outline" : "outline"}
|
||||
disabled={body.trim().length === 0 || submitting !== null || actionPending}
|
||||
onClick={() => void submit(followUpAction)}
|
||||
>
|
||||
{followUpAction === "close" ? (
|
||||
<PullRequestGlyph.closed className="size-3.5" />
|
||||
) : (
|
||||
<PullRequestGlyph.reopen className="size-3.5" />
|
||||
)}
|
||||
{submitting === followUpAction
|
||||
? followUpAction === "close"
|
||||
? "Closing..."
|
||||
: "Reopening..."
|
||||
: followUpAction === "close"
|
||||
? "Close with comment"
|
||||
: "Reopen with comment"}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
size="xs"
|
||||
variant="outline"
|
||||
disabled={body.trim().length === 0 || submitting !== null || actionPending}
|
||||
onClick={() => void submit("comment")}
|
||||
>
|
||||
<SendIcon className="size-3.5" />
|
||||
{submitting === "comment" ? "Posting..." : "Comment"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</PopoverPopup>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* The comment half of the floating composer: a remark on the pull request itself, optionally
|
||||
* the one that closes or reopens it. The popover around it belongs to PullRequestComposer.
|
||||
*/
|
||||
import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
|
||||
import { SendIcon } from "lucide-react";
|
||||
import { useState, type RefObject } from "react";
|
||||
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
import { pullRequestEnvironment } from "~/state/pullRequests";
|
||||
|
||||
import { Button } from "../ui/button";
|
||||
import { Textarea } from "../ui/textarea";
|
||||
import { toastManager } from "../ui/toast";
|
||||
import { PullRequestGlyph } from "./pullRequestIcons";
|
||||
|
||||
export function PullRequestCommentForm({
|
||||
environmentId,
|
||||
reference,
|
||||
detail,
|
||||
actionPending,
|
||||
textareaRef,
|
||||
onCommentAction,
|
||||
onCommented,
|
||||
onClose,
|
||||
}: {
|
||||
environmentId: EnvironmentId;
|
||||
reference: PullRequestRef;
|
||||
detail: PullRequestDetailView;
|
||||
actionPending: boolean;
|
||||
textareaRef: RefObject<HTMLTextAreaElement | null>;
|
||||
onCommentAction: (
|
||||
body: string,
|
||||
action: "close" | "reopen",
|
||||
) => Promise<{ readonly commentPosted: boolean }>;
|
||||
onCommented: () => void;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const [body, setBody] = useState("");
|
||||
const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
|
||||
const postComment = useAtomCommand(pullRequestEnvironment.comment, { reportFailure: false });
|
||||
const followUpAction =
|
||||
detail.state === "open" &&
|
||||
detail.capabilities.actions.includes("close") &&
|
||||
detail.viewerPermissions.actions.includes("close")
|
||||
? ("close" as const)
|
||||
: detail.state === "closed" &&
|
||||
detail.capabilities.actions.includes("reopen") &&
|
||||
detail.viewerPermissions.actions.includes("reopen")
|
||||
? ("reopen" as const)
|
||||
: null;
|
||||
|
||||
const submit = async (action: "comment" | "close" | "reopen") => {
|
||||
const trimmed = body.trim();
|
||||
if (trimmed.length === 0 || submitting !== null || actionPending) return;
|
||||
setSubmitting(action);
|
||||
if (action !== "comment") {
|
||||
const result = await onCommentAction(trimmed, action);
|
||||
if (result.commentPosted) {
|
||||
setBody("");
|
||||
onClose();
|
||||
}
|
||||
setSubmitting(null);
|
||||
return;
|
||||
}
|
||||
const result = await postComment({
|
||||
environmentId,
|
||||
input: {
|
||||
...reference,
|
||||
body: trimmed,
|
||||
},
|
||||
});
|
||||
if (result._tag === "Failure") {
|
||||
setSubmitting(null);
|
||||
toastManager.add({ type: "error", title: "Could not post the comment" });
|
||||
return;
|
||||
}
|
||||
setBody("");
|
||||
setSubmitting(null);
|
||||
onClose();
|
||||
onCommented();
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Textarea
|
||||
ref={textareaRef}
|
||||
className="[&_textarea]:max-h-64"
|
||||
// Locked while posting: the body is cleared on success, which would otherwise throw
|
||||
// away a new draft typed while the request was still in flight.
|
||||
disabled={submitting !== null || actionPending}
|
||||
value={body}
|
||||
rows={3}
|
||||
placeholder="Leave a comment"
|
||||
aria-label="Comment on this pull request"
|
||||
onChange={(event) => setBody(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.nativeEvent.isComposing || event.keyCode === 229) return;
|
||||
if (
|
||||
event.key === "Enter" &&
|
||||
(event.metaKey || event.ctrlKey) &&
|
||||
!event.shiftKey &&
|
||||
!event.altKey
|
||||
) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (!event.repeat) void submit("comment");
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
{followUpAction === null ? null : (
|
||||
<Button
|
||||
size="xs"
|
||||
variant={followUpAction === "close" ? "destructive-outline" : "outline"}
|
||||
disabled={body.trim().length === 0 || submitting !== null || actionPending}
|
||||
onClick={() => void submit(followUpAction)}
|
||||
>
|
||||
{followUpAction === "close" ? (
|
||||
<PullRequestGlyph.closed className="size-3.5" />
|
||||
) : (
|
||||
<PullRequestGlyph.reopen className="size-3.5" />
|
||||
)}
|
||||
{submitting === followUpAction
|
||||
? followUpAction === "close"
|
||||
? "Closing..."
|
||||
: "Reopening..."
|
||||
: followUpAction === "close"
|
||||
? "Close with comment"
|
||||
: "Reopen with comment"}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
size="xs"
|
||||
variant="outline"
|
||||
disabled={body.trim().length === 0 || submitting !== null || actionPending}
|
||||
onClick={() => void submit("comment")}
|
||||
>
|
||||
<SendIcon className="size-3.5" />
|
||||
{submitting === "comment" ? "Posting..." : "Comment"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* The single floating control over a pull request. Commenting on the change and submitting the
|
||||
* review that carries the Code tab's line comments used to float as two buttons that crowded
|
||||
* each other and read as the same offer twice; they are two modes of one composer now.
|
||||
*
|
||||
* Opening picks the mode with work waiting in it, so a reader who has collected line comments
|
||||
* lands on the review and everyone else lands on the comment box. Either mode's draft survives
|
||||
* the toggle: they are separate texts going to separate places, and merging them would send a
|
||||
* summary as a comment or the reverse.
|
||||
*/
|
||||
import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
|
||||
import { MessageSquareIcon, Trash2Icon, XIcon } from "lucide-react";
|
||||
import { useRef, useState } from "react";
|
||||
|
||||
import { Button } from "../ui/button";
|
||||
import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
|
||||
import { Toggle, ToggleGroup } from "../ui/toggle-group";
|
||||
import { PullRequestCommentForm } from "./PullRequestCommentForm";
|
||||
import { PullRequestReviewForm } from "./PullRequestReviewForm";
|
||||
import {
|
||||
pullRequestReviewKey,
|
||||
usePendingReviewComments,
|
||||
usePullRequestReviewStore,
|
||||
} from "./pullRequestReviewStore";
|
||||
|
||||
export function PullRequestComposer({
|
||||
environmentId,
|
||||
reference,
|
||||
detail,
|
||||
actionPending,
|
||||
onCommentAction,
|
||||
onCommented,
|
||||
onReviewSubmitted,
|
||||
}: {
|
||||
environmentId: EnvironmentId;
|
||||
reference: PullRequestRef;
|
||||
detail: PullRequestDetailView;
|
||||
actionPending: boolean;
|
||||
onCommentAction: (
|
||||
body: string,
|
||||
action: "close" | "reopen",
|
||||
) => Promise<{ readonly commentPosted: boolean }>;
|
||||
onCommented: () => void;
|
||||
onReviewSubmitted: () => void;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [reviewPending, setReviewPending] = useState(false);
|
||||
const [requestedMode, setRequestedMode] = useState<"comment" | "review">("comment");
|
||||
const commentRef = useRef<HTMLTextAreaElement>(null);
|
||||
const reviewRef = useRef<HTMLTextAreaElement>(null);
|
||||
const pendingComments = usePendingReviewComments(reference);
|
||||
const clearComments = usePullRequestReviewStore((store) => store.clear);
|
||||
// A summary typed but not sent is review work too, and it outlives the popover. Selected as a
|
||||
// boolean rather than the text, so typing one does not re-render the composer per keystroke.
|
||||
const reviewKey = pullRequestReviewKey(reference);
|
||||
const summaryStarted = usePullRequestReviewStore(
|
||||
(store) => (store.summaries[reviewKey] ?? "").trim().length > 0,
|
||||
);
|
||||
const reviewStarted = pendingComments.length > 0 || summaryStarted;
|
||||
|
||||
// What is offered is the intersection of two different questions: what this host can do at
|
||||
// all, and what this account may do on this repository. Either one saying no means a control
|
||||
// that would only ever end in a refusal.
|
||||
const canComment = detail.capabilities.comment && detail.viewerPermissions.comment;
|
||||
const verdicts = detail.capabilities.review.verdicts.filter((verdict) =>
|
||||
detail.viewerPermissions.verdicts.includes(verdict),
|
||||
);
|
||||
if (!canComment && verdicts.length === 0) return null;
|
||||
|
||||
const mode = canComment ? (verdicts.length === 0 ? "comment" : requestedMode) : "review";
|
||||
|
||||
return (
|
||||
<Popover
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (next) setRequestedMode(reviewStarted ? "review" : "comment");
|
||||
setOpen(next);
|
||||
}}
|
||||
>
|
||||
<PopoverTrigger
|
||||
render={
|
||||
<Button
|
||||
size="icon"
|
||||
variant="glass"
|
||||
className="rounded-full shadow-lg [--glass-opacity:50%]"
|
||||
/>
|
||||
}
|
||||
// The only label this control has, so the pending count belongs in it: the badge beside
|
||||
// the icon is decorative and a reader who cannot see it still needs the number.
|
||||
aria-label={
|
||||
pendingComments.length > 0
|
||||
? `Review pull request, ${pendingComments.length} ${pendingComments.length === 1 ? "comment" : "comments"} pending`
|
||||
: reviewStarted || !canComment
|
||||
? "Review pull request"
|
||||
: "Comment on pull request"
|
||||
}
|
||||
>
|
||||
<MessageSquareIcon className="size-4" />
|
||||
{pendingComments.length > 0 ? (
|
||||
<span
|
||||
aria-hidden
|
||||
className="absolute -top-1 -right-1 flex h-3.5 min-w-3.5 items-center justify-center rounded-full bg-info px-1 text-[9px] font-semibold tabular-nums text-white"
|
||||
>
|
||||
{pendingComments.length}
|
||||
</span>
|
||||
) : null}
|
||||
</PopoverTrigger>
|
||||
<PopoverPopup
|
||||
keepMounted
|
||||
side="top"
|
||||
align="end"
|
||||
sideOffset={8}
|
||||
className="w-96 max-w-[calc(100vw-2rem)]"
|
||||
initialFocus={mode === "review" ? reviewRef : commentRef}
|
||||
aria-label="Pull request composer"
|
||||
>
|
||||
<div className="mb-3 flex items-center justify-between gap-2">
|
||||
{canComment && verdicts.length > 0 ? (
|
||||
<ToggleGroup
|
||||
aria-label="Composer mode"
|
||||
variant="segmented"
|
||||
value={[mode]}
|
||||
onValueChange={(next) => {
|
||||
const value = next[0];
|
||||
if (value === "comment" || value === "review") setRequestedMode(value);
|
||||
}}
|
||||
>
|
||||
<Toggle value="comment">Comment</Toggle>
|
||||
<Toggle value="review">
|
||||
{pendingComments.length > 0 ? `Review (${pendingComments.length})` : "Review"}
|
||||
</Toggle>
|
||||
</ToggleGroup>
|
||||
) : (
|
||||
<PopoverTitle className="text-sm">
|
||||
{mode === "review" ? "Review pull request" : "Comment on pull request"}
|
||||
</PopoverTitle>
|
||||
)}
|
||||
<div className="flex items-center gap-1">
|
||||
{mode === "review" && pendingComments.length > 0 ? (
|
||||
<Button
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
aria-label="Discard pending line comments"
|
||||
title="Discard pending line comments"
|
||||
disabled={reviewPending}
|
||||
onClick={() => clearComments(reviewKey)}
|
||||
>
|
||||
<Trash2Icon className="size-3.5" />
|
||||
</Button>
|
||||
) : null}
|
||||
<PopoverClose
|
||||
render={<Button size="icon-xs" variant="ghost" />}
|
||||
aria-label="Close composer"
|
||||
>
|
||||
<XIcon className="size-3.5" />
|
||||
</PopoverClose>
|
||||
</div>
|
||||
</div>
|
||||
{/* Keep both forms mounted across toggles and dismissal so drafts and in-flight
|
||||
submit guards survive closing and reopening the composer. */}
|
||||
{verdicts.length > 0 ? (
|
||||
<div hidden={mode !== "review"}>
|
||||
<PullRequestReviewForm
|
||||
environmentId={environmentId}
|
||||
reference={reference}
|
||||
verdicts={verdicts}
|
||||
requestChangesSummaryRequired={detail.provider === "forgejo"}
|
||||
textareaRef={reviewRef}
|
||||
pending={reviewPending}
|
||||
onPendingChange={setReviewPending}
|
||||
onSubmitted={() => {
|
||||
setOpen(false);
|
||||
onReviewSubmitted();
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
{canComment ? (
|
||||
<div hidden={mode !== "comment"}>
|
||||
<PullRequestCommentForm
|
||||
environmentId={environmentId}
|
||||
reference={reference}
|
||||
detail={detail}
|
||||
actionPending={actionPending}
|
||||
textareaRef={commentRef}
|
||||
onCommentAction={onCommentAction}
|
||||
onCommented={onCommented}
|
||||
onClose={() => setOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
</PopoverPopup>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
@@ -130,7 +130,7 @@ import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState";
|
||||
import type { PullRequestAgentSelectionInput } from "./PullRequestCodeTab";
|
||||
import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLinkContextMenu";
|
||||
import { PullRequestMarkdownContext } from "./PullRequestMarkdown";
|
||||
import { PullRequestCommentComposer } from "./PullRequestCommentComposer";
|
||||
import { PullRequestComposer } from "./PullRequestComposer";
|
||||
import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
|
||||
import { PullRequestTimelineTab } from "./PullRequestTimelineTab";
|
||||
import {
|
||||
@@ -2793,9 +2793,9 @@ export function PullRequestDetailPanel({
|
||||
</div>
|
||||
|
||||
{/* Float over the content; do not reserve a footer or padding in the PR tabs. */}
|
||||
{detail?.capabilities.comment && detail.viewerPermissions.comment ? (
|
||||
{detail ? (
|
||||
<div className="absolute right-4 bottom-3 z-20">
|
||||
<PullRequestCommentComposer
|
||||
<PullRequestComposer
|
||||
key={JSON.stringify([
|
||||
environmentId,
|
||||
reference.projectId,
|
||||
@@ -2809,6 +2809,7 @@ export function PullRequestDetailPanel({
|
||||
actionPending={actionPending}
|
||||
onCommentAction={performCommentAction}
|
||||
onCommented={refreshDetail}
|
||||
onReviewSubmitted={refreshDetail}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
+55
-40
@@ -1,17 +1,18 @@
|
||||
/**
|
||||
* The review form floated over the Code tab: how many comments the review is holding, its
|
||||
* summary, and the verdict that sends the lot. Hidden entirely on a host that cannot take a
|
||||
* review. The glass card frame belongs to the caller (PullRequestCodeTab), which is why this
|
||||
* only contributes its own padding.
|
||||
* The review half of the floating composer: the summary and the verdict that sends it, together
|
||||
* with whatever line comments the review is holding. The count of those lives on the composer's
|
||||
* trigger and mode toggle, and each pending card can be dropped from the diff, so neither is
|
||||
* repeated here. The popover around it belongs to PullRequestComposer.
|
||||
*/
|
||||
import type { EnvironmentId, PullRequestRef, PullRequestReviewVerdict } from "@t3tools/contracts";
|
||||
import { CheckIcon, MessageSquareIcon, XCircleIcon } from "lucide-react";
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { useState, type ReactNode, type RefObject } from "react";
|
||||
|
||||
import { pullRequestEnvironment } from "~/state/pullRequests";
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
|
||||
import { Button } from "../ui/button";
|
||||
import { Select, SelectItem, SelectPopup, SelectTrigger } from "../ui/select";
|
||||
import { Textarea } from "../ui/textarea";
|
||||
import { toastManager } from "../ui/toast";
|
||||
import {
|
||||
@@ -46,27 +47,32 @@ const VERDICTS: ReadonlyArray<{
|
||||
},
|
||||
];
|
||||
|
||||
export function PullRequestReviewBar({
|
||||
export function PullRequestReviewForm({
|
||||
environmentId,
|
||||
reference,
|
||||
verdicts,
|
||||
requestChangesSummaryRequired,
|
||||
textareaRef,
|
||||
pending,
|
||||
onPendingChange,
|
||||
onSubmitted,
|
||||
}: {
|
||||
environmentId: EnvironmentId;
|
||||
reference: PullRequestRef;
|
||||
verdicts: ReadonlyArray<PullRequestReviewVerdict>;
|
||||
requestChangesSummaryRequired: boolean;
|
||||
textareaRef: RefObject<HTMLTextAreaElement | null>;
|
||||
pending: boolean;
|
||||
onPendingChange: (pending: boolean) => void;
|
||||
onSubmitted: () => void;
|
||||
}) {
|
||||
const [pending, setPending] = useState(false);
|
||||
const [requestedVerdict, setRequestedVerdict] = useState<PullRequestReviewVerdict>("comment");
|
||||
const comments = usePendingReviewComments(reference);
|
||||
const reviewKey = pullRequestReviewKey(reference);
|
||||
// The panel stays mounted while the selected pull request changes. Keeping summaries beside
|
||||
// the keyed line-comment drafts makes the selected pull request's body correct on the first
|
||||
// render, before an effect could reset state left behind by the previous one.
|
||||
const body = usePullRequestReviewStore((store) => store.summaries[reviewKey] ?? "");
|
||||
const clear = usePullRequestReviewStore((store) => store.clear);
|
||||
const removeComments = usePullRequestReviewStore((store) => store.removeComments);
|
||||
const setSummary = usePullRequestReviewStore((store) => store.setSummary);
|
||||
const clearSummary = usePullRequestReviewStore((store) => store.clearSummary);
|
||||
@@ -75,13 +81,14 @@ export function PullRequestReviewBar({
|
||||
});
|
||||
|
||||
const offered = VERDICTS.filter((verdict) => verdicts.includes(verdict.value));
|
||||
if (offered.length === 0) return null;
|
||||
const selectedVerdict =
|
||||
offered.find((verdict) => verdict.value === requestedVerdict) ?? offered[0];
|
||||
|
||||
const submit = async (verdict: (typeof VERDICTS)[number]) => {
|
||||
if (pending) return;
|
||||
const submittedBody = body;
|
||||
const submittedComments = comments;
|
||||
setPending(true);
|
||||
onPendingChange(true);
|
||||
const result = await submitReview({
|
||||
environmentId,
|
||||
input: {
|
||||
@@ -91,7 +98,7 @@ export function PullRequestReviewBar({
|
||||
comments: submittedComments,
|
||||
},
|
||||
});
|
||||
setPending(false);
|
||||
onPendingChange(false);
|
||||
if (result._tag === "Failure") {
|
||||
// The draft is kept: whatever went wrong, retyping the review is not the answer.
|
||||
toastManager.add({ type: "error", title: "The review could not be submitted" });
|
||||
@@ -115,22 +122,11 @@ export function PullRequestReviewBar({
|
||||
: verdict === "approve" || body.trim().length > 0 || comments.length > 0;
|
||||
|
||||
return (
|
||||
<div className="px-4 py-3">
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span>
|
||||
{comments.length === 0
|
||||
? "No line comments yet"
|
||||
: `${comments.length} ${comments.length === 1 ? "comment" : "comments"} pending`}
|
||||
</span>
|
||||
{comments.length > 0 ? (
|
||||
<Button size="xs" variant="ghost" disabled={pending} onClick={() => clear(reviewKey)}>
|
||||
Discard
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
<>
|
||||
<Textarea
|
||||
size="sm"
|
||||
className="mt-2"
|
||||
ref={textareaRef}
|
||||
rows={3}
|
||||
className="[&_textarea]:max-h-64"
|
||||
value={body}
|
||||
placeholder={
|
||||
requestChangesSummaryRequired && verdicts.includes("request-changes")
|
||||
@@ -140,22 +136,41 @@ export function PullRequestReviewBar({
|
||||
aria-label="Review summary"
|
||||
onChange={(event) => setSummary(reviewKey, event.target.value)}
|
||||
/>
|
||||
<div className="mt-2 flex flex-wrap justify-end gap-2">
|
||||
{offered.map((verdict) => (
|
||||
<Button
|
||||
key={verdict.value}
|
||||
size="xs"
|
||||
variant={verdict.value === "comment" ? "outline" : "default"}
|
||||
disabled={pending || !canSubmit(verdict.value)}
|
||||
onClick={() => void submit(verdict)}
|
||||
>
|
||||
<div className="mt-2 flex justify-between gap-2">
|
||||
<Select
|
||||
value={selectedVerdict?.value ?? null}
|
||||
disabled={pending}
|
||||
onValueChange={(value) => {
|
||||
if (value !== null) setRequestedVerdict(value);
|
||||
}}
|
||||
>
|
||||
<SelectTrigger size="xs" className="w-auto min-w-0" aria-label="Review verdict">
|
||||
<span className="flex items-center gap-1.5">
|
||||
{verdict.icon}
|
||||
{verdict.label}
|
||||
{selectedVerdict?.icon}
|
||||
{selectedVerdict?.label}
|
||||
</span>
|
||||
</Button>
|
||||
))}
|
||||
</SelectTrigger>
|
||||
<SelectPopup side="top" alignItemWithTrigger={false}>
|
||||
{offered.map((verdict) => (
|
||||
<SelectItem key={verdict.value} value={verdict.value}>
|
||||
<span className="flex items-center gap-1.5">
|
||||
{verdict.icon}
|
||||
{verdict.label}
|
||||
</span>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectPopup>
|
||||
</Select>
|
||||
<Button
|
||||
size="xs"
|
||||
disabled={pending || selectedVerdict === undefined || !canSubmit(selectedVerdict.value)}
|
||||
onClick={() => {
|
||||
if (selectedVerdict !== undefined) void submit(selectedVerdict);
|
||||
}}
|
||||
>
|
||||
{pending ? "Submitting..." : "Submit review"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -25,6 +25,7 @@ function PopoverPopup({
|
||||
sideOffset = 4,
|
||||
alignOffset = 0,
|
||||
tooltipStyle = false,
|
||||
keepMounted = false,
|
||||
anchor,
|
||||
...props
|
||||
}: PopoverPrimitive.Popup.Props & {
|
||||
@@ -34,10 +35,14 @@ function PopoverPopup({
|
||||
sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
|
||||
alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
|
||||
tooltipStyle?: boolean;
|
||||
keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
|
||||
anchor?: PopoverPrimitive.Positioner.Props["anchor"];
|
||||
}) {
|
||||
// Viewport rekeys its children when the active trigger clears on close. Persistent
|
||||
// single-trigger forms need a stable container to retain drafts and submit guards.
|
||||
const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
|
||||
return (
|
||||
<PopoverPrimitive.Portal>
|
||||
<PopoverPrimitive.Portal keepMounted={keepMounted}>
|
||||
<PopoverPrimitive.Positioner
|
||||
align={align}
|
||||
alignOffset={alignOffset}
|
||||
@@ -59,7 +64,7 @@ function PopoverPopup({
|
||||
data-slot="popover-popup"
|
||||
{...props}
|
||||
>
|
||||
<PopoverPrimitive.Viewport
|
||||
<Viewport
|
||||
className={cn(
|
||||
"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",
|
||||
tooltipStyle
|
||||
@@ -70,7 +75,7 @@ function PopoverPopup({
|
||||
data-slot="popover-viewport"
|
||||
>
|
||||
{children}
|
||||
</PopoverPrimitive.Viewport>
|
||||
</Viewport>
|
||||
</PopoverPrimitive.Popup>
|
||||
</PopoverPrimitive.Positioner>
|
||||
</PopoverPrimitive.Portal>
|
||||
|
||||
Reference in new issue
Block a user