The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 3 | import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts"; |
| 4 | import { SendIcon } from "lucide-react"; |
| 5 | import { msg, T, useGT } from "gt-react"; |
| 6 | import { useEffect, useState, type RefObject } from "react"; |
| 7 | |
| 8 | import { m } from "~/i18n/messages"; |
| 9 | |
| 10 | import { TextFieldSpeechControls, useTextFieldSpeech } from "~/speech/useTextFieldSpeech"; |
| 11 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 12 | |
| 13 | import { Button } from "../ui/button"; |
| 14 | import { toastManager } from "../ui/toast"; |
| 15 | import { PullRequestGlyph } from "./pullRequestIcons"; |
| 16 | import { |
| 17 | PullRequestMarkdownTextarea, |
| 18 | pullRequestCompletionScope, |
| 19 | } from "./PullRequestMarkdownTextarea"; |
| 20 | import { pullRequestReviewKey } from "./pullRequestReviewStore"; |
| 21 | import { useInlineActionConfirm } from "./useInlineActionConfirm"; |
| 22 | |
| 23 | import { changeRequestTermKind } from "./changeRequestTerm"; |
| 24 | |
| 25 | export function PullRequestCommentForm({ |
| 26 | environmentId, |
| 27 | active, |
| 28 | reference, |
| 29 | detail, |
| 30 | actionPending, |
| 31 | textareaRef, |
| 32 | onCommentAction, |
| 33 | onCommented, |
| 34 | onClose, |
| 35 | }: { |
| 36 | environmentId: EnvironmentId; |
| 37 | /** False while the form stays mounted but hidden; dictation is cancelled then. */ |
| 38 | active: boolean; |
| 39 | reference: PullRequestRef; |
| 40 | detail: PullRequestDetailView; |
| 41 | actionPending: boolean; |
| 42 | textareaRef: RefObject<HTMLTextAreaElement | null>; |
| 43 | onCommentAction: ( |
| 44 | body: string, |
| 45 | action: "close" | "reopen", |
| 46 | ) => Promise<{ readonly commentPosted: boolean }>; |
| 47 | onCommented: () => void; |
| 48 | onClose: () => void; |
| 49 | }) { |
| 50 | const gt = useGT(); |
| 51 | const [body, setBody] = useState(""); |
| 52 | const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null); |
| 53 | const locked = submitting !== null || actionPending; |
| 54 | const speech = useTextFieldSpeech({ |
| 55 | environmentId, |
| 56 | projectId: reference.projectId, |
| 57 | ownerKey: JSON.stringify(["pr-comment", environmentId, reference]), |
| 58 | text: body, |
| 59 | textareaRef, |
| 60 | onTextChange: setBody, |
| 61 | disabled: !active || locked, |
| 62 | }); |
| 63 | const { cancel } = speech; |
| 64 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- discards a recording when the hidden form can no longer be submitted |
| 65 | useEffect(() => { |
| 66 | if (!active) cancel(); |
| 67 | }, [active, cancel]); |
| 68 | const postComment = useAtomCommand(pullRequestEnvironment.comment, { |
| 69 | reportFailure: false, |
| 70 | }); |
| 71 | const inlineConfirm = useInlineActionConfirm(pullRequestReviewKey(reference)); |
| 72 | const followUpAction = |
| 73 | detail.state === "open" && |
| 74 | detail.capabilities.actions.includes("close") && |
| 75 | detail.viewerPermissions.actions.includes("close") |
| 76 | ? ("close" as const) |
| 77 | : detail.state === "closed" && |
| 78 | detail.capabilities.actions.includes("reopen") && |
| 79 | detail.viewerPermissions.actions.includes("reopen") |
| 80 | ? ("reopen" as const) |
| 81 | : null; |
| 82 | |
| 83 | const submit = async (action: "comment" | "close" | "reopen", text: string) => { |
| 84 | const trimmed = text.trim(); |
| 85 | if (trimmed.length === 0 || submitting !== null || actionPending) return; |
| 86 | setSubmitting(action); |
| 87 | if (action !== "comment") { |
| 88 | const result = await onCommentAction(trimmed, action); |
| 89 | if (result.commentPosted) { |
| 90 | setBody(""); |
| 91 | onClose(); |
| 92 | } |
| 93 | setSubmitting(null); |
| 94 | return; |
| 95 | } |
| 96 | const result = await postComment({ |
| 97 | environmentId, |
| 98 | input: { |
| 99 | ...reference, |
| 100 | body: trimmed, |
| 101 | }, |
| 102 | }); |
| 103 | if (result._tag === "Failure") { |
| 104 | setSubmitting(null); |
| 105 | toastManager.add({ type: "error", title: m(msg("Could not post the comment")) }); |
| 106 | return; |
| 107 | } |
| 108 | setBody(""); |
| 109 | setSubmitting(null); |
| 110 | onClose(); |
| 111 | onCommented(); |
| 112 | }; |
| 113 | const submitAfterDictation = (action: "comment" | "close" | "reopen") => |
| 114 | speech.submitAfterDictation((text) => void submit(action, text)); |
| 115 | const submitDisabled = |
| 116 | speech.preparing || (body.trim().length === 0 && !speech.blocksSubmission) || locked; |
| 117 | |
| 118 | return ( |
| 119 | <div className="space-y-2"> |
| 120 | <PullRequestMarkdownTextarea |
| 121 | ref={textareaRef} |
| 122 | completionScope={pullRequestCompletionScope(detail, environmentId, reference)} |
| 123 | // Lock while posting because success clears the draft. |
| 124 | disabled={locked} |
| 125 | readOnly={speech.freezesEditor} |
| 126 | value={body} |
| 127 | rows={3} |
| 128 | placeholder={gt("Leave a comment")} |
| 129 | aria-label={gt( |
| 130 | "Comment on this {term, select, mr {merge request} cr {change request} other {pull request}}", |
| 131 | { |
| 132 | term: changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest), |
| 133 | }, |
| 134 | )} |
| 135 | onValueChange={setBody} |
| 136 | onKeyDown={(event) => { |
| 137 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 138 | if ( |
| 139 | event.key === "Enter" && |
| 140 | (event.metaKey || event.ctrlKey) && |
| 141 | !event.shiftKey && |
| 142 | !event.altKey |
| 143 | ) { |
| 144 | event.preventDefault(); |
| 145 | event.stopPropagation(); |
| 146 | if (!event.repeat) submitAfterDictation("comment"); |
| 147 | } |
| 148 | }} |
| 149 | /> |
| 150 | <div className="flex flex-wrap items-center justify-end gap-2"> |
| 151 | <TextFieldSpeechControls speech={speech} disabled={locked} /> |
| 152 | {followUpAction === null ? null : ( |
| 153 | <Button |
| 154 | size="xs" |
| 155 | variant={followUpAction === "close" ? "destructive-outline" : "outline"} |
| 156 | disabled={submitDisabled} |
| 157 | {...(followUpAction === "close" |
| 158 | ? inlineConfirm.props("close", () => submitAfterDictation("close")) |
| 159 | : { onClick: () => submitAfterDictation("reopen") })} |
| 160 | > |
| 161 | {followUpAction === "close" ? ( |
| 162 | <PullRequestGlyph.closed className="size-3.5" /> |
| 163 | ) : ( |
| 164 | <PullRequestGlyph.reopen className="size-3.5" /> |
| 165 | )} |
| 166 | {followUpAction === "close" ? ( |
| 167 | // The longest label holds the width, so the confirming click lands on the button. |
| 168 | <span className="inline-grid"> |
| 169 | <span aria-hidden className="invisible col-start-1 row-start-1"> |
| 170 | <T>Close with comment</T> |
| 171 | </span> |
| 172 | <span className="col-start-1 row-start-1"> |
| 173 | {submitting === "close" |
| 174 | ? gt("Closing...") |
| 175 | : inlineConfirm.isArmed("close") |
| 176 | ? gt("Confirm close?") |
| 177 | : gt("Close with comment")} |
| 178 | </span> |
| 179 | </span> |
| 180 | ) : submitting === "reopen" ? ( |
| 181 | gt("Reopening...") |
| 182 | ) : ( |
| 183 | gt("Reopen with comment") |
| 184 | )} |
| 185 | </Button> |
| 186 | )} |
| 187 | <Button |
| 188 | size="xs" |
| 189 | variant="outline" |
| 190 | disabled={submitDisabled} |
| 191 | onClick={() => submitAfterDictation("comment")} |
| 192 | > |
| 193 | <SendIcon className="size-3.5" /> |
| 194 | {submitting === "comment" |
| 195 | ? gt("Posting...") |
| 196 | : gt("Comment", { $context: "verb, submit button" })} |
| 197 | </Button> |
| 198 | </div> |
| 199 | </div> |
| 200 | ); |
| 201 | } |