use-stop-on-first-error.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  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. export function useStopOnFirstError(opts = {}) {
  6. const { eventSource } = opts
  7. const { stopOnFirstError, setStopOnFirstError } = useCompileContext()
  8. const { _id: projectId } = useProjectContext()
  9. const enableStopOnFirstError = useCallback(() => {
  10. if (!stopOnFirstError) {
  11. const opts = { projectId }
  12. if (eventSource) {
  13. opts.source = eventSource
  14. }
  15. eventTracking.sendMB('stop-on-first-error-enabled', opts)
  16. }
  17. setStopOnFirstError(true)
  18. }, [eventSource, projectId, stopOnFirstError, setStopOnFirstError])
  19. const disableStopOnFirstError = useCallback(() => {
  20. const opts = { projectId }
  21. if (eventSource) {
  22. opts.source = eventSource
  23. }
  24. if (stopOnFirstError) {
  25. eventTracking.sendMB('stop-on-first-error-disabled', opts)
  26. }
  27. setStopOnFirstError(false)
  28. }, [eventSource, projectId, stopOnFirstError, setStopOnFirstError])
  29. return { enableStopOnFirstError, disableStopOnFirstError }
  30. }