All files / features / pipelines PipelineDetailPage.tsx

63.38% Statements 45/71
67.24% Branches 39/58
46.87% Functions 15/32
65.67% Lines 44/67

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281                                      69x   69x 69x       120x                 120x                 120x                       120x                         120x 120x                 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x       18x 18x 18x 18x 18x 48x         18x                                                             12x                         12x                     12x                         12x                                                                                                                                                                                                                          
import { useState, useRef } from 'react'
import { useParams, useNavigate, useSearchParams } from 'react-router-dom'
import { usePipeline, useDeletePipeline } from './pipelinesApi'
import { useProject } from '@/features/projects/projectsApi'
import { useRepositories } from '@/features/repositories/repositoriesApi'
import { usePipelineRunFiltersAndData } from './hooks/usePipelineRunFiltersAndData'
import { useCreateRun } from '@/features/runs/runsApi'
import { RunPhaseIcon } from '@/shared/components/RunPhaseIcon'
import { SourceBadge } from '@/shared/components/SourceBadge'
import { DurationDisplay } from '@/shared/components/DurationDisplay'
import { DataTable, type Column } from '@/shared/components/DataTable'
import { CrudReadCard } from '@/shared/components/CrudReadCard'
import { EntitySelect } from '@/shared/components/EntitySelect'
import type { SelectionOption } from '@/shared/components/EntitySelect'
import { Button } from '@/components/ui/button'
import { CrudModal } from '@/shared/components/CrudModal'
import { ConfirmDialog } from '@/shared/components/ConfirmDialog'
import { Page } from '@/shared/components/Page'
import { formatDateTime, formatRelativeTime, abbreviateNumber } from '@/shared/utils/format'
import type { components } from '@/types/api'
import { PipelineForm, type CrudFormRef as PipelineFormRef } from '@/features/pipelines/components/PipelineForm'
 
type Run = components['schemas']['Run']
 
const runsColumns: Column<Run>[] = [
  {
    key: 'phase',
    label: 'Phase',
    render: (item) => <RunPhaseIcon phase={item.phase ?? 'pending'} />,
  },
  {
    key: 'source_type',
    label: 'Source',
    render: (item) => (
      <SourceBadge source={item.source_type ?? 'manual'} />
    ),
  },
  {
    key: 'created_at',
    label: 'Created',
    render: (item) => (
      <span className="text-sm text-muted-foreground">
        {formatRelativeTime(item.created_at)}
      </span>
    ),
  },
  {
    key: 'tokens',
    label: 'Tokens',
    render: (item) => (
      <span className="text-sm text-muted-foreground">
        {abbreviateNumber(
          (item.token_input ?? 0) + (item.token_output ?? 0) || null,
        )}
      </span>
    ),
  },
  {
    key: 'duration',
    label: 'Duration',
    render: (item) => {
      const ms =
        item.started_at && item.completed_at
          ? new Date(item.completed_at).getTime() -
            new Date(item.started_at).getTime()
          : null
      return <DurationDisplay ms={ms} />
    },
  },
]
 
