Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DiffCommentAnnotation.tsx

138 lines4.7 KB

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;
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;
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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- focuses the draft textarea on the next animation frame after mount
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 selection:bg-primary selection:text-primary-foreground"
64 contentEditable={false}
65 onPointerDown={(event) => event.stopPropagation()}
66 >
67 <MessageCircle className="size-3.5 h-5 shrink-0 text-primary/70" aria-hidden="true" />
68 <p className="min-w-0 flex-1 whitespace-pre-wrap text-sm leading-5">{displayedText}</p>
69 {onDelete ? (
70 <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">
71 <Button
72 variant="ghost-muted"
73 size="icon-xs"
74 aria-label="Delete comment"
75 onClick={onDelete}
76 >
77 <Trash2 className="size-3" />
78 </Button>
79 </span>
80 ) : null}
81 </div>
82 );
83 }
84​
85 // Pierre hides native carets and selection; textarea comments need their own visible 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 );