Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DiffCommentAnnotation.tsx

204 lines6.8 KB

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