All files / features / pipelines PipelineDetailPage.tsx

70% Statements 91/130
64.58% Branches 62/96
58.97% Functions 23/39
74.73% Lines 71/95

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                            39x   39x 10x 60x 60x 60x 60x 60x 60x 60x 36x 36x 36x 2x 39x       60x             10x   60x         10x       60x     10x 10x 10x             10x 60x 60x 60x   10x           2x 2x 2x 60x 60x 60x 60x 2x   2x       2x   10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 1x   10x 10x         1x                                         10x 4x   4x     4x                       6x                                                                                                                        
import { useState } from 'react'
import { useParams, useNavigate } from 'react-router-dom'
import { usePipeline, useDeletePipeline } from './pipelinesApi'
import { useProject } from '@/features/projects/projectsApi'
import { useRuns, useCreateRun } from '@/features/runs/runsApi'
import { DataTable, type Column } from '@/shared/components/DataTable'
import { Breadcrumb } from '@/shared/components/Breadcrumb'
import { ConfirmDialog } from '@/shared/components/ConfirmDialog'
import { RunPhaseIcon } from '@/shared/components/RunPhaseIcon'
import { SourceBadge } from '@/shared/components/SourceBadge'
import { DurationDisplay } from '@/shared/components/DurationDisplay'
import { Button } from '@/components/ui/button'
import { Card, CardContent } from '@/components/ui/card'
import { formatDateTime } from '@/shared/utils/format'
 
function relativeTime(dateStr: string | undefined): string {
  Iif (!dateStr) return '\u2014'
  const diff = Date.now() - new Date(dateStr).getTime()
 I const mins = Math.floor(diff / 60000)
  Iif (mins < 1) return 'now'
  Iif (mins < 60) return `${mins} min ago`
 I const hours = Math.floor(mins / 60)
 I if (hours < 24) return `${hours}h ago`
  const days = Math.floor(hours / 24)
  Iif (days < 7) return `${days}d ago`
  return new Date(dateStr).toLocaleDateString()
}I
 
const runColumns: Column[] = [
  {
    key: 'phase',
    label: 'Phase',
    render: (item) => (
      <RunPhaseIcon phase={item.phase as 'pending' | 'running' | 'succeeded' | 'failed'} />
    ),
  },
  {
    key: 'source_type',
    label: 'Source',
    render: (item) => (
      <SourceBadge source={item.source_type as 'manual' | 'webhook' | 'schedule' | 'cli'} />
    ),
  },
  {
    key: 'created_at',
    label: 'Created',
    render: (item) => (
      <span className="text-sm text-muted-foreground">{relativeTime(item.created_at as string)}</span>
    ),
  },
  {
    key: 'tokens',
    label: 'Tokens',
    render: (item) => {
      const input = item.token_input as number
      const output = item.token_output as number
      return <span>{(input + output).toLocaleString()}</span>
    },
  },
  {
    key: 'duration',
    label: 'Duration',
    render: (item) => {
      const started = item.started_at as string | null
      const completed = item.completed_at as string | null
      const ms = started && completed
        ? new Date(completed).getTime() - new Date(started).getTime()
        : null
      return <DurationDisplay ms={ms} />
    },
  },
]
 
