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
31export function DiffCommentAnnotation({
32 kind,
33 rangeLabel,
34 text,
35 onTextChange,
36 onCancel,
37 onComment,
38 onDelete,
39 placeholder = "Add a comment…",
40 submitLabel = "Comment",
41 pending = false,
42 secondaryAction,
43 focusOnMount = true,
44}: DiffCommentAnnotationProps) {
45 const [localDraftText, setLocalDraftText] = useState("");
46 const displayedText = kind === "draft" && !onTextChange ? localDraftText : text;
47 const trimmedText = displayedText.trim();
48 const textareaRef = useRef<HTMLTextAreaElement | null>(null);
49
50 useLayoutEffect(() => {
51 if (kind !== "draft" || !focusOnMount) return;
52 const frame = window.requestAnimationFrame(() => {
53 textareaRef.current?.focus({ preventScroll: true });
54 });
55 return () => window.cancelAnimationFrame(frame);
56 }, [focusOnMount, kind]);
57
58 if (kind === "comment") {
59 return (
60 <div
61 data-diff-comment-annotation
62 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"
63 contentEditable={false}
64 onPointerDown={(event) => event.stopPropagation()}
65 >
66 <MessageCircle className="size-3.5 h-5 shrink-0 text-primary/70" aria-hidden="true" />
67 <p className="min-w-0 flex-1 whitespace-pre-wrap text-sm leading-5">{displayedText}</p>
68 {onDelete ? (
69 <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">
70 <Button
71 variant="ghost-muted"
72 size="icon-xs"
73 aria-label="Delete comment"
74 onClick={onDelete}
75 >
76 <Trash2 className="size-3" />
77 </Button>
78 </span>
79 ) : null}
80 </div>
81 );
82 }
83
84 // Editable file annotations inherit Pierre's transparent native caret and selection.
85 // Comments use a native textarea, so they need their own visible text feedback.
86 return (
87 <div
88 data-diff-comment-annotation
89 className="px-3 py-2 font-sans text-foreground caret-foreground selection:bg-primary selection:text-primary-foreground"
90 contentEditable={false}
91 onPointerDown={(event) => event.stopPropagation()}
92 >
93 <Textarea
94 ref={textareaRef}
95 autoFocus={focusOnMount}
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-3xs text-muted-foreground/70">⌘/Ctrl Enter to send</span>
118 <Button variant="ghost-muted" size="xs" onClick={onCancel}>
119 Cancel
120 </Button>
121 {secondaryAction ? (
122 <Button
123 size="xs"
124 variant="outline"
125 disabled={!secondaryAction.allowEmpty && !trimmedText}
126 onClick={() => secondaryAction.onAction(trimmedText)}
127 >
128 {secondaryAction.icon}
129 {secondaryAction.label}
130 </Button>
131 ) : null}
132 <Button size="xs" disabled={pending || !trimmedText} onClick={() => onComment(trimmedText)}>
133 {submitLabel}
134 </Button>
135 </div>
136 </div>
137 );
138}