use-editor-analytics.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
  2. import {
  3. Segmentation,
  4. sendMB,
  5. sendMBOnce,
  6. sendMBSampled,
  7. } from '@/infrastructure/event-tracking'
  8. import { useCallback } from 'react'
  9. export function populateEditorRedesignSegmentation<
  10. SegmentationType extends Segmentation,
  11. >(
  12. segmentation: SegmentationType | undefined = {} as SegmentationType,
  13. editorRedesign: boolean
  14. ): SegmentationType & { 'editor-redesign'?: 'enabled' } {
  15. return editorRedesign
  16. ? { ...segmentation, 'editor-redesign': 'enabled' }
  17. : segmentation
  18. }
  19. export const useEditorAnalytics = () => {
  20. const editorRedesign = useIsNewEditorEnabled()
  21. const populateSegmentation = useCallback(
  22. (segmentation: Segmentation | undefined = {}): Segmentation => {
  23. return populateEditorRedesignSegmentation(segmentation, editorRedesign)
  24. },
  25. [editorRedesign]
  26. )
  27. const sendEvent: typeof sendMB = useCallback(
  28. (key, segmentation) => {
  29. sendMB(key, populateSegmentation(segmentation))
  30. },
  31. [populateSegmentation]
  32. )
  33. const sendEventOnce: typeof sendMBOnce = useCallback(
  34. (key, segmentation) => {
  35. sendMBOnce(key, populateSegmentation(segmentation))
  36. },
  37. [populateSegmentation]
  38. )
  39. const sendEventSampled: typeof sendMBSampled = useCallback(
  40. (key, segmentation, rate) => {
  41. sendMBSampled(key, populateSegmentation(segmentation), rate)
  42. },
  43. [populateSegmentation]
  44. )
  45. return { sendEvent, sendEventOnce, sendEventSampled }
  46. }