useStudioCommandMenuTelemetry.ts 1.2 KB

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