Repository navigation
Expand file tree
/
Copy pathPullRequestCommentForm.tsx
More file actions
190 lines (185 loc) · 6.69 KB
/
Copy pathPullRequestCommentForm.tsx
File metadata and controls
190 lines (185 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import { useAtomCommand } from "~/state/use-atom-command";
import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
import { SendIcon } from "lucide-react";
import { useEffect, useState, type RefObject } from "react";
import { TextFieldSpeechControls, useTextFieldSpeech } from "~/speech/useTextFieldSpeech";
import { pullRequestEnvironment } from "~/state/pullRequests";
import { Button } from "../ui/button";
import { toastManager } from "../ui/toast";
import { PullRequestGlyph } from "./pullRequestIcons";
import {
PullRequestMarkdownTextarea,
pullRequestCompletionScope,
} from "./PullRequestMarkdownTextarea";
import { pullRequestReviewKey } from "./pullRequestReviewStore";
import { useInlineActionConfirm } from "./useInlineActionConfirm";
export function PullRequestCommentForm({
environmentId,
active,
reference,
detail,
actionPending,
textareaRef,
onCommentAction,
onCommented,
onClose,
}: {
environmentId: EnvironmentId;
/** False while the form stays mounted but hidden; dictation is cancelled then. */
active: boolean;
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 locked = submitting !== null || actionPending;
const speech = useTextFieldSpeech({
environmentId,
projectId: reference.projectId,
ownerKey: JSON.stringify(["pr-comment", environmentId, reference]),
text: body,
textareaRef,
onTextChange: setBody,
disabled: !active || locked,
});
const { cancel } = speech;
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- discards a recording when the hidden form can no longer be submitted
useEffect(() => {
if (!active) cancel();
}, [active, cancel]);
const postComment = useAtomCommand(pullRequestEnvironment.comment, {
reportFailure: false,
});
const inlineConfirm = useInlineActionConfirm(pullRequestReviewKey(reference));
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", text: string) => {
const trimmed = text.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();
};
const submitAfterDictation = (action: "comment" | "close" | "reopen") =>
speech.submitAfterDictation((text) => void submit(action, text));
const submitDisabled =
speech.preparing || (body.trim().length === 0 && !speech.blocksSubmission) || locked;
return (
<div className="space-y-2">
<PullRequestMarkdownTextarea
ref={textareaRef}
completionScope={pullRequestCompletionScope(detail, environmentId, reference)}
// Lock while posting because success clears the draft.
disabled={locked}
readOnly={speech.freezesEditor}
value={body}
rows={3}
placeholder="Leave a comment"
aria-label={`Comment on this ${sourceControlClients.get(detail.provider).changeRequest.singular}`}
onValueChange={setBody}
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) submitAfterDictation("comment");
}
}}
/>
<div className="flex flex-wrap items-center justify-end gap-2">
<div className="mr-auto flex min-w-0 flex-wrap items-center gap-1">
<TextFieldSpeechControls speech={speech} disabled={locked} />
</div>
{followUpAction === null ? null : (
<Button
size="xs"
variant={followUpAction === "close" ? "destructive-outline" : "outline"}
disabled={submitDisabled}
{...(followUpAction === "close"
? inlineConfirm.props("close", () => submitAfterDictation("close"))
: { onClick: () => submitAfterDictation("reopen") })}
>
{followUpAction === "close" ? (
<PullRequestGlyph.closed className="size-3.5" />
) : (
<PullRequestGlyph.reopen className="size-3.5" />
)}
{followUpAction === "close" ? (
// The longest label holds the width, so the confirming click lands on the button.
<span className="inline-grid">
<span aria-hidden className="invisible col-start-1 row-start-1">
Close with comment
</span>
<span className="col-start-1 row-start-1">
{submitting === "close"
? "Closing..."
: inlineConfirm.isArmed("close")
? "Confirm close?"
: "Close with comment"}
</span>
</span>
) : submitting === "reopen" ? (
"Reopening..."
) : (
"Reopen with comment"
)}
</Button>
)}
<Button
size="xs"
variant="outline"
disabled={submitDisabled}
onClick={() => submitAfterDictation("comment")}
>
<SendIcon className="size-3.5" />
{submitting === "comment" ? "Posting..." : "Comment"}
</Button>
</div>
</div>
);
}