2022-06-13 08:08:38 -04:00
|
|
|
import { useCallback } from 'react'
|
|
|
|
import { useDetachCompileContext as useCompileContext } from '../context/detach-compile-context'
|
|
|
|
import { useProjectContext } from '../context/project-context'
|
|
|
|
import * as eventTracking from '../../infrastructure/event-tracking'
|
|
|
|
|
2023-01-09 09:33:43 -05:00
|
|
|
type UseStopOnFirstErrorProps = {
|
|
|
|
eventSource?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useStopOnFirstError(opts: UseStopOnFirstErrorProps = {}) {
|
2022-06-13 08:08:38 -04:00
|
|
|
const { eventSource } = opts
|
|
|
|
const { stopOnFirstError, setStopOnFirstError } = useCompileContext()
|
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
2023-01-09 09:33:43 -05:00
|
|
|
type Opts = {
|
|
|
|
projectId: string
|
|
|
|
source?: UseStopOnFirstErrorProps['eventSource']
|
|
|
|
}
|
|
|
|
|
2022-06-13 08:08:38 -04:00
|
|
|
const enableStopOnFirstError = useCallback(() => {
|
|
|
|
if (!stopOnFirstError) {
|
2023-01-09 09:33:43 -05:00
|
|
|
const opts: Opts = { projectId }
|
2022-06-13 08:08:38 -04:00
|
|
|
if (eventSource) {
|
|
|
|
opts.source = eventSource
|
|
|
|
}
|
|
|
|
eventTracking.sendMB('stop-on-first-error-enabled', opts)
|
|
|
|
}
|
|
|
|
setStopOnFirstError(true)
|
|
|
|
}, [eventSource, projectId, stopOnFirstError, setStopOnFirstError])
|
|
|
|
|
|
|
|
const disableStopOnFirstError = useCallback(() => {
|
2023-01-09 09:33:43 -05:00
|
|
|
const opts: Opts = { projectId }
|
2022-06-13 08:08:38 -04:00
|
|
|
if (eventSource) {
|
|
|
|
opts.source = eventSource
|
|
|
|
}
|
|
|
|
if (stopOnFirstError) {
|
|
|
|
eventTracking.sendMB('stop-on-first-error-disabled', opts)
|
|
|
|
}
|
|
|
|
setStopOnFirstError(false)
|
|
|
|
}, [eventSource, projectId, stopOnFirstError, setStopOnFirstError])
|
|
|
|
|
|
|
|
return { enableStopOnFirstError, disableStopOnFirstError }
|
|
|
|
}
|