Open History to see the last commit
1import { MessageCircle, Trash2 } from "lucide-react";
2import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
3
4import { Button } from "~/components/ui/button";
5import { Textarea } from "~/components/ui/textarea";
6
7import { isCommentSubmitShortcut } from "./commentSubmitShortcut";
8
9interface DiffCommentSecondaryAction {
10 readonly label: string;
11 readonly icon?: ReactNode;
12 readonly allowEmpty?: boolean;
13 readonly onAction: (text: string) => void;
14}
15
16interface DiffCommentAnnotationProps {
17 kind: "draft" | "comment";
18 rangeLabel: string;
19 text: string;
20 onTextChange?: (text: string) => void;
21 onCancel: () => void;
22 onComment: (text: string) => void;
23 onDelete?: () => void;
24 placeholder?: string;
25 submitLabel?: string;
26 pending?: boolean;
27 secondaryAction?: DiffCommentSecondaryAction;
28 focusOnMount?: boolean;
29}
30
31/** The shared inline comment treatment for file previews, thread diffs, and pull-request diffs. */
32export function DiffCommentAnnotation({
33 kind,
34 rangeLabel,
35 text,
36 onTextChange,
37 onCancel,
38 onComment,
39 onDelete,
40 placeholder = "Add a comment…",
41 submitLabel = "Comment",
42 pending = false,
43 secondaryAction,
44 focusOnMount = true,
45}: DiffCommentAnnotationProps) {
46 const [localDraftText, setLocalDraftText] = useState("");
47 const displayedText = kind === "draft" && !onTextChange ? localDraftText : text;
48 const trimmedText = displayedText.trim();
49 const textareaRef = useRef<HTMLTextAreaElement | null>(null);
50
51 useLayoutEffect(() => {
52 if (kind !== "draft" || !focusOnMount) return;
53 const frame = window.requestAnimationFrame(() => {
54 textareaRef.current?.focus({ preventScroll: true });
55 });
56 return () => window.cancelAnimationFrame(frame);
57 }, [focusOnMount, kind]);
58
59 if (kind === "comment") {
60 return (
61 <div
62 data-diff-comment-annotation
63 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"
64 contentEditable={false}
65 onPointerDown={(event) => event.stopPropagation()}
66 >
67 <MessageCircle className="mt-0.5 size-3.5 shrink-0 text-primary/70" aria-hidden="true" />
68 <p className="min-w-0 flex-1 whitespace-pre-wrap text-[13px] leading-5">{displayedText}</p>
69 {onDelete ? (
70 <Button
71 className="-my-1 -mr-1 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover/comment:opacity-100 focus-visible:opacity-100 max-sm:opacity-100"
72 variant="ghost"
73 size="icon-xs"
74 aria-label="Delete comment"
75 onClick={onDelete}
76 >
77 <Trash2 className="size-3" />
78 </Button>
79 ) : null}
80 </div>
81 );
82 }
83
84 return (
85 <div
86 data-diff-comment-annotation
87 className="px-3 py-2 font-sans text-foreground"
88 contentEditable={false}
89 onPointerDown={(event) => event.stopPropagation()}
90 >
91 <Textarea
92 ref={textareaRef}
93 autoFocus={focusOnMount}
94 unstyled
95 className="relative inline-flex w-full rounded-md border border-border/50 bg-background/20 font-sans text-foreground transition-colors focus-within:border-border/70 [&_[data-slot=textarea]]:min-h-12 [&_[data-slot=textarea]]:cursor-text [&_[data-slot=textarea]]:caret-foreground [&_[data-slot=textarea]]:px-2.5 [&_[data-slot=textarea]]:py-1.5 [&_[data-slot=textarea]]:font-sans [&_[data-slot=textarea]]:text-xs [&_[data-slot=textarea]]:leading-5 max-sm:[&_[data-slot=textarea]]:min-h-12"
96 size="sm"
97 value={displayedText}
98 placeholder={placeholder}
99 aria-label={`Comment on lines ${rangeLabel}`}
100 onChange={(event) => (onTextChange ?? setLocalDraftText)(event.target.value)}
101 onFocus={(event) => {
102 const end = event.currentTarget.value.length;
103 event.currentTarget.setSelectionRange(end, end);
104 }}
105 onKeyDown={(event) => {
106 if (event.key === "Escape") {
107 event.preventDefault();
108 onCancel();
109 }
110 if (isCommentSubmitShortcut(event, trimmedText, pending)) {
111 event.preventDefault();
112 onComment(trimmedText);
113 }
114 }}
115 />
116 <div className="mt-1.5 flex items-center gap-1">
117 <span className="mr-auto text-[10px] text-muted-foreground/70">⌘/Ctrl Enter to send</span>
118 <Button
119 className="text-muted-foreground hover:text-foreground"
120 variant="ghost"
121 size="xs"
122 onClick={onCancel}
123 >
124 Cancel
125 </Button>
126 {secondaryAction ? (
127 <Button
128 size="xs"
129 variant="outline"
130 disabled={!secondaryAction.allowEmpty && !trimmedText}
131 onClick={() => secondaryAction.onAction(trimmedText)}
132 >
133 {secondaryAction.icon}
134 {secondaryAction.label}
135 </Button>
136 ) : null}
137 <Button size="xs" disabled={pending || !trimmedText} onClick={() => onComment(trimmedText)}>
138 {submitLabel}
139 </Button>
140 </div>
141 </div>
142 );
143}