All files / features / pipelines PipelineDetailPage.tsx

86.56% Statements 58/67
72.13% Branches 44/61
82.6% Functions 19/23
90.32% Lines 56/62

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                    67x   67x   94x 94x 94x 94x 94x 94x 94x                                   94x                                                                   72x 72x         72x 72x                 72x                   72x 72x               66x     132x                                                         72x 72x     94x             800x                 800x                 800x           800x 800x 800x 800x             800x 800x 800x 800x 108x    
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 { usePipelineConfigSchema } from './usePipelineConfigSchema'
import { useEntityDetail } from '@/layers/L1/entityResolver'
import { PIPELINE_FIELD_SET, PIPELINE_FIELD_TYPES, PIPELINE_ENTITY_SELECT_SOURCES, PIPELINE_FIELD_LINTS } from './pipelinesCrudConfig'
 
export function PipelineDetailPage() {
  const nav = useNavigate()
  const { id } = useParams<{ id: string }>()
  const repos = useRepositoryOptions()
  const { data: pipeline } = useEntityDetail('pipelines', id ?? '')
  const { help: configSchemaHelp } = usePipelineConfigSchema()

  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',
          'enabled',
          'code_repo_id',
          'manifest_repo_id',
          'skill_repos',
        ],
      },
      {
        label: 'Timestamps',
        fields: ['created_at', 'updated_at'],
      },
    ],
    fieldFormatters: {
      description: (value: unknown) => (value ? String(value) : '\u2014'),
      output_branch: (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>
      ),
      skill_repos: (value: unknown) => {
        const skillRepos = Array.isArray(value) ? value as Array<{ name: string; url: string }> : []
        if (skillRepos.length === 0) return <span data-testid="pipeline-skill-repo-refs">{'\u2014'}</span>
        return (
          <ul data-testid="pipeline-skill-repo-refs" className="flex flex-col gap-1">
            {skillRepos.map((repo) => (
              <li key={repo.name} className="text-sm">
                <span className="font-mono font-semibold text-muted-foreground">{repo.name}:</span>{' '}
                <span>{repo.url}</span>
              </li>
            ))}
          </ul>
        )
      },
      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' | 'api' | 'schedule' | 'vikunja' | 'gitea' | 'github')} />
        ),
      },
      {
        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,
    fieldLints: PIPELINE_FIELD_LINTS,
    fieldHelp: configSchemaHelp ? { config: configSchemaHelp } : undefined,
    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 ?? ''} />}
      />
      <h2 className="mb-4 text-lg font-semibold">Latest runs</h2>
      <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`)}
            data-testid="view-all-runs"
          >
            View All Runs →
          </button>
        }
      />
      <Crud config={crudConfig} />
      <Outlet />
    </Page>
  )
}