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