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";
6import { textDirection } from "~/textDirection";
7
8import { isCommentSubmitShortcut } from "./commentSubmitShortcut";
9
10interface DiffCommentSecondaryAction {
11 readonly label: string;
12 readonly icon?: ReactNode;
13 readonly allowEmpty?: boolean;
14 readonly onAction: (text: string) => void;
15}
16
17interface DiffCommentAnnotationProps {
18 kind: "draft" | "comment";
19 rangeLabel: string;
20 text: string;
21 onTextChange?: (text: string) => void;
22 onCancel: () => void;
23 onComment: (text: string) => void;
24 onDelete?: () => void;
25 placeholder?: string;
26 submitLabel?: string;
27 pending?: boolean;
28 secondaryAction?: DiffCommentSecondaryAction;
29 focusOnMount?: boolean;
30}
31
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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- focuses the draft textarea on the next animation frame after mount
52 useLayoutEffect(() => {
53 if (kind !== "draft" || !focusOnMount) return;
54 const frame = window.requestAnimationFrame(() => {
55 textareaRef.current?.focus({ preventScroll: true });
56 });
57 return () => window.cancelAnimationFrame(frame);
58 }, [focusOnMount, kind]);
59
60 if (kind === "comment") {
61 return (
62 <div
63 data-diff-comment-annotation
64 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"
65 contentEditable={false}
66 onPointerDown={(event) => event.stopPropagation()}
67 >
68 <MessageCircle className="size-3.5 h-5 shrink-0 text-primary/70" aria-hidden="true" />
69 <p
70 className="min-w-0 flex-1 whitespace-pre-wrap text-sm leading-5"
71 dir={textDirection(displayedText)}
72 >
73 {displayedText}
74 </p>
75 {onDelete ? (
76 <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">
77 <Button
78 variant="ghost-muted"
79 size="icon-xs"
80 aria-label="Delete comment"
81 onClick={onDelete}
82 >
83 <Trash2 className="size-3" />
84 </Button>
85 </span>
86 ) : null}
87 </div>
88 );
89 }
90
91 // Pierre hides native carets and selection; textarea comments need their own visible feedback.
92 return (
93 <div
94 data-diff-comment-annotation
95 className="px-3 py-2 font-sans text-foreground caret-foreground selection:bg-primary selection:text-primary-foreground"
96 contentEditable={false}
97 onPointerDown={(event) => event.stopPropagation()}
98 >
99 <Textarea
100 ref={textareaRef}
101 autoFocus={focusOnMount}
102 size="sm"
103 value={displayedText}
104 dir={textDirection(displayedText)}
105 placeholder={placeholder}
106 aria-label={`Comment on lines ${rangeLabel}`}
107 onChange={(event) => (onTextChange ?? setLocalDraftText)(event.target.value)}
108 onFocus={(event) => {
109 const end = event.currentTarget.value.length;
110 event.currentTarget.setSelectionRange(end, end);
111 }}
112 onKeyDown={(event) => {
113 if (event.key === "Escape") {
114 event.preventDefault();
115 onCancel();
116 }
117 if (isCommentSubmitShortcut(event, trimmedText, pending)) {
118 event.preventDefault();
119 onComment(trimmedText);
120 }
121 }}
122 />
123 <div className="mt-1.5 flex items-center gap-1">
124 <span className="mr-auto text-3xs text-muted-foreground/70">⌘/Ctrl Enter to send</span>
125 <Button variant="ghost-muted" size="xs" onClick={onCancel}>
126 Cancel
127 </Button>
128 {secondaryAction ? (
129 <Button
130 size="xs"
131 variant="outline"
132 disabled={!secondaryAction.allowEmpty && !trimmedText}
133 onClick={() => secondaryAction.onAction(trimmedText)}
134 >
135 {secondaryAction.icon}
136 {secondaryAction.label}
137 </Button>
138 ) : null}
139 <Button size="xs" disabled={pending || !trimmedText} onClick={() => onComment(trimmedText)}>
140 {submitLabel}
141 </Button>
142 </div>
143 </div>
144 );
145}