useStudioCommandMenuTelemetry.ts43 lines · main
1// @ts-nocheck
2import { useParams } from 'common'
3import type {
4 CommandMenuClosedEvent,
5 CommandMenuCommandClickedEvent,
6 CommandMenuOpenedEvent,
7 CommandMenuSearchSubmittedEvent,
8} from 'common/telemetry-constants'
9import { useCallback } from 'react'
10
11import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
12import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
13
14export function useStudioCommandMenuTelemetry() {
15 const { ref: projectRef } = useParams()
16 const { data: organization } = useSelectedOrganizationQuery()
17 const { mutate: sendEvent } = useSendEventMutation()
18
19 const onTelemetry = useCallback(
20 (
21 event:
22 | CommandMenuOpenedEvent
23 | CommandMenuClosedEvent
24 | CommandMenuCommandClickedEvent
25 | CommandMenuSearchSubmittedEvent
26 ) => {
27 // Add studio-specific groups (project and organization)
28 const eventWithGroups = {
29 ...event,
30 groups: {
31 ...event.groups,
32 ...(projectRef && { project: projectRef }),
33 ...(organization?.slug && { organization: organization.slug }),
34 },
35 }
36
37 sendEvent(eventWithGroups)
38 },
39 [projectRef, organization?.slug, sendEvent]
40 )
41
42 return { onTelemetry }
43}