Open History to see the last commit
1import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
2import { use, useEffect, useRef, useState, type RefObject } from "react";
3
4import { TextFieldSpeechControls, useTextFieldSpeech } from "~/speech/useTextFieldSpeech";
5import { textDirection } from "~/textDirection";
6
7import { ComposerContextActionsContext } from "../composerContextActions";
8import { Button } from "../ui/button";
9
10export function AssistantCitationCommentEditor({
11 citation,
12 inputRef,
13 onSubmit,
14 onSubmitAndSend,
15 onCancel,
16 onDraftChange,
17 onVoiceSetupOpenChange,
18}: {
19 citation: AssistantCitation;
20 inputRef?: RefObject<HTMLTextAreaElement | null>;
21 onSubmit: (comment: string) => boolean;
22 onSubmitAndSend?: (comment: string) => boolean;
23 onCancel: () => void;
24 onDraftChange?: (comment: string) => void;
25 onVoiceSetupOpenChange?: (open: boolean) => void;
26}) {
27 const [comment, setComment] = useState(citation.comment ?? "");
28 const localInputRef = useRef<HTMLTextAreaElement | null>(null);
29 const textareaRef = inputRef ?? localInputRef;
30 const { environmentId, projectId } = use(ComposerContextActionsContext);
31 const updateComment = (text: string) => {
32 setComment(text);
33 onDraftChange?.(text);
34 };
35 const speech = useTextFieldSpeech({
36 environmentId: environmentId ?? citation.environmentId,
37 projectId,
38 ownerKey: JSON.stringify([
39 "citation",
40 environmentId,
41 citation.threadId,
42 citation.messageId,
43 citation.start,
44 citation.end,
45 ]),
46 text: comment,
47 textareaRef,
48 onTextChange: updateComment,
49 });
50 const setupOpen = speech.setup.open;
51 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the citation popover open while the voice setup dialog owns focus
52 useEffect(() => {
53 onVoiceSetupOpenChange?.(setupOpen);
54 return () => onVoiceSetupOpenChange?.(false);
55 }, [onVoiceSetupOpenChange, setupOpen]);
56 const commentTooLong = comment.length > ASSISTANT_CITATION_MAX_COMMENT_LENGTH;
57 const submit = () => {
58 speech.submitAfterDictation((text) => {
59 if (text.length <= ASSISTANT_CITATION_MAX_COMMENT_LENGTH) onSubmit(text);
60 });
61 };
62 const submitAndSend = () => {
63 speech.submitAfterDictation((text) => {
64 if (text.length <= ASSISTANT_CITATION_MAX_COMMENT_LENGTH) (onSubmitAndSend ?? onSubmit)(text);
65 });
66 };
67
68 return (
69 <div
70 data-citation-comment-editor="true"
71 onKeyDown={(event) => {
72 event.stopPropagation();
73 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
74 if (event.key === "Escape") {
75 event.preventDefault();
76 if (speech.state.phase !== "idle") speech.cancel();
77 else onCancel();
78 }
79 }}
80 >
81 <textarea
82 ref={textareaRef}
83 readOnly={speech.freezesEditor}
84 aria-label="Comment on selected text"
85 aria-description="Enter to save the citation comment; Command/Ctrl+Enter to save and send; Shift+Enter for a new line."
86 aria-invalid={commentTooLong || undefined}
87 placeholder="Add an optional comment..."
88 rows={2}
89 dir={textDirection(comment)}
90 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"
91 value={comment}
92 onChange={(event) => updateComment(event.currentTarget.value)}
93 onKeyDown={(event) => {
94 if (
95 event.key === "Enter" &&
96 !event.shiftKey &&
97 !event.nativeEvent.isComposing &&
98 event.keyCode !== 229
99 ) {
100 event.preventDefault();
101 if (event.metaKey || event.ctrlKey) {
102 submitAndSend();
103 } else {
104 submit();
105 }
106 }
107 }}
108 />
109 {commentTooLong ? (
110 <p role="status" className="pt-1 text-xs text-destructive">
111 Comments can contain up to {ASSISTANT_CITATION_MAX_COMMENT_LENGTH.toLocaleString()}{" "}
112 characters.
113 </p>
114 ) : null}
115 <div className="mt-2 flex flex-wrap items-center justify-end gap-2">
116 <TextFieldSpeechControls speech={speech} />
117 <Button
118 variant="outline"
119 size="xs"
120 onPointerDown={(event) => event.preventDefault()}
121 onClick={onCancel}
122 >
123 Cancel
124 </Button>
125 <Button
126 size="xs"
127 disabled={speech.preparing || (commentTooLong && !speech.blocksSubmission)}
128 onPointerDown={(event) => event.preventDefault()}
129 onClick={submit}
130 >
131 {commentTooLong ? "Shorten comment" : "Save"}
132 </Button>
133 </div>
134 </div>
135 );
136}