@@ -265,11 +265,9 @@ export const Panel = memo(function Panel({ workspaceId: propWorkspaceId }: Panel
265265
266266 if ( copilotChatId ) return
267267 if ( autoSelectAttemptedForRef . current . has ( activeWorkflowId ) ) return
268+ if ( copilotChatList . length === 0 ) return
268269 autoSelectAttemptedForRef . current . add ( activeWorkflowId )
269-
270- if ( copilotChatList . length > 0 ) {
271- setCopilotChatId ( copilotChatList [ 0 ] . id )
272- }
270+ setCopilotChatId ( copilotChatList [ 0 ] . id )
273271 } , [ copilotChatList , copilotChatId , activeWorkflowId , setCopilotChatId ] )
274272
275273 useEffect ( ( ) => {
@@ -362,15 +360,30 @@ export const Panel = memo(function Panel({ workspaceId: propWorkspaceId }: Panel
362360 } )
363361 . then ( ( res ) => ( res . ok ? res . json ( ) : Promise . reject ( new Error ( 'create chat failed' ) ) ) )
364362 . then ( ( data : { id ?: string } ) => {
365- if ( data ?. id ) {
366- setCopilotChatId ( data . id )
367- loadCopilotChats ( )
368- }
363+ if ( ! data ?. id ) return
364+ // Seed the new chat into the list cache before selecting it. Without this, the
365+ // auto-select effect sees a selected id that isn't in the (still-stale) list and
366+ // deselects it, which leaves the panel detached from the freshly created row.
367+ queryClient . setQueryData < CopilotChatListItem [ ] > (
368+ copilotChatsKeys . list ( activeWorkflowId ) ,
369+ ( prev ) => [
370+ {
371+ id : data . id ! ,
372+ title : null ,
373+ workflowId : activeWorkflowId ,
374+ updatedAt : new Date ( ) . toISOString ( ) ,
375+ activeStreamId : null ,
376+ } ,
377+ ...( prev ?? [ ] ) ,
378+ ]
379+ )
380+ setCopilotChatId ( data . id )
381+ loadCopilotChats ( )
369382 } )
370383 . catch ( ( err ) => {
371384 logger . error ( 'Failed to create copilot chat' , err )
372385 } )
373- } , [ activeWorkflowId , workspaceId , loadCopilotChats , setCopilotChatId ] )
386+ } , [ activeWorkflowId , workspaceId , loadCopilotChats , setCopilotChatId , queryClient ] )
374387
375388 const prevResolvedRef = useRef < string | undefined > ( undefined )
376389 useEffect ( ( ) => {
0 commit comments