diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx index 87261b2504d..2ec3ba00662 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx @@ -30,10 +30,7 @@ import { useShallow } from 'zustand/react/shallow' import { VariableIcon } from '@/components/icons' import { ThinkingLoader } from '@/components/ui' import { requestJson } from '@/lib/api/client/request' -import { - createWorkflowCopilotChatContract, - deleteCopilotChatContract, -} from '@/lib/api/contracts/copilot' +import { createWorkflowCopilotChatContract } from '@/lib/api/contracts/copilot' import { getWorkflowNormalizedStateContract } from '@/lib/api/contracts/workflows' import { useSession } from '@/lib/auth/client' import { getWorkspaceUsageLimitAction } from '@/lib/billing/workspace-permissions' @@ -79,6 +76,7 @@ import { type CopilotChatListItem, copilotChatsKeys, useCopilotChats, + useDeleteCopilotChat, } from '@/hooks/queries/copilot-chats' import { useFolderMap } from '@/hooks/queries/folders' import { isWorkflowEffectivelyLocked } from '@/hooks/queries/utils/folder-tree' @@ -89,7 +87,6 @@ import { usePermissionConfig } from '@/hooks/use-permission-config' import { useSettingsNavigation } from '@/hooks/use-settings-navigation' import { useChatStore } from '@/stores/chat/store' import { useVariablesModalStore } from '@/stores/modals/variables/store' -import { useMothershipDraftsStore } from '@/stores/mothership-drafts/store' import type { ChatContext, PanelTab } from '@/stores/panel' import { usePanelStore } from '@/stores/panel' import { useVariablesStore } from '@/stores/variables/store' @@ -393,23 +390,37 @@ export const Panel = memo(function Panel() { [setCopilotChatId] ) + const { mutate: deleteCopilotChat, isPending: isDeletingCopilotChat } = useDeleteCopilotChat() const handleCopilotDeleteChat = useCallback( (chatId: string) => { - requestJson(deleteCopilotChatContract, { body: { chatId } }) - .then(() => { - if (copilotChatId === chatId) { - setCopilotChatId(undefined) - } - // The draft store is persisted, so an unpruned key survives forever. - const draftKey = copilotDraftKey(workspaceId, copilotDraftWorkflowId, chatId) - if (draftKey) useMothershipDraftsStore.getState().clearDraft(draftKey) - loadCopilotChats() - }) - .catch((err) => { - logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) - }) + // Leave the chat first: its deleted event drops the open transcript, which would refetch. + // Auto-select must not reopen it from the list while the delete is in flight. + if (copilotChatId === chatId) { + if (activeWorkflowId) autoSelectAttemptedFor.add(activeWorkflowId) + setCopilotChatId(undefined) + } + deleteCopilotChat( + { + chatId, + workflowId: activeWorkflowId ?? undefined, + draftKey: copilotDraftKey(workspaceId, copilotDraftWorkflowId, chatId), + }, + { + onError: (err) => { + logger.error('Failed to delete copilot chat', { error: toError(err).message, chatId }) + }, + } + ) }, - [copilotChatId, loadCopilotChats, setCopilotChatId, workspaceId, copilotDraftWorkflowId] + [ + activeWorkflowId, + autoSelectAttemptedFor, + copilotChatId, + copilotDraftWorkflowId, + deleteCopilotChat, + setCopilotChatId, + workspaceId, + ] ) const handleCopilotToolResult = useCallback( @@ -1095,6 +1106,7 @@ export const Panel = memo(function Panel() { action={ handleCopilotDeleteChat(chat.id)} > diff --git a/apps/sim/hooks/queries/copilot-chats.ts b/apps/sim/hooks/queries/copilot-chats.ts index e9df553dbff..4cefd12496c 100644 --- a/apps/sim/hooks/queries/copilot-chats.ts +++ b/apps/sim/hooks/queries/copilot-chats.ts @@ -1,7 +1,12 @@ -import { skipToken, useQuery } from '@tanstack/react-query' +import { skipToken, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { ApiClientError } from '@/lib/api/client/errors' import { requestJson } from '@/lib/api/client/request' -import { type CopilotChatListItem, listCopilotChatsContract } from '@/lib/api/contracts/copilot' +import { + type CopilotChatListItem, + deleteCopilotChatContract, + listCopilotChatsContract, +} from '@/lib/api/contracts/copilot' +import { useMothershipDraftsStore } from '@/stores/mothership-drafts/store' export type { CopilotChatListItem } @@ -38,3 +43,25 @@ export function useCopilotChats(workflowId?: string) { staleTime: COPILOT_CHAT_LIST_STALE_TIME, }) } + +interface DeleteCopilotChatVariables { + chatId: string + /** Workflow whose chat list drops the chat, captured when the delete starts. */ + workflowId?: string + /** Persisted composer draft to prune once the chat is gone. */ + draftKey?: string +} + +/** Deletes a workflow copilot chat, prunes its draft, and refreshes its workflow's chat list. */ +export function useDeleteCopilotChat() { + const queryClient = useQueryClient() + return useMutation({ + mutationFn: ({ chatId }: DeleteCopilotChatVariables) => + requestJson(deleteCopilotChatContract, { body: { chatId } }), + onSuccess: (_data, { workflowId, draftKey }) => { + // The draft store is persisted, so an unpruned key survives forever. + if (draftKey) useMothershipDraftsStore.getState().clearDraft(draftKey) + return queryClient.invalidateQueries({ queryKey: copilotChatsKeys.list(workflowId) }) + }, + }) +}