All files / features / runs RunsPage.tsx

60.93% Statements 39/64
43.75% Branches 14/32
40.9% Functions 9/22
64.4% Lines 38/59

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                      69x   69x   66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x                                   66x             66x 66x 66x 66x                                                       90x                     66x                                                                             1188x 396x 396x                                                                                           1x      
import { useState, useRef } from 'react'
import { useNavigate, useSearchParams } from 'react-router-dom'
import { Play } from 'lucide-react'
import { useRunFiltersAndData } from './hooks/useRunFiltersAndData'
import { useRunColumns } from './hooks/useRunColumns'
import { useProjects } from '@/features/projects/projectsApi'
import { CrudModal } from '@/shared/components/CrudModal'
import { DataTable } from '@/shared/components/DataTable'
import { Page, PageHeader } from '@/shared/components/Page'
import { Button } from '@/components/ui/button'
import { RunForm } from '@/features/runs/components/RunForm'
import type { components } from '@/types/api'
import type { CrudFormRef } from '@/features/runs/components/RunForm'
 
type Run = components['schemas']['Run']
 
export function RunsPage() {
  const [searchParams, setSearchParams] = useSearchParams()
  const nav = useNavigate()
 
  const { data, isLoading, error, isRefetching, refetch, filterProps } = useRunFiltersAndData({ prefix: 'runs' })
  const { filterValues, onFilterChange, onFiltersClear, sortKey, sortOrder, onSort } = filterProps
  const columns = useRunColumns()
  const { data: projectsData } = useProjects()
 
  const [formDirty, setFormDirty] = useState(false)
  const [formPending, setFormPending] = useState(false)
  const formRef = useRef<CrudFormRef>(null)
 
  const projects = projectsData?.data ?? []
 
  const sourceTypeOptions = [
    { value: 'manual', label: 'Manual' },
    { value: 'webhook', label: 'Webhook' },
    { value: 'api', label: 'API' },
    { value: 'schedule', label: 'Schedule' },
  ] as const
  const sourceMetaOptions = [
    { value: 'Vikunja', label: 'Vikunja' },
    { value: 'Jira', label: 'Jira' },
  ] as const
  const allSourceOptions = [...sourceTypeOptions, ...sourceMetaOptions]
  const sourceTypeSelected = (filterValues.source_type ?? '').split('|').filter(Boolean)
  const sourceSelected = (filterValues.source ?? '').split('|').filter(Boolean)
 
  const filterDefs = [
    {
      key: 'phase' as const,
      label: 'Phase',
      type: 'multi-select' as const,
      options: [
        { value: 'pending', label: 'Pending' },
        { value: 'running', label: 'Running' },
        { value: 'succeeded', label: 'Succeeded' },
        { value: 'failed', label: 'Failed' },
      ],
    },
    {
      key: 'project_id' as const,
      label: 'Project',
      type: 'multi-select' as const,
      options: projects.map((p) => ({ value: p.id, label: p.name })),
    },
    {
      key: 'created_after' as const,
      label: 'Date Range',
      type: 'date-range' as const,
    },
  ]
 
  return (
    <Page>
      <PageHeader
        title="Runs"
        breadcrumbs={[
          { label: 'Dashboard', href: '/' },
          { label: 'Runs' },
        ]}
        actions={
          <Button
            onClick={() =>
              setSearchParams((prev) => {
                prev.set('create', 'true')
                return prev
              })
            }
          >
            <Play className="mr-2 h-4 w-4" />
            Trigger New Run
          </Button>
        }
      />
      {/* Custom unified Source filter */}
      <div className="flex flex-col gap-1">
        <label className="text-xs text-muted-foreground">Source</label>
        <div className="flex flex-wrap gap-1">
          {allSourceOptions.map((opt) => {
            const isSourceType = sourceTypeOptions.some((o) => o.value === opt.value)
            const active = isSourceType
              ? sourceTypeSelected.includes(opt.value)
              : sourceSelected.includes(opt.value)
            return (
              <button
                key={opt.value}
                id={`filter-source-${opt.value}`}
                aria-label={`Source: ${opt.label}`}
                type="button"
                onClick={() => {
                  if (isSourceType) {
                    const next = active
                      ? sourceTypeSelected.filter((s) => s !== opt.value)
                      : [...sourceTypeSelected, opt.value]
                    onFilterChange('source_type', next.length > 0 ? next : undefined)
                  } else {
                    const next = active
                      ? sourceSelected.filter((s) => s !== opt.value)
                      : [...sourceSelected, opt.value]
                    onFilterChange('source', next.length > 0 ? next : undefined)
                  }
                }}
                className={`rounded-md border px-2.5 py-1 text-xs font-medium transition-colors ${
                  active
                    ? 'border-primary bg-primary/10 text-primary'
                    : 'border-border bg-background text-muted-foreground hover:bg-accent'
                }`}
              >
                {opt.label}
              </button>
            )
          })}
        </div>
      </div>
      <DataTable<Run>
        prefix="runs"
        columns={columns}
        data={data}
        isLoading={isLoading}
        error={error}
        entityName="Run"
        sse={{ enabled: true }}
        filters={filterDefs}
        filterValues={filterValues}
        onFilterChange={onFilterChange}
        onFiltersClear={onFiltersClear}
        sortKey={sortKey}
        sortOrder={sortOrder}
        onSort={onSort}
        onRowClick={(item) => nav(`/runs/${item.run_id}`)}
        onRetry={() => refetch()}
        isRefreshing={isRefetching}
      />
      <CrudModal
        open={searchParams.get('create') === 'true'}
        title="Trigger New Run"
        saveLabel="Trigger Run"
        isDirty={formDirty}
        isPending={formPending}
        onClose={() =>
          setSearchParams((prev) => {
            prev.delete('create')
            return prev
          })
        }
        onSave={() => formRef.current?.submit()}
      >
        <RunForm
          ref={formRef}
          onSuccess={() => {
            setSearchParams({})
          }}
          onClose={() =>
            setSearchParams((prev) => {
              prev.delete('create')
              return prev
            })
          }
          onDirtyChange={setFormDirty}
          onPendingChange={setFormPending}
        />
      </CrudModal>
    </Page>
  )
}