Skip to content

Commit fbd2031

Browse files
committed
Focus chatbox after new message
1 parent 3b3d2ef commit fbd2031

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

app/(main)/chats/[id]/chat-box.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assert from "assert";
77
import { useRouter } from "next/navigation";
88
import TextareaAutosize from "react-textarea-autosize";
99
import { type Chat } from "./page";
10-
import { useTransition } from "react";
10+
import { RefObject, useEffect, useRef, useTransition } from "react";
1111

1212
export 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}

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ model Chat {
2222
model String
2323
title String
2424
llamaCoderVersion String @default("v2")
25+
shadcn Boolean @default(false)
2526
messages Message[]
2627
createdAt DateTime @default(now())
2728
}

0 commit comments

Comments
 (0)