All files / features / pipelines RunNowButton.tsx

47.91% Statements 23/48
32.35% Branches 11/34
63.63% Functions 7/11
63.88% Lines 23/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44          59x   59x   24x 24x 24x 24x 24x             24x 16x                             24x              
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
// depcruiser:ignore l3-not-lower — Custom view: Run Now mutation navigation (wireframes/pipeline-detail.md)
import { useMutation } from '@/layers/L1/entityResolver'
import { Button } from '@/components/ui/button'
 
export function RunNowButton({ pipelineId }: { pipelineId: string }) {
  const nav = useNavigate()
  const mutation = useMutation('runs')
  const [trackedRunId, setTrackedRunId] = useState<string | null>(null)
 
  const handleClick = () => {
    mutation.create({
      pipeline_id: pipelineId,
      source_type: 'manual',
      source_metadata: { user: 'self' },
    } as never)
  }
 
  useEffect(() => {
    if (!mutation.isPending && !mutation.isError && mutation.lastMutationData) {
  I    const data = mutation.lastMutationData as { run_id?: string } | null
      const newId = data?.run_id
      if (newId && trackedRunId !== newId) {
        setTrackedRunId(newId)
        nav(`/runs/${newId}`)
      }
    }
  }, [mutation.isPending, mutation.isError, mutation.lastMutationData, trackedRunId, nav])
 
  return (
    <Button
      variant="outline"
      size="sm"
      type="button"
      onClick={handleClick}
      disabled={mutation.isPending}
      data-testid="run-now"
    >
      {mutation.isPending ? 'Running...' : 'Run Now'}
    </Button>
  )
}