Skip to content

Commit ecbf5e5

Browse files
Merge pull request #4342 from simstudioai/staging
v0.6.62: fix new copilot chat creation and selection on refresh
2 parents 6aa3fe3 + 8d042f7 commit ecbf5e5

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)