Open History to see the last commit
1import { MessageCircle, Trash2 } from "lucide-react";
2import { 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}
29
30/** The shared inline comment treatment for file previews, thread diffs, and pull-request diffs. */
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}: DiffCommentAnnotationProps) {
44 const [localDraftText, setLocalDraftText] = useState("");
45 const displayedText = kind === "draft" && !onTextChange ? localDraftText : text;
46 const trimmedText = displayedText.trim();
47
48 if (kind === "comment") {
49 return (
50 <div
51 data-diff-comment-annotation
52 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"
53 contentEditable={false}
54 onPointerDown={(event) => event.stopPropagation()}
55 >
56 <MessageCircle className="mt-0.5 size-3.5 shrink-0 text-primary/70" aria-hidden="true" />
57 <p className="min-w-0 flex-1 whitespace-pre-wrap text-[13px] leading-5">{displayedText}</p>
58 {onDelete ? (
59 <Button
60 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"
61 variant="ghost"
62 size="icon-xs"
63 aria-label="Delete comment"
64 onClick={onDelete}
65 >
66 <Trash2 className="size-3" />
67 </Button>
68 ) : null}
69 </div>
70 );
71 }
72
73 return (
74 <div
75 data-diff-comment-annotation
76 className="px-3 py-2 font-sans text-foreground"
77 contentEditable={false}
78 onPointerDown={(event) => event.stopPropagation()}
79 >
80 <Textarea
81 autoFocus
82 unstyled
83 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]]: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"
84 size="sm"
85 value={displayedText}
86 placeholder={placeholder}
87 aria-label={`Comment on lines ${rangeLabel}`}
88 onChange={(event) => (onTextChange ?? setLocalDraftText)(event.target.value)}
89 onFocus={(event) => {
90 const end = event.currentTarget.value.length;
91 event.currentTarget.setSelectionRange(end, end);
92 }}
93 onKeyDown={(event) => {
94 if (event.key === "Escape") {
95 event.preventDefault();
96 onCancel();
97 }
98 if (isCommentSubmitShortcut(event, trimmedText, pending)) {
99 event.preventDefault();
100 onComment(trimmedText);
101 }
102 }}
103 />
104 <div className="mt-1.5 flex items-center gap-1">
105 <span className="mr-auto text-[10px] text-muted-foreground/70">⌘/Ctrl Enter to send</span>
106 <Button
107 className="text-muted-foreground hover:text-foreground"
108 variant="ghost"
109 size="xs"
110 onClick={onCancel}
111 >
112 Cancel
113 </Button>
114 {secondaryAction ? (
115 <Button
116 size="xs"
117 variant="outline"
118 disabled={!secondaryAction.allowEmpty && !trimmedText}
119 onClick={() => secondaryAction.onAction(trimmedText)}
120 >
121 {secondaryAction.icon}
122 {secondaryAction.label}
123 </Button>
124 ) : null}
125 <Button size="xs" disabled={pending || !trimmedText} onClick={() => onComment(trimmedText)}>
126 {submitLabel}
127 </Button>
128 </div>
129 </div>
130 );
131}