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