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 type { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | import { T, useGT, useMessages } from "gt-react"; |
| 3 | import { reviewCommentRangeText } from "~/lib/composerContextRecords"; |
| 4 | import { MessageCircle, Trash2 } from "lucide-react"; |
| 5 | import { useLayoutEffect, useRef, useState, type ReactNode } from "react"; |
| 6 | |
| 7 | import { Button } from "~/components/ui/button"; |
| 8 | import { |
| 9 | PullRequestMarkdownTextarea, |
| 10 | type PullRequestCompletionScope, |
| 11 | } from "~/components/pullRequest/PullRequestMarkdownTextarea"; |
| 12 | import { |
| 13 | SpeechTextField, |
| 14 | TextFieldSpeechControls, |
| 15 | type TextFieldSpeech, |
| 16 | } from "~/speech/useTextFieldSpeech"; |
| 17 | import { textDirection } from "~/textDirection"; |
| 18 | |
| 19 | import { isCommentSubmitShortcut } from "./commentSubmitShortcut"; |
| 20 | |
| 21 | interface DiffCommentSecondaryAction { |
| 22 | readonly label: string; |
| 23 | readonly icon?: ReactNode; |
| 24 | readonly allowEmpty?: boolean; |
| 25 | readonly onAction: (text: string) => void; |
| 26 | } |
| 27 | |
| 28 | interface DiffCommentAnnotationProps { |
| 29 | /** With an environment, the draft can be dictated; its project supplies vocabulary. */ |
| 30 | environmentId?: EnvironmentId | undefined; |
| 31 | projectId?: ProjectId | null | undefined; |
| 32 | ownerKey?: string; |
| 33 | kind: "draft" | "comment"; |
| 34 | rangeLabel: string; |
| 35 | text: string; |
| 36 | onTextChange?: (text: string) => void; |
| 37 | onCancel: () => void; |
| 38 | onComment: (text: string) => void; |
| 39 | onDelete?: () => void; |
| 40 | placeholder?: string; |
| 41 | submitLabel?: string; |
| 42 | pending?: boolean; |
| 43 | secondaryAction?: DiffCommentSecondaryAction; |
| 44 | focusOnMount?: boolean; |
| 45 | /** Set only for pull request comments, where `@` completes the repository's users. */ |
| 46 | completionScope?: PullRequestCompletionScope | null; |
| 47 | } |
| 48 | |
| 49 | export function DiffCommentAnnotation({ |
| 50 | environmentId, |
| 51 | projectId, |
| 52 | ownerKey, |
| 53 | kind, |
| 54 | rangeLabel, |
| 55 | text, |
| 56 | onTextChange, |
| 57 | onCancel, |
| 58 | onComment, |
| 59 | onDelete, |
| 60 | placeholder, |
| 61 | submitLabel, |
| 62 | pending = false, |
| 63 | secondaryAction, |
| 64 | focusOnMount = true, |
| 65 | completionScope = null, |
| 66 | }: DiffCommentAnnotationProps) { |
| 67 | const gt = useGT(); |
| 68 | const messages = useMessages(); |
| 69 | const [localDraftText, setLocalDraftText] = useState(""); |
| 70 | const displayedText = kind === "draft" && !onTextChange ? localDraftText : text; |
| 71 | const trimmedText = displayedText.trim(); |
| 72 | const textareaRef = useRef<HTMLTextAreaElement | null>(null); |
| 73 | |
| 74 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- focuses the draft textarea on the next animation frame after mount |
| 75 | useLayoutEffect(() => { |
| 76 | if (kind !== "draft" || !focusOnMount) return; |
| 77 | const frame = window.requestAnimationFrame(() => { |
| 78 | textareaRef.current?.focus({ preventScroll: true }); |
| 79 | }); |
| 80 | return () => window.cancelAnimationFrame(frame); |
| 81 | }, [focusOnMount, kind]); |
| 82 | |
| 83 | if (kind === "comment") { |
| 84 | return ( |
| 85 | <div |
| 86 | data-diff-comment-annotation |
| 87 | className="group/comment flex min-w-0 items-start gap-2.5 border-s-2 border-primary/55 bg-primary/[0.045] px-3 py-2.5 font-sans text-foreground selection:bg-primary selection:text-primary-foreground" |
| 88 | contentEditable={false} |
| 89 | onPointerDown={(event) => event.stopPropagation()} |
| 90 | > |
| 91 | <MessageCircle className="size-3.5 h-5 shrink-0 text-primary/70" aria-hidden="true" /> |
| 92 | <p |
| 93 | className="min-w-0 flex-1 whitespace-pre-wrap text-sm leading-5" |
| 94 | dir={textDirection(displayedText)} |
| 95 | > |
| 96 | {displayedText} |
| 97 | </p> |
| 98 | {onDelete ? ( |
| 99 | <span className="-my-1 -mr-1 flex shrink-0 opacity-0 transition-opacity group-hover/comment:opacity-100 focus-within:opacity-100 max-sm:opacity-100"> |
| 100 | <Button |
| 101 | variant="ghost-muted" |
| 102 | size="icon-xs" |
| 103 | aria-label={gt("Delete comment")} |
| 104 | onClick={onDelete} |
| 105 | > |
| 106 | <Trash2 className="size-3" /> |
| 107 | </Button> |
| 108 | </span> |
| 109 | ) : null} |
| 110 | </div> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | const submit = ( |
| 115 | speech: TextFieldSpeech | undefined, |
| 116 | action: (text: string) => void, |
| 117 | allowEmpty = false, |
| 118 | ) => { |
| 119 | const finish = (value: string) => { |
| 120 | if (!pending && (allowEmpty || value.trim())) action(value.trim()); |
| 121 | }; |
| 122 | if (speech) speech.submitAfterDictation(finish); |
| 123 | else finish(displayedText); |
| 124 | }; |
| 125 | |
| 126 | // Pierre hides native carets and selection; textarea comments need their own visible feedback. |
| 127 | const renderDraft = (speech?: TextFieldSpeech) => ( |
| 128 | <div |
| 129 | data-diff-comment-annotation |
| 130 | className="px-3 py-2 font-sans text-foreground caret-foreground selection:bg-primary selection:text-primary-foreground" |
| 131 | contentEditable={false} |
| 132 | onPointerDown={(event) => event.stopPropagation()} |
| 133 | > |
| 134 | <PullRequestMarkdownTextarea |
| 135 | ref={textareaRef} |
| 136 | completionScope={completionScope} |
| 137 | readOnly={speech?.freezesEditor} |
| 138 | autoFocus={focusOnMount} |
| 139 | size="sm" |
| 140 | value={displayedText} |
| 141 | dir={textDirection(displayedText)} |
| 142 | placeholder={placeholder ?? gt("Add a comment…")} |
| 143 | aria-label={gt("Comment on lines {rangeLabel}", { |
| 144 | rangeLabel: reviewCommentRangeText(rangeLabel, messages), |
| 145 | })} |
| 146 | onValueChange={onTextChange ?? setLocalDraftText} |
| 147 | onFocus={(event) => { |
| 148 | const end = event.currentTarget.value.length; |
| 149 | event.currentTarget.setSelectionRange(end, end); |
| 150 | }} |
| 151 | onKeyDown={(event) => { |
| 152 | if (event.key === "Escape") { |
| 153 | event.preventDefault(); |
| 154 | if (speech && speech.state.phase !== "idle") speech.cancel(); |
| 155 | else onCancel(); |
| 156 | } |
| 157 | if (isCommentSubmitShortcut(event, trimmedText, pending, speech?.blocksSubmission)) { |
| 158 | event.preventDefault(); |
| 159 | submit(speech, onComment); |
| 160 | } |
| 161 | }} |
| 162 | /> |
| 163 | <div className="mt-1.5 flex flex-wrap items-center gap-1"> |
| 164 | <span className="mr-auto text-3xs text-muted-foreground/70"> |
| 165 | <T>⌘/Ctrl Enter to send</T> |
| 166 | </span> |
| 167 | {speech ? <TextFieldSpeechControls speech={speech} disabled={pending} /> : null} |
| 168 | <Button variant="ghost-muted" size="xs" onClick={onCancel}> |
| 169 | <T>Cancel</T> |
| 170 | </Button> |
| 171 | {secondaryAction ? ( |
| 172 | <Button |
| 173 | size="xs" |
| 174 | variant="outline" |
| 175 | disabled={ |
| 176 | speech?.preparing || |
| 177 | pending || |
| 178 | (!speech?.blocksSubmission && !secondaryAction.allowEmpty && !trimmedText) |
| 179 | } |
| 180 | onClick={() => submit(speech, secondaryAction.onAction, secondaryAction.allowEmpty)} |
| 181 | > |
| 182 | {secondaryAction.icon} |
| 183 | {secondaryAction.label} |
| 184 | </Button> |
| 185 | ) : null} |
| 186 | <Button |
| 187 | size="xs" |
| 188 | disabled={speech?.preparing || pending || (!speech?.blocksSubmission && !trimmedText)} |
| 189 | onClick={() => submit(speech, onComment)} |
| 190 | > |
| 191 | {submitLabel ?? gt("Comment", { $context: "verb, submit button" })} |
| 192 | </Button> |
| 193 | </div> |
| 194 | </div> |
| 195 | ); |
| 196 | |
| 197 | return environmentId ? ( |
| 198 | <SpeechTextField |
| 199 | environmentId={environmentId} |
| 200 | projectId={projectId} |
| 201 | ownerKey={ownerKey ?? rangeLabel} |
| 202 | text={displayedText} |
| 203 | textareaRef={textareaRef} |
| 204 | onTextChange={onTextChange ?? setLocalDraftText} |
| 205 | disabled={pending} |
| 206 | > |
| 207 | {renderDraft} |
| 208 | </SpeechTextField> |
| 209 | ) : ( |
| 210 | renderDraft() |
| 211 | ); |
| 212 | } |