use-handle-log-entry-click.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { MouseEventHandler, useCallback } from 'react'
  2. import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
  3. import { ErrorLevel, SourceLocation } from '../util/types'
  4. const useHandleLogEntryClick = ({
  5. level,
  6. ruleId,
  7. sourceLocation,
  8. onSourceLocationClick,
  9. }: {
  10. level: ErrorLevel | undefined
  11. ruleId: string | undefined
  12. sourceLocation: SourceLocation | undefined
  13. onSourceLocationClick?: (location: SourceLocation) => void
  14. }) => {
  15. const { sendEvent } = useEditorAnalytics()
  16. const handleLogEntryLinkClick: MouseEventHandler<HTMLButtonElement> =
  17. useCallback(
  18. event => {
  19. event.preventDefault()
  20. if (onSourceLocationClick && sourceLocation) {
  21. onSourceLocationClick(sourceLocation)
  22. const parts = sourceLocation?.file?.split('.')
  23. const extension =
  24. parts?.length && parts?.length > 1 ? parts.pop() : ''
  25. sendEvent('log-entry-link-click', { level, ruleId, extension })
  26. }
  27. },
  28. [level, onSourceLocationClick, ruleId, sourceLocation, sendEvent]
  29. )
  30. return handleLogEntryLinkClick
  31. }
  32. export default useHandleLogEntryClick