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

DiffCommentAnnotation.tsx

136 lines4.3 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 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"
63 contentEditable={false}
64 onPointerDown={(event) => event.stopPropagation()}
65 >
66 <MessageCircle className="mt-0.5 size-3.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 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 size="sm"
95 value={displayedText}
96 placeholder={placeholder}
97 aria-label={`Comment on lines ${rangeLabel}`}
98 onChange={(event) => (onTextChange ?? setLocalDraftText)(event.target.value)}
99 onFocus={(event) => {
100 const end = event.currentTarget.value.length;
101 event.currentTarget.setSelectionRange(end, end);
102 }}
103 onKeyDown={(event) => {
104 if (event.key === "Escape") {
105 event.preventDefault();
106 onCancel();
107 }
108 if (isCommentSubmitShortcut(event, trimmedText, pending)) {
109 event.preventDefault();
110 onComment(trimmedText);
111 }
112 }}
113 />
114 <div className="mt-1.5 flex items-center gap-1">
115 <span className="mr-auto text-3xs text-muted-foreground/70">⌘/Ctrl Enter to send</span>
116 <Button variant="ghost-muted" size="xs" onClick={onCancel}>
117 Cancel
118 </Button>
119 {secondaryAction ? (
120 <Button
121 size="xs"
122 variant="outline"
123 disabled={!secondaryAction.allowEmpty && !trimmedText}
124 onClick={() => secondaryAction.onAction(trimmedText)}
125 >
126 {secondaryAction.icon}
127 {secondaryAction.label}
128 </Button>
129 ) : null}
130 <Button size="xs" disabled={pending || !trimmedText} onClick={() => onComment(trimmedText)}>
131 {submitLabel}
132 </Button>
133 </div>
134 </div>
135 );