The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 2 | import type { EnvironmentId, IssueRef } from "@t3tools/contracts"; |
| 3 | import { msg, useGT } from "gt-react"; |
| 4 | import { ArrowUpIcon } from "lucide-react"; |
| 5 | import { useRef, useState } from "react"; |
| 6 | |
| 7 | import { m } from "~/i18n/messages"; |
| 8 | import { TextFieldSpeechControls, useTextFieldSpeech } from "~/speech/useTextFieldSpeech"; |
| 9 | import { issueEnvironment } from "~/state/issues"; |
| 10 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 11 | |
| 12 | import { ComposerSurface } from "../chat/ComposerSurface"; |
| 13 | import { readableFailure } from "../pullRequest/pullRequestDetail.logic"; |
| 14 | import { Spinner } from "../ui/spinner"; |
| 15 | import { toastManager } from "../ui/toast"; |
| 16 | |
| 17 | /** The thread composer's card, floating over the issue's timeline, with a comment as its message. */ |
| 18 | export function IssueCommentComposer({ |
| 19 | environmentId, |
| 20 | reference, |
| 21 | blocked, |
| 22 | }: { |
| 23 | environmentId: EnvironmentId; |
| 24 | reference: IssueRef; |
| 25 | /** Why the viewer cannot comment here; null when they can. */ |
| 26 | blocked: string | null; |
| 27 | }) { |
| 28 | const gt = useGT(); |
| 29 | const [body, setBody] = useState(""); |
| 30 | const [submitting, setSubmitting] = useState(false); |
| 31 | const textareaRef = useRef<HTMLTextAreaElement>(null); |
| 32 | const comment = useAtomCommand(issueEnvironment.comment, { reportFailure: false }); |
| 33 | const locked = blocked !== null || submitting; |
| 34 | const speech = useTextFieldSpeech({ |
| 35 | environmentId, |
| 36 | projectId: reference.projectId, |
| 37 | ownerKey: JSON.stringify(["issue-comment", environmentId, reference]), |
| 38 | text: body, |
| 39 | textareaRef, |
| 40 | onTextChange: setBody, |
| 41 | disabled: locked, |
| 42 | }); |
| 43 | |
| 44 | const submit = async (text: string) => { |
| 45 | if (text.trim().length === 0 || locked) return; |
| 46 | setSubmitting(true); |
| 47 | const result = await comment({ environmentId, input: { ...reference, body: text } }); |
| 48 | setSubmitting(false); |
| 49 | if (result._tag === "Failure") { |
| 50 | toastManager.add({ |
| 51 | type: "error", |
| 52 | title: m(msg("The comment could not be posted")), |
| 53 | description: readableFailure( |
| 54 | squashAtomCommandFailure(result), |
| 55 | m(msg("The tracker refused it.")), |
| 56 | ), |
| 57 | }); |
| 58 | return; |
| 59 | } |
| 60 | setBody(""); |
| 61 | }; |
| 62 | const submitAfterDictation = () => speech.submitAfterDictation((text) => void submit(text)); |
| 63 | const submitDisabled = |
| 64 | speech.preparing || (body.trim().length === 0 && !speech.blocksSubmission) || locked; |
| 65 | |
| 66 | return ( |
| 67 | <div className="pointer-events-none absolute inset-x-0 bottom-0 z-10 px-3 pb-3"> |
| 68 | <ComposerSurface.Shell className="pointer-events-auto"> |
| 69 | <ComposerSurface.Host> |
| 70 | <form |
| 71 | className="relative z-10 flex flex-col" |
| 72 | onSubmit={(event) => { |
| 73 | event.preventDefault(); |
| 74 | submitAfterDictation(); |
| 75 | }} |
| 76 | > |
| 77 | <textarea |
| 78 | ref={textareaRef} |
| 79 | rows={2} |
| 80 | className="field-sizing-content max-h-64 min-h-[2lh] w-full resize-none bg-transparent px-3.5 pt-3 pb-1 text-base text-foreground outline-none wrap-anywhere placeholder:text-muted-foreground/70 disabled:opacity-64 sm:text-sm" |
| 81 | aria-label={gt("Add a comment")} |
| 82 | placeholder={blocked ?? gt("Leave a comment")} |
| 83 | value={body} |
| 84 | // Lock while posting because success clears the draft. |
| 85 | disabled={locked} |
| 86 | readOnly={speech.freezesEditor} |
| 87 | onChange={(event) => setBody(event.target.value)} |
| 88 | onKeyDown={(event) => { |
| 89 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 90 | if (event.key === "Enter" && (event.metaKey || event.ctrlKey) && !event.shiftKey) { |
| 91 | event.preventDefault(); |
| 92 | if (!event.repeat) submitAfterDictation(); |
| 93 | } |
| 94 | }} |
| 95 | /> |
| 96 | <div className="flex flex-wrap items-center justify-end gap-2 px-2.5 pb-2.5"> |
| 97 | <TextFieldSpeechControls speech={speech} disabled={locked} /> |
| 98 | <button |
| 99 | type="submit" |
| 100 | className="flex size-8 items-center justify-center rounded-full bg-message-action text-message-action-foreground shadow-xs transition-all duration-150 enabled:cursor-pointer enabled:shadow-message-action/24 enabled:inset-shadow-control-highlight hover:scale-105 hover:bg-message-action-hover active:shadow-none active:inset-shadow-control-pressed disabled:pointer-events-none disabled:bg-foreground/12 disabled:text-muted-foreground disabled:shadow-none" |
| 101 | disabled={submitDisabled} |
| 102 | aria-label={ |
| 103 | submitting |
| 104 | ? gt("Posting comment") |
| 105 | : gt("Comment", { $context: "verb, posts the comment" }) |
| 106 | } |
| 107 | > |
| 108 | {submitting ? ( |
| 109 | <Spinner size="sm" aria-hidden /> |
| 110 | ) : ( |
| 111 | <ArrowUpIcon aria-hidden className="size-4" /> |
| 112 | )} |
| 113 | </button> |
| 114 | </div> |
| 115 | </form> |
| 116 | </ComposerSurface.Host> |
| 117 | </ComposerSurface.Shell> |
| 118 | </div> |
| 119 | ); |
| 120 | } |