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

LocalCommentAnnotation.tsx

90 lines2.8 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 onCancel: () => void;
12 onComment: (text: string) => void;
13 onDelete: () => void;
15​
16export function LocalCommentAnnotation({
17 kind,
18 rangeLabel,
19 text: savedText,
20 onCancel,
21 onComment,
22 onDelete,
23}: LocalCommentAnnotationProps) {
24 const [text, setText] = useState("");
25​
26 if (kind === "comment") {
27 return (
28 <div
29 data-file-comment-annotation
30 className="mx-3 my-2 rounded-xl border border-border/70 bg-background p-3 shadow-sm"
31 contentEditable={false}
32 onPointerDown={(event) => event.stopPropagation()}
33 >
34 <div className="flex items-center gap-2">
35 <MessageCircle className="size-4 text-muted-foreground" />
36 <span className="text-xs font-medium">Local comment</span>
37 <span className="ml-auto text-[11px] text-muted-foreground">{rangeLabel}</span>
38 <Button variant="ghost" size="icon-xs" aria-label="Delete comment" onClick={onDelete}>
39 <Trash2 className="size-3.5" />
40 </Button>
41 </div>
42 <p className="mt-2 whitespace-pre-wrap text-sm leading-relaxed text-foreground">
43 {savedText}
44 </p>
45 </div>
46 );
47 }
48​
49 return (
50 <div
51 data-file-comment-annotation
52 className="mx-3 my-2 rounded-xl border border-border/70 bg-background p-3 shadow-lg"
53 contentEditable={false}
54 onPointerDown={(event) => event.stopPropagation()}
55 >
56 <div className="flex items-center gap-2">
57 <MessageCircle className="size-4 text-muted-foreground" />
58 <span className="text-sm font-medium">Local comment</span>
59 </div>
60 <div className="mt-1 text-xs text-muted-foreground">Comment on lines {rangeLabel}</div>
61 <Textarea
62 autoFocus
63 className="mt-3"
64 size="sm"
65 value={text}
66 placeholder="Request change"
67 aria-label={`Comment on lines ${rangeLabel}`}
68 onChange={(event) => setText(event.target.value)}
69 onKeyDown={(event) => {
70 if (event.key === "Escape") {
71 event.preventDefault();
72 onCancel();
73 }
74 if ((event.metaKey || event.ctrlKey) && event.key === "Enter" && text.trim()) {
75 event.preventDefault();
76 onComment(text.trim());
77 }
78 }}
79 />
80 <div className="mt-3 flex justify-end gap-2">
81 <Button variant="ghost" size="sm" onClick={onCancel}>
82 Cancel
83 </Button>
84 <Button size="sm" disabled={!text.trim()} onClick={() => onComment(text.trim())}>
85 Comment
86 </Button>
87 </div>
88 </div>
89 );