Open History to see the last commit
1import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
2import { useState, type Ref } from "react";
3
4import { Button } from "../ui/button";
5
6export function AssistantCitationCommentEditor({
7 citation,
8 inputRef,
9 onSubmit,
10 onCancel,
11}: {
12 citation: AssistantCitation;
13 inputRef?: Ref<HTMLTextAreaElement>;
14 onSubmit: (comment: string) => boolean;
15 onCancel: () => void;
16}) {
17 const [comment, setComment] = useState(citation.comment ?? "");
18 const commentTooLong = comment.length > ASSISTANT_CITATION_MAX_COMMENT_LENGTH;
19 const submit = () => {
20 if (!commentTooLong) onSubmit(comment);
21 };
22
23 return (
24 <div
25 data-citation-comment-editor="true"
26 onKeyDown={(event) => {
27 event.stopPropagation();
28 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
29 if (event.key === "Escape") {
30 event.preventDefault();
31 onCancel();
32 }
33 }}
34 >
35 <textarea
36 ref={inputRef}
37 aria-label="Comment on selected text"
38 aria-description="Enter to save the citation comment; Shift+Enter for a new line."
39 aria-invalid={commentTooLong || undefined}
40 placeholder="Add an optional comment..."
41 rows={2}
42 className="field-sizing-content block max-h-40 min-h-16 w-full resize-none bg-transparent px-1 py-1.5 text-base outline-none placeholder:text-muted-foreground sm:text-sm"
43 value={comment}
44 onChange={(event) => setComment(event.currentTarget.value)}
45 onKeyDown={(event) => {
46 if (
47 event.key === "Enter" &&
48 !event.shiftKey &&
49 !event.nativeEvent.isComposing &&
50 event.keyCode !== 229
51 ) {
52 event.preventDefault();
53 submit();
54 }
55 }}
56 />
57 {commentTooLong ? (
58 <p role="status" className="pt-1 text-xs text-destructive">
59 Comments can contain up to {ASSISTANT_CITATION_MAX_COMMENT_LENGTH.toLocaleString()}{" "}
60 characters.
61 </p>
62 ) : null}
63 <div className="mt-2 flex items-center justify-end gap-2">
64 <Button
65 variant="outline"
66 size="xs"
67 onPointerDown={(event) => event.preventDefault()}
68 onClick={onCancel}
69 >
70 Cancel
71 </Button>
72 <Button
73 size="xs"
74 disabled={commentTooLong}
75 onPointerDown={(event) => event.preventDefault()}
76 onClick={submit}
77 >
78 {commentTooLong ? "Shorten comment" : "Save"}
79 </Button>
80 </div>
81 </div>
82 );
83}