({ disabled = false, onEnter }: Props)
| 37 | * immediately type into a freshly-created conversation. |
| 38 | */ |
| 39 | export function PromptInput({ disabled = false, onEnter }: Props) { |
| 40 | const { t } = useTranslation(); |
| 41 | const { pendingPreInput, setPendingPreInput, events } = useAIContext(); |
| 42 | |
| 43 | const [value, setValue] = useState(""); |
| 44 | const textareaRef = useRef<HTMLTextAreaElement | null>(null); |
| 45 | |
| 46 | // Focus on mount + on `new-conversation` so the user can start typing |
| 47 | // immediately. |
| 48 | useEffect(() => { |
| 49 | textareaRef.current?.focus(); |
| 50 | }, []); |
| 51 | useEffect(() => { |
| 52 | const off = events.on("new-conversation", () => { |
| 53 | textareaRef.current?.focus(); |
| 54 | }); |
| 55 | return () => { |
| 56 | off(); |
| 57 | }; |
| 58 | }, [events]); |
| 59 | |
| 60 | // Consume `pendingPreInput`: when the provider sets it, copy into |
| 61 | // local state and clear the trigger. rAF mirrors the Vue version's |
| 62 | // `flush: "post"` watch — defers to the next paint so any conversation |
| 63 | // creation that triggered the seed has landed first. |
| 64 | useEffect(() => { |
| 65 | if (!pendingPreInput) return; |
| 66 | const raf = requestAnimationFrame(() => { |
| 67 | setValue(pendingPreInput); |
| 68 | setPendingPreInput(undefined); |
| 69 | }); |
| 70 | return () => cancelAnimationFrame(raf); |
| 71 | }, [pendingPreInput, setPendingPreInput]); |
| 72 | |
| 73 | // Autosize: after every value change, set height to scrollHeight, |
| 74 | // clamped to [MIN_ROWS, MAX_ROWS] lines. |
| 75 | useLayoutEffect(() => { |
| 76 | const el = textareaRef.current; |
| 77 | if (!el) return; |
| 78 | el.style.height = "auto"; |
| 79 | const minHeight = MIN_ROWS * LINE_HEIGHT_PX; |
| 80 | const maxHeight = MAX_ROWS * LINE_HEIGHT_PX; |
| 81 | const next = Math.min(maxHeight, Math.max(minHeight, el.scrollHeight)); |
| 82 | el.style.height = `${next}px`; |
| 83 | }, [value]); |
| 84 | |
| 85 | const applyValue = (raw: string) => { |
| 86 | setValue(""); |
| 87 | onEnter(raw); |
| 88 | }; |
| 89 | |
| 90 | const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => { |
| 91 | if (e.key !== "Enter") return; |
| 92 | if (e.shiftKey) return; // Shift+Enter → newline |
| 93 | e.preventDefault(); |
| 94 | if (!value.trim()) return; |
| 95 | applyValue(value); |
| 96 | }; |
nothing calls this directly
no test coverage detected