fix(webui): persist chatbot sidebar input text across route navigation

This commit is contained in:
yiling
2026-07-30 17:01:03 +08:00
parent fd1ff8d70e
commit 04f0a39300
3 changed files with 25 additions and 2 deletions
@@ -26,7 +26,7 @@ import {
Trash2,
Wrench,
} from 'lucide-react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useRef } from 'react'
import Markdown from 'react-markdown'
import { formatTimeAgo } from '../lib/time'
@@ -53,6 +53,8 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
abortActiveSession,
selectedAgentId,
setSelectedAgentId,
inputText,
setInputText,
} = useChatbotStore()
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
@@ -87,7 +89,6 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
}
}, [chatAgents, selectedAgentId, setSelectedAgentId])
const [inputText, setInputText] = useState('')
const messagesEndRef = useRef<HTMLDivElement>(null)
const scrollAreaRef = useRef<HTMLDivElement>(null)
const isFirstScrollRef = useRef(true)
+17
View File
@@ -39,9 +39,26 @@ describe('useChatbotStore', () => {
historySessions: [],
isStreaming: false,
selectedAgentId: 'agent-1',
inputText: '',
})
})
it('should persist and reset inputText', () => {
expect(useChatbotStore.getState().inputText).toBe('')
useChatbotStore.getState().setInputText('Draft text in progress')
expect(useChatbotStore.getState().inputText).toBe('Draft text in progress')
useChatbotStore.setState({
currentSessionId: 'sess-abc',
messages: [{ id: 'msg-1', role: 'user', content: 'test' } as unknown as ChatMessage],
})
// Navigating or changing other state does not wipe inputText
expect(useChatbotStore.getState().inputText).toBe('Draft text in progress')
useChatbotStore.getState().startNewSession()
expect(useChatbotStore.getState().inputText).toBe('')
})
it('should add, remove and clear assets in context', () => {
const store = useChatbotStore.getState()
expect(store.chatAssets).toHaveLength(0)
+5
View File
@@ -28,6 +28,8 @@ interface ChatbotState {
setIsStreaming: (streaming: boolean) => void
selectedAgentId: string | null
setSelectedAgentId: (id: string | null) => void
inputText: string
setInputText: (text: string) => void
scrollTop: number
isAtBottom: boolean
@@ -74,6 +76,8 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
setIsStreaming: (streaming) => set({ isStreaming: streaming }),
selectedAgentId: null,
setSelectedAgentId: (id) => set({ selectedAgentId: id }),
inputText: '',
setInputText: (text) => set({ inputText: text }),
scrollTop: 0,
isAtBottom: true,
setScrollState: (scrollTop, isAtBottom) => set({ scrollTop, isAtBottom }),
@@ -147,6 +151,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
currentSessionId: null,
messages: [],
chatAssets: [],
inputText: '',
scrollTop: 0,
isAtBottom: true,
})