@@ -3,6 +3,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
33import { useShallow } from 'zustand/react/shallow'
44
55import { Chat } from './chat'
6+ import { ChatHistoryScreen } from './components/chat-history-screen'
67import { LoginModal } from './components/login-modal'
78import { ProjectPickerScreen } from './components/project-picker-screen'
89import { TerminalLink } from './components/terminal-link'
@@ -15,6 +16,7 @@ import { useTerminalFocus } from './hooks/use-terminal-focus'
1516import { useTheme } from './hooks/use-theme'
1617import { getProjectRoot } from './project-files'
1718import { useChatStore , type TopBannerType } from './state/chat-store'
19+ import { useChatHistoryStore } from './state/chat-history-store'
1820import { openFileAtPath } from './utils/open-file'
1921import { formatCwd } from './utils/path-helpers'
2022import { findGitRoot } from './utils/git'
@@ -169,6 +171,32 @@ export const App = ({
169171 }
170172 } , [ gitRoot , onProjectChange ] )
171173
174+ // Chat history state from store
175+ const { showChatHistory, closeChatHistory } = useChatHistoryStore ( )
176+
177+ // State to track which chat to resume (set when user selects from history)
178+ const [ resumeChatId , setResumeChatId ] = useState < string | null > ( null )
179+
180+ const handleResumeChat = useCallback (
181+ ( chatId : string ) => {
182+ closeChatHistory ( )
183+ // Reset chat store to clear previous messages before loading the selected chat
184+ resetChatStore ( )
185+ setResumeChatId ( chatId )
186+ } ,
187+ [ closeChatHistory , resetChatStore ]
188+ )
189+
190+ const handleNewChat = useCallback ( ( ) => {
191+ closeChatHistory ( )
192+ resetChatStore ( )
193+ setResumeChatId ( null )
194+ } , [ closeChatHistory , resetChatStore ] )
195+
196+ // Determine effective continueChat values
197+ const effectiveContinueChat = continueChat || resumeChatId !== null
198+ const effectiveContinueChatId = resumeChatId ?? continueChatId
199+
172200 const headerContent = useMemo ( ( ) => {
173201 const displayPath = formatCwd ( projectRoot )
174202
@@ -252,8 +280,23 @@ export const App = ({
252280 )
253281 }
254282
283+ // Render chat history screen when requested
284+ if ( showChatHistory ) {
285+ return (
286+ < ChatHistoryScreen
287+ onSelectChat = { handleResumeChat }
288+ onCancel = { closeChatHistory }
289+ onNewChat = { handleNewChat }
290+ />
291+ )
292+ }
293+
294+ // Use key to force remount when resuming a different chat from history
295+ const chatKey = resumeChatId ?? 'current'
296+
255297 return (
256298 < Chat
299+ key = { chatKey }
257300 headerContent = { headerContent }
258301 initialPrompt = { initialPrompt }
259302 agentId = { agentId }
@@ -262,8 +305,8 @@ export const App = ({
262305 setIsAuthenticated = { setIsAuthenticated }
263306 setUser = { setUser }
264307 logoutMutation = { logoutMutation }
265- continueChat = { continueChat }
266- continueChatId = { continueChatId }
308+ continueChat = { effectiveContinueChat }
309+ continueChatId = { effectiveContinueChatId }
267310 authStatus = { authStatus }
268311 initialMode = { initialMode }
269312 gitRoot = { gitRoot }
0 commit comments