Skip to content

Commit f3df729

Browse files
authored
Merge pull request #278 from QueueLab/fix/new-chat-button-disappears
fix(chat): Remove isButtonPressed state from ChatPanel
2 parents 9071e7b + 7cc3024 commit f3df729

5 files changed

Lines changed: 25 additions & 22 deletions

File tree

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL="postgresql://user:password@host:port/db"

app/actions.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ async function submit(formData?: FormData, skip?: boolean) {
248248
};
249249
}
250250

251+
async function clearChat() {
252+
'use server'
253+
254+
const aiState = getMutableAIState<typeof AI>()
255+
256+
aiState.done({
257+
chatId: nanoid(),
258+
messages: []
259+
})
260+
}
261+
251262
export type AIState = {
252263
messages: AIMessage[];
253264
chatId: string;
@@ -272,6 +283,7 @@ const initialUIState: UIState = [];
272283
export const AI = createAI<AIState, UIState>({
273284
actions: {
274285
submit,
286+
clearChat,
275287
},
276288
initialUIState,
277289
initialAIState,

bun.lockb

554 Bytes
Binary file not shown.

components/chat-panel.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use client'
22

33
import { useEffect, useState, useRef } from 'react'
4-
import { useRouter } from 'next/navigation'
54
import type { AI, UIState } from '@/app/actions'
65
import { useUIState, useActions } from 'ai/rsc'
76
// Removed import of useGeospatialToolMcp as it's no longer used/available
@@ -20,13 +19,11 @@ interface ChatPanelProps {
2019

2120
export function ChatPanel({ messages, input, setInput }: ChatPanelProps) {
2221
const [, setMessages] = useUIState<typeof AI>()
23-
const { submit } = useActions()
22+
const { submit, clearChat } = useActions()
2423
// Removed mcp instance as it's no longer passed to submit
25-
const [isButtonPressed, setIsButtonPressed] = useState(false)
2624
const [isMobile, setIsMobile] = useState(false)
2725
const inputRef = useRef<HTMLTextAreaElement>(null)
2826
const formRef = useRef<HTMLFormElement>(null)
29-
const router = useRouter()
3027

3128
// Detect mobile layout
3229
useEffect(() => {
@@ -38,19 +35,8 @@ export function ChatPanel({ messages, input, setInput }: ChatPanelProps) {
3835
return () => window.removeEventListener('resize', checkMobile)
3936
}, [])
4037

41-
useEffect(() => {
42-
if (isButtonPressed) {
43-
inputRef.current?.focus()
44-
setIsButtonPressed(false)
45-
}
46-
}, [isButtonPressed])
47-
4838
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
4939
e.preventDefault()
50-
if (isButtonPressed) {
51-
handleClear()
52-
setIsButtonPressed(false)
53-
}
5440
setMessages(currentMessages => [
5541
...currentMessages,
5642
{
@@ -64,16 +50,17 @@ export function ChatPanel({ messages, input, setInput }: ChatPanelProps) {
6450
setMessages(currentMessages => [...currentMessages, responseMessage as any])
6551
}
6652

67-
const handleClear = () => {
68-
router.push('/')
53+
const handleClear = async () => {
54+
setMessages([])
55+
await clearChat()
6956
}
7057

7158
useEffect(() => {
7259
inputRef.current?.focus();
7360
}, [])
7461

7562
// New chat button (appears when there are messages)
76-
if (messages.length > 0 && !isButtonPressed && !isMobile) {
63+
if (messages.length > 0 && !isMobile) {
7764
return (
7865
<div
7966
className={cn(

components/mobile-icons-bar.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client'
22

33
import React from 'react'
4-
import { useRouter } from 'next/navigation'
4+
import { useUIState, useActions } from 'ai/rsc'
5+
import { AI } from '@/app/actions'
56
import { Button } from '@/components/ui/button'
67
import {
78
Search,
@@ -18,10 +19,12 @@ import { MapToggle } from './map-toggle'
1819
import { ModeToggle } from './mode-toggle'
1920

2021
export const MobileIconsBar: React.FC = () => {
21-
const router = useRouter()
22+
const [, setMessages] = useUIState<typeof AI>()
23+
const { clearChat } = useActions()
2224

23-
const handleNewChat = () => {
24-
router.push('/')
25+
const handleNewChat = async () => {
26+
setMessages([])
27+
await clearChat()
2528
}
2629

2730
return (

0 commit comments

Comments
 (0)