@@ -4,6 +4,7 @@ import { memo, useCallback, useEffect, useRef, useState } from 'react'
44import { createLogger } from '@sim/logger'
55import { History , Plus , Square } from 'lucide-react'
66import { useParams , useRouter } from 'next/navigation'
7+ import { usePostHog } from 'posthog-js/react'
78import { useShallow } from 'zustand/react/shallow'
89import {
910 BubbleChatClose ,
@@ -33,6 +34,7 @@ import {
3334import { Lock , Unlock , Upload } from '@/components/emcn/icons'
3435import { VariableIcon } from '@/components/icons'
3536import { useSession } from '@/lib/auth/auth-client'
37+ import { captureEvent } from '@/lib/posthog/client'
3638import { generateWorkflowJson } from '@/lib/workflows/operations/import-export'
3739import { ConversationListItem } from '@/app/workspace/[workspaceId]/components'
3840import { MothershipChat } from '@/app/workspace/[workspaceId]/home/components'
@@ -101,6 +103,9 @@ export const Panel = memo(function Panel({ workspaceId: propWorkspaceId }: Panel
101103 const params = useParams ( )
102104 const workspaceId = propWorkspaceId ?? ( params . workspaceId as string )
103105
106+ const posthog = usePostHog ( )
107+ const posthogRef = useRef ( posthog )
108+
104109 const panelRef = useRef < HTMLElement > ( null )
105110 const fileInputRef = useRef < HTMLInputElement > ( null )
106111 const { activeTab, setActiveTab, panelWidth, _hasHydrated, setHasHydrated } = usePanelStore (
@@ -264,6 +269,10 @@ export const Panel = memo(function Panel({ workspaceId: propWorkspaceId }: Panel
264269 loadCopilotChats ( )
265270 } , [ loadCopilotChats ] )
266271
272+ useEffect ( ( ) => {
273+ posthogRef . current = posthog
274+ } , [ posthog ] )
275+
267276 const handleCopilotSelectChat = useCallback ( ( chat : { id : string ; title : string | null } ) => {
268277 setCopilotChatId ( chat . id )
269278 setCopilotChatTitle ( chat . title )
@@ -394,6 +403,14 @@ export const Panel = memo(function Panel({ workspaceId: propWorkspaceId }: Panel
394403 [ copilotEditQueuedMessage ]
395404 )
396405
406+ const handleCopilotStopGeneration = useCallback ( ( ) => {
407+ captureEvent ( posthogRef . current , 'task_generation_aborted' , {
408+ workspace_id : workspaceId ,
409+ view : 'copilot' ,
410+ } )
411+ copilotStopGeneration ( )
412+ } , [ copilotStopGeneration , workspaceId ] )
413+
397414 const handleCopilotSubmit = useCallback (
398415 ( text : string , fileAttachments ?: FileAttachmentForApi [ ] , contexts ?: ChatContext [ ] ) => {
399416 const trimmed = text . trim ( )
@@ -833,7 +850,7 @@ export const Panel = memo(function Panel({ workspaceId: propWorkspaceId }: Panel
833850 isSending = { copilotIsSending }
834851 isReconnecting = { copilotIsReconnecting }
835852 onSubmit = { handleCopilotSubmit }
836- onStopGeneration = { copilotStopGeneration }
853+ onStopGeneration = { handleCopilotStopGeneration }
837854 messageQueue = { copilotMessageQueue }
838855 onRemoveQueuedMessage = { copilotRemoveFromQueue }
839856 onSendQueuedMessage = { copilotSendNow }
0 commit comments