Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ vi.mock(import("~/state/pullRequests"), async (original) => {

import { render } from "../../../test/render";
import PullRequestCodeTab from "./PullRequestCodeTab";
import { usePullRequestReviewStore } from "./pullRequestReviewStore";

const detail: PullRequestDetailView = {
provider: SourceControlProviderKind.make("github"),
Expand Down Expand Up @@ -176,6 +177,7 @@ function setup({ paged }: { paged: boolean }) {
mocks.diffCommits = [];
mocks.viewedPaths = null;
mocks.viewedListeners.clear();
usePullRequestReviewStore.setState({ replies: {} });
}

const tab = (
Expand Down
34 changes: 34 additions & 0 deletions apps/web/src/components/pullRequest/PullRequestCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ import { useInlineActionConfirm } from "./useInlineActionConfirm";

import { changeRequestTermKind } from "./changeRequestTerm";

/** A fresh object each time, so quoting the same comment twice still lands. */
export type PullRequestQuoteRequest = { readonly text: string };

export function PullRequestCommentForm({
environmentId,
active,
reference,
detail,
actionPending,
textareaRef,
quoteRequest,
onCommentAction,
onCommented,
onClose,
onQuoted,
}: {
environmentId: EnvironmentId;
/** False while the form stays mounted but hidden; dictation is cancelled then. */
Expand All @@ -40,17 +45,46 @@ export function PullRequestCommentForm({
detail: PullRequestDetailView;
actionPending: boolean;
textareaRef: RefObject<HTMLTextAreaElement | null>;
quoteRequest?: PullRequestQuoteRequest | undefined;
onCommentAction: (
body: string,
action: "close" | "reopen",
) => Promise<{ readonly commentPosted: boolean }>;
onCommented: () => void;
onClose: () => void;
/** A quote deferred past a successful post lands after `onClose`, so the form asks to be shown again. */
onQuoted: () => void;
}) {
const gt = useGT();
const [body, setBody] = useState("");
const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
const locked = submitting !== null || actionPending;
const [answeredQuoteRequest, setAnsweredQuoteRequest] = useState(quoteRequest);
const [pendingQuotes, setPendingQuotes] = useState<ReadonlyArray<string>>([]);
const [caretAfterQuote, setCaretAfterQuote] = useState(false);
let quotes = pendingQuotes;
if (quoteRequest !== answeredQuoteRequest) {
setAnsweredQuoteRequest(quoteRequest);
if (quoteRequest) quotes = [...quotes, quoteRequest.text];
}
// A successful post clears the draft, so quotes wait until posting settles.
if (quotes.length > 0 && !locked) {
setPendingQuotes([]);
setBody((draft) => [draft.trimEnd(), ...quotes].filter((part) => part.length > 0).join("\n\n"));
setCaretAfterQuote(true);
} else if (quotes !== pendingQuotes) {
setPendingQuotes(quotes);
}
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reveals the composer and moves the DOM caret after a quote lands
useEffect(() => {
const textarea = textareaRef.current;
if (!caretAfterQuote || !textarea) return;
setCaretAfterQuote(false);
onQuoted();
textarea.focus({ preventScroll: true });
textarea.setSelectionRange(textarea.value.length, textarea.value.length);
textarea.scrollTop = textarea.scrollHeight;
}, [caretAfterQuote, textareaRef, onQuoted]);
const speech = useTextFieldSpeech({
environmentId,
projectId: reference.projectId,
Expand Down
15 changes: 14 additions & 1 deletion apps/web/src/components/pullRequest/PullRequestComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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 { PullRequestCommentForm, type PullRequestQuoteRequest } from "./PullRequestCommentForm";
import { PullRequestReviewForm } from "./PullRequestReviewForm";
import {
pullRequestReviewKey,
Expand All @@ -27,6 +27,7 @@ export function PullRequestComposer({
onCommented,
onReviewSubmitted,
openReviewRequest,
quoteRequest,
}: {
environmentId: EnvironmentId;
reference: PullRequestRef;
Expand All @@ -39,6 +40,7 @@ export function PullRequestComposer({
onCommented: () => void;
onReviewSubmitted: () => void;
openReviewRequest?: number | undefined;
quoteRequest?: PullRequestQuoteRequest | undefined;
}) {
const gt = useGT();
const term = changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest);
Expand All @@ -61,6 +63,12 @@ export function PullRequestComposer({
setRequestedMode("review");
setOpen(true);
}
const [answeredQuoteRequest, setAnsweredQuoteRequest] = useState(quoteRequest);
if (quoteRequest !== answeredQuoteRequest) {
setAnsweredQuoteRequest(quoteRequest);
setRequestedMode("comment");
setOpen(true);
}

const canComment = detail.capabilities.comment && detail.viewerPermissions.comment;
const verdicts = detail.capabilities.review.verdicts.filter((verdict) =>
Expand Down Expand Up @@ -205,9 +213,14 @@ export function PullRequestComposer({
detail={detail}
actionPending={actionPending}
textareaRef={commentRef}
quoteRequest={quoteRequest}
onCommentAction={onCommentAction}
onCommented={onCommented}
onClose={() => setOpen(false)}
onQuoted={() => {
setRequestedMode("comment");
setOpen(true);
}}
/>
</div>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import { openOnHostLabel } from "./pullRequestLinkContextMenu";
import { PullRequestMarkdownContext } from "./PullRequestMarkdown";
import { pullRequestMarkdownPlugins } from "./pullRequestMarkdown.logic";
import { PullRequestComposer } from "./PullRequestComposer";
import type { PullRequestQuoteRequest } from "./PullRequestCommentForm";
import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
import { PullRequestTimelineTab } from "./PullRequestTimelineTab";
import {
Expand Down Expand Up @@ -433,6 +434,7 @@ export function PullRequestDetailPanel({
useEnvironmentQuery(pullRequestEnvironment.changeBreakdown({ environmentId, input: reference }));
const [threadPickerOpen, setThreadPickerOpen] = useState(false);
const [openReviewRequest, setOpenReviewRequest] = useState(0);
const [quoteRequest, setQuoteRequest] = useState<PullRequestQuoteRequest>();
const {
canWriteSourceControl,
detailQuery,
Expand Down Expand Up @@ -2096,6 +2098,9 @@ export function PullRequestDetailPanel({
fixCheckLabel={handoffLabels.fixCheck}
{...(canFixFindings ? { onFixFinding: startFixFinding } : {})}
{...(attachTarget === null ? {} : { onAddToChat: addCommentToChat })}
{...(detail.capabilities.comment && detail.viewerPermissions.comment
? { onQuoteReply: (text: string) => setQuoteRequest({ text }) }
: {})}
onRefresh={refreshDetail}
onRefreshChecks={refreshFromHost}
/>
Expand Down Expand Up @@ -2217,6 +2222,7 @@ export function PullRequestDetailPanel({
onCommented={refreshDetail}
onReviewSubmitted={refreshDetail}
openReviewRequest={openReviewRequest}
quoteRequest={quoteRequest}
/>
</div>
) : null}
Expand Down
Loading
Loading