The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 2 | import type { EnvironmentId, PullRequestRef, PullRequestReviewVerdict } from "@t3tools/contracts"; |
| 3 | import { CheckIcon, MessageSquareIcon, XCircleIcon } from "lucide-react"; |
| 4 | import { msg, useGT, useMessages } from "gt-react"; |
| 5 | import { useEffect, useState, type ReactNode, type RefObject } from "react"; |
| 6 | |
| 7 | import { m } from "~/i18n/messages"; |
| 8 | |
| 9 | import { TextFieldSpeechControls, useTextFieldSpeech } from "~/speech/useTextFieldSpeech"; |
| 10 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 11 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 12 | |
| 13 | import { isCommentSubmitShortcut } from "../diffs/commentSubmitShortcut"; |
| 14 | import { Button } from "../ui/button"; |
| 15 | import { Select, SelectItem, SelectPopup, SelectTrigger } from "../ui/select"; |
| 16 | import { toastManager } from "../ui/toast"; |
| 17 | import { readableFailure } from "./pullRequestDetail.logic"; |
| 18 | import { |
| 19 | pullRequestReviewKey, |
| 20 | usePendingReviewComments, |
| 21 | usePullRequestReviewStore, |
| 22 | } from "./pullRequestReviewStore"; |
| 23 | import { |
| 24 | PullRequestMarkdownTextarea, |
| 25 | type PullRequestCompletionScope, |
| 26 | } from "./PullRequestMarkdownTextarea"; |
| 27 | |
| 28 | import type { ChangeRequestTermKind } from "./changeRequestTerm"; |
| 29 | |
| 30 | const VERDICTS: ReadonlyArray<{ |
| 31 | readonly value: PullRequestReviewVerdict; |
| 32 | readonly label: string; |
| 33 | readonly sent: (term: ChangeRequestTermKind) => string; |
| 34 | readonly icon: ReactNode; |
| 35 | }> = [ |
| 36 | { |
| 37 | value: "comment", |
| 38 | label: msg("Comment", { $context: "review verdict" }), |
| 39 | sent: () => m(msg("Review submitted")), |
| 40 | icon: <MessageSquareIcon className="size-3" />, |
| 41 | }, |
| 42 | { |
| 43 | value: "approve", |
| 44 | label: msg("Approve", { $context: "review verdict" }), |
| 45 | sent: (term) => |
| 46 | m( |
| 47 | msg( |
| 48 | "{term, select, mr {Merge request} cr {Change request} other {Pull request}} approved", |
| 49 | { term }, |
| 50 | ), |
| 51 | ), |
| 52 | icon: <CheckIcon className="size-3" />, |
| 53 | }, |
| 54 | { |
| 55 | value: "request-changes", |
| 56 | label: msg("Request changes"), |
| 57 | sent: () => m(msg("Changes requested")), |
| 58 | icon: <XCircleIcon className="size-3" />, |
| 59 | }, |
| 60 | ]; |
| 61 | |
| 62 | export function PullRequestReviewForm({ |
| 63 | environmentId, |
| 64 | active, |
| 65 | reference, |
| 66 | completionScope, |
| 67 | term, |
| 68 | verdicts, |
| 69 | requestChangesSummaryRequired, |
| 70 | textareaRef, |
| 71 | pending, |
| 72 | onPendingChange, |
| 73 | onSubmitted, |
| 74 | }: { |
| 75 | environmentId: EnvironmentId; |
| 76 | /** False while the form stays mounted but hidden; dictation is cancelled then. */ |
| 77 | active: boolean; |
| 78 | reference: PullRequestRef; |
| 79 | completionScope: PullRequestCompletionScope | null; |
| 80 | term: ChangeRequestTermKind; |
| 81 | verdicts: ReadonlyArray<PullRequestReviewVerdict>; |
| 82 | requestChangesSummaryRequired: boolean; |
| 83 | textareaRef: RefObject<HTMLTextAreaElement | null>; |
| 84 | pending: boolean; |
| 85 | onPendingChange: (pending: boolean) => void; |
| 86 | onSubmitted: () => void; |
| 87 | }) { |
| 88 | const gt = useGT(); |
| 89 | const resolve = useMessages(); |
| 90 | const [requestedVerdict, setRequestedVerdict] = useState<PullRequestReviewVerdict>("comment"); |
| 91 | const comments = usePendingReviewComments(reference); |
| 92 | const reviewKey = pullRequestReviewKey(reference); |
| 93 | const body = usePullRequestReviewStore((store) => store.summaries[reviewKey] ?? ""); |
| 94 | const removeComments = usePullRequestReviewStore((store) => store.removeComments); |
| 95 | const setSummary = usePullRequestReviewStore((store) => store.setSummary); |
| 96 | const speech = useTextFieldSpeech({ |
| 97 | environmentId, |
| 98 | projectId: reference.projectId, |
| 99 | ownerKey: JSON.stringify(["pr-review", environmentId, reviewKey]), |
| 100 | text: body, |
| 101 | textareaRef, |
| 102 | onTextChange: (text) => setSummary(reviewKey, text), |
| 103 | disabled: !active || pending, |
| 104 | }); |
| 105 | const { cancel } = speech; |
| 106 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- discards a recording when the hidden form can no longer be submitted |
| 107 | useEffect(() => { |
| 108 | if (!active) cancel(); |
| 109 | }, [active, cancel]); |
| 110 | const clearSummary = usePullRequestReviewStore((store) => store.clearSummary); |
| 111 | const submitReview = useAtomCommand(pullRequestEnvironment.submitReview, { |
| 112 | reportFailure: false, |
| 113 | }); |
| 114 | |
| 115 | const offered = VERDICTS.filter((verdict) => verdicts.includes(verdict.value)); |
| 116 | const selectedVerdict = |
| 117 | offered.find((verdict) => verdict.value === requestedVerdict) ?? offered[0]; |
| 118 | |
| 119 | const submit = async (verdict: (typeof VERDICTS)[number], text: string) => { |
| 120 | const submittedComments = usePullRequestReviewStore.getState().drafts[reviewKey] ?? []; |
| 121 | if (pending || !canSubmit(verdict.value, text, submittedComments.length)) return; |
| 122 | const submittedBody = text; |
| 123 | onPendingChange(true); |
| 124 | const result = await submitReview({ |
| 125 | environmentId, |
| 126 | input: { |
| 127 | ...reference, |
| 128 | verdict: verdict.value, |
| 129 | body: submittedBody, |
| 130 | comments: submittedComments, |
| 131 | }, |
| 132 | }); |
| 133 | onPendingChange(false); |
| 134 | if (result._tag === "Failure") { |
| 135 | toastManager.add({ |
| 136 | type: "error", |
| 137 | title: m(msg("The review could not be submitted")), |
| 138 | description: readableFailure( |
| 139 | squashAtomCommandFailure(result), |
| 140 | m(msg("The host refused the review. Your draft is still here to try again.")), |
| 141 | ), |
| 142 | }); |
| 143 | return; |
| 144 | } |
| 145 | // Preserve comments and summary edits made during submission. |
| 146 | removeComments( |
| 147 | reviewKey, |
| 148 | submittedComments.map((comment) => comment.id), |
| 149 | ); |
| 150 | clearSummary(reviewKey, submittedBody); |
| 151 | toastManager.add({ type: "success", title: verdict.sent(term) }); |
| 152 | onSubmitted(); |
| 153 | }; |
| 154 | |
| 155 | // Forgejo requires a summary when requesting changes, even with inline comments. |
| 156 | const canSubmit = (verdict: PullRequestReviewVerdict, summary: string, commentCount: number) => |
| 157 | verdict === "request-changes" && requestChangesSummaryRequired |
| 158 | ? summary.trim().length > 0 |
| 159 | : verdict === "approve" || summary.trim().length > 0 || commentCount > 0; |
| 160 | const placeholder = |
| 161 | selectedVerdict === undefined || canSubmit(selectedVerdict.value, "", comments.length) |
| 162 | ? gt("Summarize your review (optional)") |
| 163 | : selectedVerdict.value === "request-changes" && requestChangesSummaryRequired |
| 164 | ? gt("Summarize your review (required to request changes)") |
| 165 | : gt("Summarize your review or add line comments"); |
| 166 | |
| 167 | return ( |
| 168 | <> |
| 169 | <PullRequestMarkdownTextarea |
| 170 | ref={textareaRef} |
| 171 | completionScope={completionScope} |
| 172 | rows={3} |
| 173 | readOnly={speech.freezesEditor} |
| 174 | value={body} |
| 175 | placeholder={placeholder} |
| 176 | aria-label={gt("Review summary")} |
| 177 | onValueChange={(next) => setSummary(reviewKey, next)} |
| 178 | onKeyDown={(event) => { |
| 179 | if (event.nativeEvent.isComposing || event.keyCode === 229 || event.repeat) return; |
| 180 | if ( |
| 181 | selectedVerdict !== undefined && |
| 182 | isCommentSubmitShortcut(event, body, pending || speech.preparing, true) |
| 183 | ) { |
| 184 | event.preventDefault(); |
| 185 | event.stopPropagation(); |
| 186 | speech.submitAfterDictation((text) => void submit(selectedVerdict, text)); |
| 187 | } |
| 188 | }} |
| 189 | /> |
| 190 | <div className="mt-2 flex flex-wrap items-center justify-end gap-2"> |
| 191 | <TextFieldSpeechControls speech={speech} disabled={pending} /> |
| 192 | {offered.length > 1 ? ( |
| 193 | <Select |
| 194 | value={selectedVerdict?.value ?? null} |
| 195 | disabled={pending} |
| 196 | onValueChange={(value) => { |
| 197 | if (value !== null) setRequestedVerdict(value); |
| 198 | }} |
| 199 | > |
| 200 | <SelectTrigger size="xs" className="w-auto min-w-0" aria-label={gt("Review verdict")}> |
| 201 | <span className="flex items-center gap-1.5"> |
| 202 | {selectedVerdict?.icon} |
| 203 | {selectedVerdict ? resolve(selectedVerdict.label) : null} |
| 204 | </span> |
| 205 | </SelectTrigger> |
| 206 | <SelectPopup side="top" alignItemWithTrigger={false}> |
| 207 | {offered.map((verdict) => ( |
| 208 | <SelectItem key={verdict.value} value={verdict.value}> |
| 209 | <span className="flex items-center gap-1.5"> |
| 210 | {verdict.icon} |
| 211 | {resolve(verdict.label)} |
| 212 | </span> |
| 213 | </SelectItem> |
| 214 | ))} |
| 215 | </SelectPopup> |
| 216 | </Select> |
| 217 | ) : null} |
| 218 | <Button |
| 219 | size="xs" |
| 220 | disabled={ |
| 221 | speech.preparing || |
| 222 | pending || |
| 223 | selectedVerdict === undefined || |
| 224 | (!canSubmit(selectedVerdict.value, body, comments.length) && !speech.blocksSubmission) |
| 225 | } |
| 226 | onClick={() => { |
| 227 | if (selectedVerdict !== undefined) |
| 228 | speech.submitAfterDictation((text) => void submit(selectedVerdict, text)); |
| 229 | }} |
| 230 | > |
| 231 | {pending ? gt("Submitting...") : gt("Submit review")} |
| 232 | </Button> |
| 233 | </div> |
| 234 | </> |
| 235 | ); |
| 236 | } |