All files / features / pipelines PipelineDetailPage.tsx

85.48% Statements 53/62
59.64% Branches 34/57
81.81% Functions 18/22
89.65% Lines 52/58

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                  47x   47x   4x 4x 4x 4x 4x 4x                                   4x                                                                   2x 2x         2x         2x 2x                 2x                 2x 2x     4x             40x                 40x                 40x           40x 40x 40x 40x             40x 40x 40x 40x 12x                 28x 28x 16x 16x 16x 16x 16x                
import { Outlet, useNavigate, useParams } from 'react-router-dom'
import { Page, EntityDetail, DataTable, Crud } from '@/layers/L2R'
import type { PageConfig, DetailConfig, ListConfig, CrudConfig } from '@/layers/interfaces'
import { RunPhaseIcon } from '@/shared/components/RunPhaseIcon'
import { SourceBadge } from '@/shared/components/SourceBadge'
import { abbreviateNumber, formatDateTime, formatRelativeTime } from '@/shared/utils/format'
import { RunNowButton } from './RunNowButton'
import { useRepositoryOptions } from './useRepositoryOptions'
import { useEntityDetail } from '@/layers/L1/entityResolver'
import { PIPELINE_FIELD_SET, PIPELINE_FIELD_TYPES, PIPELINE_ENTITY_SELECT_SOURCES } from './pipelinesCrudConfig'
 
export function PipelineDetailPage() {
  const nav = useNavigate()
  const { id } = useParams<{ id: string }>()
  const repos = useRepositoryOptions()
  const { data: pipeline } = useEntityDetail('pipelines', id ?? '')
 
  const pageConfig: PageConfig = {
    title: pipeline?.name ?? 'Pipeline',
    breadcrumbs: [
      { label: 'Dashboard', href: '/' },
      { label: 'Projects', href: '/projects' },
      ...(pipeline
        ? [{ label: String(pipeline.project_name ?? ''), href: `/projects/${pipeline.project_id}` }]
        : []),
      { label: pipeline?.name ?? id ?? '' },
    ],
  }
 
  const detailConfig: DetailConfig = {
    entityPath: 'pipelines',
    title: 'Pipeline',
    actionTargets: { edit: `/pipelines/${id}/edit`, delete: true },
    onDeleteSuccess: () => {
      // Wireframe: "Delete → useDeletePipeline() → navigate(/projects/{projectId})"
      // The project_id is not known until the entity is loaded; the underlying
      // delete flow happens via L2W coordinator. To preserve cross-feature
      // navigation, we use the entity data resolved in fieldFormatters below.
    },
    readGrouping: [
      {
        label: 'Basic',
        fields: ['name', 'description'],
      },
      {
        label: 'Configuration',
        fields: [
          'output_branch',
          'manifest_path',
          'enabled',
          'code_repo_id',
          'manifest_repo_id',
        ],
      },
      {
        label: 'Timestamps',
        fields: ['created_at', 'updated_at'],
      },
    ],
    fieldFormatters: {
      description: (value: unknown) => (value ? String(value) : '\u2014'),
      output_branch: (value: unknown) => <code>{String(value ?? '')}</code>,
      manifest_path: (value: unknown) => <code>{String(value ?? '')}</code>,
      enabled: (value: unknown) => (value ? '✅' : '❌'),
      code_repo_id: (value: unknown) => (
        <span data-testid="pipeline-code-repo" className="text-sm">
          {repos.byIdFallback(value)}
        </span>
      ),
      manifest_repo_id: (value: unknown) => (
        <span data-testid="pipeline-manifest-repo" className="text-sm">
          {repos.byIdFallback(value)}
        </span>
      ),
      created_at: (value: unknown) => formatDateTime(value as string | null | undefined),
      updated_at: (value: unknown) => formatDateTime(value as string | null | undefined),
    },
  }
 
  const runsListConfig: ListConfig = {
    entityPath: 'runs',
    prefix: 'plruns',
    columns: [
      {
        field: 'phase',
        label: 'Phase',
        render: (row) => (
          <RunPhaseIcon phase={((row.phase ?? 'pending') as 'pending' | 'running' | 'succeeded' | 'failed')} />
        ),
      },
      {
        field: 'source_type',
        label: 'Source',
        render: (row) => (
          <SourceBadge source={((row.source_type ?? 'manual') as 'manual' | 'webhook' | 'api' | 'schedule')} />
        ),
      },
      {
        field: 'created_at',
        label: 'Created',
        render: (row) => formatRelativeTime(row.created_at as string | null | undefined),
      },
      {
        field: 'tokens',
        label: 'Tokens',
        render: (row) => {
          const input = row.token_input as number | undefined
          const output = row.token_output as number | undefined
          const total = (input ?? 0) + (output ?? 0)
          return total ? abbreviateNumber(total) : '\u2014'
        },
      },
      {
        field: 'duration',
        label: 'Duration',
        render: (row) => {
          const started = row.started_at as string | undefined
          const completed = row.completed_at as string | undefined
          const phase = row.phase as string | undefined
          if (phase === 'running') {
            return <span className="text-sm text-blue-500">running</span>
          }
          const ms = started && completed ? new Date(completed).getTime() - new Date(started).getTime() : null
          if (ms == null) return '\u2014'
          const totalSec = Math.floor(ms / 1000)
          if (totalSec < 60) return `${totalSec}s`
          const m = Math.floor(totalSec / 60)
          const s = totalSec % 60
          return s > 0 ? `${m}m ${s}s` : `${m}m`
        },
      },
    ],
    sort: { field: 'created_at', order: 'desc' },
    fixedParams: { pipeline_id: id ?? '', limit: 10 },
    sse: { enabled: true },
    rowTargetOverride: (row) => nav(`/runs/${row.run_id as string}`),
  }

  const crudConfig: CrudConfig = {
    entityPath: 'pipelines',
    fieldSet: PIPELINE_FIELD_SET,
    fieldTypes: PIPELINE_FIELD_TYPES,
    entitySelectSources: PIPELINE_ENTITY_SELECT_SOURCES,
    successTarget: `/pipelines/${id}`,
    cancelTarget: `/pipelines/${id}`,
    title: 'Edit Pipeline',
  }
 
  return (
    <Page config={pageConfig}>
      <EntityDetail
        config={detailConfig}
        extra={<RunNowButton pipelineId={id ?? ''} />}
      />
      <DataTable
        config={runsListConfig}
        extra={
          <button
            type="button"
            className="inline-flex items-center rounded-md border border-input bg-background px-3 py-1 text-sm hover:bg-accent"
            onClick={() => nav(`/runs?runs_pipeline_id=${id}&runs_sort=created_at&runs_order=desc`)}
     I       data-testid="view-all-runs"
          >
            View All Runs →
          </button>
        }
      />
      <Crud config={crudConfig} />
      <Outlet />
    </Page>
  )
}