export function PipelineDetailPage() {
  const { id } = useParams<{ id: string }>()
  const navigate = useNavigate()
  const { data: pipeline, isLoading, error, refetch } = usePipeline(id ?? '')
  const { data: projectData } = useProject(pipeline?.project_id ?? '')
  const deletePipeline = useDeletePipeline()
  const createRun = useCreateRun()
  const [showDelete, setShowDelete] = useState(false)
  const project = projectData?.project
 
  const { data: runsData, isLoading: runsLoading } = useRuns({
    pipeline_id: id,
    limit: 10,
  })
  const runs = runsData?.data ?? []
 
  Iif (error) {
    const is404 = (error as { response?: { status?: number } })?.response?.status === 404
    return (
      <div className="flex flex-col items-center gap-4 py-12">
        <p className="text-destructive">{is404 ? 'Pipeline not found' : 'Failed to load pipeline'}</p>
        <Button variant="outline" onClick={() => refetch()}>Retry</Button>
      </div>
    )
  }
 
  if (isLoading || !pipeline) {
    return (
      <div className="flex flex-col gap-6">
        <div className="h-8 w-48 animate-pulse rounded bg-muted" />
 I       <div className="h-48 animate-pulse rounded-lg bg-muted" />
      </div>
    )
  }
 
  return (
    <div className="flex flex-col gap-6">
      <Breadcrumb items={[
        { label: 'Dashboard', href: '/' },
        { label: 'Projects', href: '/projects' },
        { label: project?.name ?? '...', href: `/projects/${pipeline.project_id}` },
        { label: 'Pipelines' },
        { label: pipeline.name },
      ]} />
 
      <div className="flex items-center justify-between">
        <h1 className="text-2xl font-bold">{pipeline.name}</h1>
        <div className="flex gap-2">
          <Button variant="default" onClick={() =>
            createRun.mutate(
              { pipeline_id: id ?? '', source_type: 'manual', source_metadata: null as never },
              { onSuccess: (run) => navigate(`/runs/${run.run_id}`) },
            )
          }>
            Run Now
          </Button>
          <Button variant="ghost" onClick={() => navigate(`/runs?pipeline_id=${id}`)}>
            View Runs
          </Button>
          <Button variant="outline" onClick={() => navigate(`/pipelines/${id}/edit`)}>
            Edit
          </Button>
          <Button variant="destructive" onClick={() => setShowDelete(true)}>
            Delete
          </Button>
        </div>
      </div>
 
      <Card>
        <CardContent className="grid grid-cols-2 gap-4 p-4 text-sm">
          <div>
            <span className="text-muted-foreground">Project</span>
            <p>
              <a
                href={`#/projects/${pipeline.project_id}`}
                className="text-primary hover:underline"
              >
                {project?.name ?? pipeline.project_id}
              </a>
            </p>
          </div>
          <div>
            <span className="text-muted-foreground">Enabled</span>
            <p>{pipeline.enabled ? 'Yes' : 'No'}</p>
          </div>
          <div className="col-span-2">
            <span className="text-muted-foreground">Description</span>
            <p>{pipeline.description ?? '\u2014'}</p>
          </div>
          <div className="col-span-2">
            <span className="text-muted-foreground">Manifest Source URL</span>
            <p className="font-mono text-sm break-all">{pipeline.manifest_source_url}</p>
          </div>
          <div>
            <span className="text-muted-foreground">Output Branch</span>
            <p className="font-mono text-sm">{pipeline.output_branch ?? '\u2014'}</p>
          </div>
          <div>
            <span className="text-muted-foreground">Created</span>
            <p>{formatDateTime(pipeline.created_at)}</p>
          </div>
          <div>
            <span className="text-muted-foreground">Updated</span>
            <p>{formatDateTime(pipeline.updated_at)}</p>
          </div>
        </CardContent>
      </Card>
 
      <h2 className="text-lg font-semibold">Recent Runs</h2>
      <DataTable
        columns={runColumns}
        data={runs as unknown as Record<string, unknown>[]}
        isLoading={runsLoading}
        emptyMessage="No runs for this pipeline"
        onRowClick={(item) => navigate(`/runs/${item.run_id as string}`)}
      />
 
      <ConfirmDialog
        open={showDelete}
        title="Delete pipeline"
        message={`Are you sure you want to delete '${pipeline.name}'?`}
        onConfirm={() => {
          if (id) {
            deletePipeline.mutate(id, {
              onSuccess: () => navigate('/projects', { replace: true }),
            })
          }
          setShowDelete(false)
        }}
        onCancel={() => setShowDelete(false)}
      />
    </div>
  )
}