input text box content is retained

This commit is contained in:
Arjun 2026-02-17 22:50:26 +05:30 committed by tusharmagar
parent 52313b9718
commit 8cfc7fe9da

View file

@ -303,6 +303,8 @@ interface ChatInputInnerProps {
presetMessage?: string
onPresetMessageConsumed?: () => void
runId?: string | null
initialDraft?: string
onDraftChange?: (text: string) => void
}
function ChatInputInner({
@ -313,11 +315,26 @@ function ChatInputInner({
presetMessage,
onPresetMessageConsumed,
runId,
initialDraft,
onDraftChange,
}: ChatInputInnerProps) {
const controller = usePromptInputController()
const message = controller.textInput.value
const canSubmit = Boolean(message.trim()) && !isProcessing
// Restore draft on mount
useEffect(() => {
if (initialDraft) {
controller.textInput.setInput(initialDraft)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []) // only on mount
// Save draft on every change
useEffect(() => {
onDraftChange?.(message)
}, [message, onDraftChange])
// Handle preset message from suggestions
useEffect(() => {
if (presetMessage) {
@ -429,6 +446,8 @@ interface ChatInputWithMentionsProps {
presetMessage?: string
onPresetMessageConsumed?: () => void
runId?: string | null
initialDraft?: string
onDraftChange?: (text: string) => void
}
function ChatInputWithMentions({
@ -442,6 +461,8 @@ function ChatInputWithMentions({
presetMessage,
onPresetMessageConsumed,
runId,
initialDraft,
onDraftChange,
}: ChatInputWithMentionsProps) {
return (
<PromptInputProvider knowledgeFiles={knowledgeFiles} recentFiles={recentFiles} visibleFiles={visibleFiles}>
@ -453,6 +474,8 @@ function ChatInputWithMentions({
presetMessage={presetMessage}
onPresetMessageConsumed={onPresetMessageConsumed}
runId={runId}
initialDraft={initialDraft}
onDraftChange={onDraftChange}
/>
</PromptInputProvider>
)
@ -669,6 +692,17 @@ function App() {
const [activeChatTabId, setActiveChatTabId] = useState('default-chat-tab')
const chatTabIdCounterRef = useRef(0)
const newChatTabId = () => `chat-tab-${++chatTabIdCounterRef.current}`
const chatDraftsRef = useRef(new Map<string, string>())
const activeChatTabIdRef = useRef(activeChatTabId)
activeChatTabIdRef.current = activeChatTabId
const handleDraftChange = useCallback((text: string) => {
const tabId = activeChatTabIdRef.current
if (text) {
chatDraftsRef.current.set(tabId, text)
} else {
chatDraftsRef.current.delete(tabId)
}
}, [])
const getChatTabTitle = useCallback((tab: ChatTab) => {
if (!tab.runId) return 'New chat'
@ -2750,6 +2784,7 @@ function App() {
<Suggestions onSelect={setPresetMessage} className="mb-3 justify-center" />
)}
<ChatInputWithMentions
key={activeChatTabId}
knowledgeFiles={knowledgeFiles}
recentFiles={recentWikiFiles}
visibleFiles={visibleKnowledgeFiles}
@ -2760,6 +2795,8 @@ function App() {
presetMessage={presetMessage}
onPresetMessageConsumed={() => setPresetMessage(undefined)}
runId={runId}
initialDraft={chatDraftsRef.current.get(activeChatTabId)}
onDraftChange={handleDraftChange}
/>
</div>
</div>