use-stop-on-first-error.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { useCallback } from 'react'
  2. import { useDetachCompileContext as useCompileContext } from '../context/detach-compile-context'
  3. import { useProjectContext } from '../context/project-context'
  4. import * as eventTracking from '../../infrastructure/event-tracking'
  5. type UseStopOnFirstErrorProps = {
  6. eventSource?: string
  7. }
  8. export function useStopOnFirstError(opts: UseStopOnFirstErrorProps = {}) {
  9. const { eventSource } = opts
  10. const { stopOnFirstError, setStopOnFirstError } = useCompileContext()
  11. const { projectId } = useProjectContext()
  12. type Opts = {
  13. projectId: string
  14. source?: UseStopOnFirstErrorProps['eventSource']
  15. }
  16. const enableStopOnFirstError = useCallback(() => {
  17. if (!stopOnFirstError) {
  18. const opts: Opts = { projectId }
  19. if (eventSource) {
  20. opts.source = eventSource
  21. }
  22. eventTracking.sendMB('stop-on-first-error-enabled', opts)
  23. }
  24. setStopOnFirstError(true)
  25. }, [eventSource, projectId, stopOnFirstError, setStopOnFirstError])
  26. const disableStopOnFirstError = useCallback(() => {
  27. const opts: Opts = { projectId }
  28. if (eventSource) {
  29. opts.source = eventSource
  30. }
  31. if (stopOnFirstError) {
  32. eventTracking.sendMB('stop-on-first-error-disabled', opts)
  33. }
  34. setStopOnFirstError(false)
  35. }, [eventSource, projectId, stopOnFirstError, setStopOnFirstError])
  36. return { enableStopOnFirstError, disableStopOnFirstError }
  37. }