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