@@ -7,7 +7,7 @@ import assert from "assert";
77import { useRouter } from "next/navigation" ;
88import TextareaAutosize from "react-textarea-autosize" ;
99import { type Chat } from "./page" ;
10- import { useTransition } from "react" ;
10+ import { RefObject , useEffect , useRef , useTransition } from "react" ;
1111
1212export default function ChatBox ( {
1313 chat,
@@ -20,8 +20,20 @@ export default function ChatBox({
2020} ) {
2121 const [ isPending , startTransition ] = useTransition ( ) ;
2222 const router = useRouter ( ) ;
23-
2423 const disabled = isPending || isStreaming ;
24+ const didFocusOnce = useRef ( false ) ;
25+ const textareaRef = useRef < HTMLTextAreaElement > ( null ) ;
26+
27+ useEffect ( ( ) => {
28+ if ( ! textareaRef . current ) return ;
29+
30+ if ( ! disabled && ! didFocusOnce . current ) {
31+ textareaRef . current . focus ( ) ;
32+ didFocusOnce . current = true ;
33+ } else {
34+ didFocusOnce . current = false ;
35+ }
36+ } , [ disabled ] ) ;
2537
2638 return (
2739 < div className = "mx-auto mb-8 flex w-full max-w-prose shrink-0 px-8" >
@@ -46,8 +58,9 @@ export default function ChatBox({
4658 < fieldset className = "w-full" disabled = { disabled } >
4759 < div className = "relative flex rounded-lg border-4 border-gray-300 bg-white" >
4860 < TextareaAutosize
61+ ref = { textareaRef }
4962 placeholder = "Follow up"
50- autoFocus
63+ autoFocus = { ! disabled }
5164 required
5265 name = "prompt"
5366 rows = { 2 }
0 commit comments