export function PipelineDetailPage() {
  const { id } = useParams<{ id: string }>()
  const [searchParams, setSearchParams] = useSearchParams()
  const nav = useNavigate()
 
  const { data: pipeline, isLoading, error, refetch } = usePipeline(id ?? '')
  const { data: projectData } = useProject(pipeline?.project_id ?? '')
  const { data: reposData, isLoading: reposLoading } = useRepositories()
  const deletePipeline = useDeletePipeline()
  const createRun = useCreateRun()
  const [showDelete, setShowDelete] = useState(false)
 
  const {
    data: runsData,
    isLoading: runsLoading,
    sse,
  } = usePipelineRunFiltersAndData({ prefix: 'plruns', pipelineId: id ?? '' })
 
  const [formDirty, setFormDirty] = useState(false)
  const [formPending, setFormPending] = useState(false)
  const formRef = useRef<PipelineFormRef>(null)
  const showEdit = searchParams.get('edit') === 'true'
 
  const project = projectData?.project
 
  const repoOptions: SelectionOption[] = (reposData?.data ?? []).map((r) => ({
    value: r.id,
    label: r.name,
    description: r.url,
  }))
 
  return (
    <Page>
      <CrudReadCard
        title={pipeline?.name ?? 'Pipeline'}
        breadcrumbs={[
          { label: 'Dashboard', href: '/' },
          { label: 'Projects', href: '/projects' },
          { label: project?.name ?? '...', href: `/projects/${pipeline?.project_id}` },
          { label: pipeline?.name ?? '...' },
        ]}
        fields={[
          { label: 'Name', value: pipeline?.name, spanFull: true },
          { label: 'Description', value: pipeline?.description || '\u2014', spanFull: true },
          {
            label: 'Code Repo',
            render: () => (
              <EntitySelect
                options={repoOptions}
                value={pipeline?.code_repo_id}
                readOnly
                isLoading={reposLoading}
              />
            ),
          },
          {
            label: 'Output Branch',
            render: () => (
              <span className="font-mono text-sm">
                {pipeline?.output_branch || '\u2014'}
              </span>
            ),
          },
          {
            label: 'Manifest Repo',
            render: () => (
              <EntitySelect
                options={repoOptions}
                value={pipeline?.manifest_repo_id}
                readOnly
                isLoading={reposLoading}
              />
            ),
          },
          {
            label: 'Manifest Path',
            render: () => (
              <span className="font-mono text-sm break-all">
                {pipeline?.manifest_path}
              </span>
            ),
          },
          {
            label: 'Enabled',
            value: pipeline?.enabled ? '\u2705' : '\u274C',
          },
          {
            label: 'Created',
            value: formatDateTime(pipeline?.created_at),
          },
          {
            label: 'Updated',
            value: formatDateTime(pipeline?.updated_at),
          },
        ]}
        actions={{
          extra: [
            {
              label: 'Run Now',
              onClick: () =>
                createRun.mutate(
                  {
                    pipeline_id: id ?? '',
                    source_type: 'manual',
                    source_metadata: null,
                  },
                  {
                    onSuccess: (run) => nav(`/runs/${run.run_id}`),
                  },
                ),
            },
          ],
          edit: {
            onClick: () =>
              setSearchParams((prev) => {
                prev.set('edit', 'true')
                return prev
              }),
          },
          delete: {
            onClick: () => setShowDelete(true),
            entityName: pipeline?.name ?? 'this pipeline',
          },
        }}
        isLoading={isLoading}
        error={error}
        is404={error ? (error as { response?: { status?: number } })?.response?.status === 404 : false}
        onRetry={refetch}
        backPath={`/projects/${pipeline?.project_id}`}
      />
 
      <div className="flex items-center justify-between">
        <h2 className="text-lg font-semibold">Recent Runs</h2>
        <Button
          variant="outline"
          size="sm"
          onClick={() =>
            nav(
              `/runs?runs_pipeline_id=${id}&runs_sort=created_at&runs_order=desc`,
            )
          }
        >
          View All Runs &rarr;
        </Button>
      </div>
      <DataTable<Run>
        prefix="plruns"
        columns={runsColumns}
        data={runsData}
        isLoading={runsLoading}
        sse={sse}
        entityName="Run"
        onRowClick={(item) => nav(`/runs/${item.run_id}`)}
      />
 
      <CrudModal
        open={showEdit}
        title="Edit Pipeline"
        isDirty={formDirty}
        isPending={formPending}
        onClose={() =>
          setSearchParams((prev) => {
            prev.delete('edit')
            return prev
          })
        }
        onSave={() => formRef.current?.submit()}
      >
        <PipelineForm
          ref={formRef}
          projectId={pipeline?.project_id ?? ''}
          id={id}
          defaults={{
            name: pipeline?.name,
            code_repo_id: pipeline?.code_repo_id,
            manifest_repo_id: pipeline?.manifest_repo_id,
            manifest_path: pipeline?.manifest_path,
            output_branch: pipeline?.output_branch ?? undefined,
            enabled: pipeline?.enabled,
          }}
          onSuccess={() => {
            setSearchParams({})
            refetch()
          }}
          onClose={() =>
            setSearchParams((prev) => {
              prev.delete('edit')
              return prev
            })
          }
          onDirtyChange={setFormDirty}
          onPendingChange={setFormPending}
        />
      </CrudModal>

      <ConfirmDialog
        open={showDelete}
        title="Delete pipeline"
        message={`Are you sure you want to delete '${pipeline?.name}'?`}
        onConfirm={() =>
          deletePipeline.mutate(id ?? '', {
            onSuccess: () => nav(`/projects/${pipeline?.project_id}`, { replace: true }),
          })
        }
        onCancel={() => setShowDelete(false)}
      />
    </Page>
  )
}