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

LocalCommentAnnotation.tsx

103 lines3.6 KB

1import { MessageCircle, Trash2 } from "lucide-react";
2import { useState } from "react";
3​
4import { Button } from "~/components/ui/button";
5import { Textarea } from "~/components/ui/textarea";
6​
7interface LocalCommentAnnotationProps {
8 kind: "draft" | "comment";
9 rangeLabel: string;
10 text: string;
11 onTextChange?: (text: string) => void;
12 onCancel: () => void;
13 onComment: (text: string) => void;
14 onDelete: () => void;
16​
17export function LocalCommentAnnotation({
18 kind,
19 rangeLabel,
20 text,
21 onTextChange,
22 onCancel,
23 onComment,
24 onDelete,
25}: LocalCommentAnnotationProps) {
26 const [localDraftText, setLocalDraftText] = useState("");
27 const displayedText = kind === "draft" && !onTextChange ? localDraftText : text;
28​
29 if (kind === "comment") {
30 return (
31 <div
32 data-file-comment-annotation
33 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"
34 contentEditable={false}
35 onPointerDown={(event) => event.stopPropagation()}
36 >
37 <MessageCircle className="mt-0.5 size-3.5 shrink-0 text-primary/70" aria-hidden="true" />
38 <p className="min-w-0 flex-1 whitespace-pre-wrap text-[13px] leading-5">{displayedText}</p>
39 <Button
40 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"
41 variant="ghost"
42 size="icon-xs"
43 aria-label="Delete comment"
44 onClick={onDelete}
45 >
46 <Trash2 className="size-3" />
47 </Button>
48 </div>
49 );
50 }
51​
52 return (
53 <div
54 data-file-comment-annotation
55 className="px-3 py-2 font-sans text-foreground"
56 contentEditable={false}
57 onPointerDown={(event) => event.stopPropagation()}
58 >
59 <Textarea
60 autoFocus
61 unstyled
62 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"
63 size="sm"
64 value={displayedText}
65 placeholder="Add a comment…"
66 aria-label={`Comment on lines ${rangeLabel}`}
67 onChange={(event) => (onTextChange ?? setLocalDraftText)(event.target.value)}
68 onFocus={(event) => {
69 const end = event.currentTarget.value.length;
70 event.currentTarget.setSelectionRange(end, end);
71 }}
72 onKeyDown={(event) => {
73 if (event.key === "Escape") {
74 event.preventDefault();
75 onCancel();
76 }
77 if ((event.metaKey || event.ctrlKey) && event.key === "Enter" && displayedText.trim()) {
78 event.preventDefault();
79 onComment(displayedText.trim());
80 }
81 }}
82 />
83 <div className="mt-1.5 flex items-center gap-1">
84 <span className="mr-auto text-[10px] text-muted-foreground/70">⌘/Ctrl Enter to send</span>
85 <Button
86 className="text-muted-foreground hover:text-foreground"
87 variant="ghost"
88 size="xs"
89 onClick={onCancel}
90 >
91 Cancel
92 </Button>
93 <Button
94 size="xs"
95 disabled={!displayedText.trim()}
96 onClick={() => onComment(displayedText.trim())}
97 >
98 Comment
99 </Button>
100 </div>
101 </div>
102 );