All files / web/src/app/vision-training/train/components/wizard PhaseSection.tsx

0% Statements 0/324
0% Branches 0/1
0% Functions 0/1
0% Lines 0/324

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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
'use client'

import { css } from '../../../../../../styled-system/css'
import { CardCarousel } from './CardCarousel'
import { StepProgress } from './StepProgress'
import {
  CARDS,
  type PhaseDefinition,
  type PhaseStatus,
  type ModelType,
  type SamplesData,
  type HardwareInfo,
  type PreflightInfo,
  type TrainingConfig,
  type ServerPhase,
  type EpochData,
  type DatasetInfo,
  type LoadingProgress,
  type TrainingResult,
} from './types'

interface PhaseSectionProps {
  phase: PhaseDefinition
  phaseIndex: number
  status: PhaseStatus
  currentCardIndex: number
  onGoToCard: (phaseIndex: number, cardIndex: number) => void
  // Model type (from URL)
  modelType: ModelType
  // Data
  samples: SamplesData | null
  samplesLoading: boolean
  hardwareInfo: HardwareInfo | null
  hardwareLoading: boolean
  fetchHardware: () => void
  preflightInfo: PreflightInfo | null
  preflightLoading: boolean
  fetchPreflight: () => void
  config: TrainingConfig
  setConfig: (config: TrainingConfig | ((prev: TrainingConfig) => TrainingConfig)) => void
  isGpu: boolean
  // Training
  serverPhase: ServerPhase
  statusMessage: string
  currentEpoch: EpochData | null
  epochHistory: EpochData[]
  bestAccuracy: number
  bestPixelError: number | null
  datasetInfo: DatasetInfo | null
  loadingProgress: LoadingProgress | null
  result: TrainingResult | null
  error: string | null
  // Summaries
  getCardSummary: (cardId: string) => { label: string; value: string } | null
  // Actions
  onProgress: () => void
  onStartTraining: () => void
  onCancel: () => void
  onStopAndSave?: () => void
  onTrainAgain: () => void
  onRerunTraining?: () => void
  onSyncComplete?: () => void
  onDataWarningAcknowledged?: () => void
  canStartTraining: boolean
}

const STATUS_STYLES: Record<PhaseStatus, { borderColor: string; bg: string; opacity: number }> = {
  done: { borderColor: 'green.600', bg: 'gray.850', opacity: 1 },
  current: { borderColor: 'blue.500', bg: 'gray.800', opacity: 1 },
  upcoming: { borderColor: 'gray.700', bg: 'gray.850', opacity: 0.6 },
}

export function PhaseSection({
  phase,
  phaseIndex,
  status,
  currentCardIndex,
  onGoToCard,
  modelType,
  samples,
  samplesLoading,
  hardwareInfo,
  hardwareLoading,
  fetchHardware,
  preflightInfo,
  preflightLoading,
  fetchPreflight,
  config,
  setConfig,
  isGpu,
  serverPhase,
  statusMessage,
  currentEpoch,
  epochHistory,
  bestAccuracy,
  bestPixelError,
  datasetInfo,
  loadingProgress,
  result,
  error,
  getCardSummary,
  onProgress,
  onStartTraining,
  onCancel,
  onStopAndSave,
  onTrainAgain,
  onRerunTraining,
  onSyncComplete,
  onDataWarningAcknowledged,
  canStartTraining,
}: PhaseSectionProps) {
  const styles = STATUS_STYLES[status]

  return (
    <div
      data-element={`phase-${phase.id}`}
      data-status={status}
      className={css({
        borderLeft: '3px solid',
        borderColor: styles.borderColor,
        bg: styles.bg,
        borderRadius: 'lg',
        overflow: 'hidden',
        opacity: styles.opacity,
        transition: 'all 0.3s ease',
      })}
    >
      {/* Phase Header */}
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'space-between',
          p: 3,
          borderBottom: status === 'current' ? '1px solid' : 'none',
          borderColor: 'gray.700',
        })}
      >
        <div className={css({ display: 'flex', alignItems: 'center', gap: 2 })}>
          {/* Status indicator */}
          <div
            className={css({
              width: '20px',
              height: '20px',
              borderRadius: 'full',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              fontSize: 'xs',
              fontWeight: 'bold',
              bg: status === 'done' ? 'green.600' : status === 'current' ? 'blue.600' : 'gray.600',
              color: 'white',
            })}
          >
            {status === 'done' ? '✓' : status === 'current' ? '●' : '○'}
          </div>
          <span
            className={css({
              fontWeight: 'semibold',
              fontSize: 'sm',
              color: status === 'current' ? 'gray.100' : 'gray.400',
            })}
          >
            {phase.title}
          </span>
        </div>

        {/* Status badge */}
        <span
          className={css({
            fontSize: 'xs',
            px: 2,
            py: 0.5,
            borderRadius: 'full',
            bg: status === 'done' ? 'green.800' : status === 'current' ? 'blue.800' : 'gray.700',
            color: status === 'done' ? 'green.300' : status === 'current' ? 'blue.300' : 'gray.500',
          })}
        >
          {status === 'done' ? 'Complete' : status === 'current' ? 'In Progress' : 'Upcoming'}
        </span>
      </div>

      {/* Phase Content */}
      {status === 'current' ? (
        <div className={css({ p: 4 })}>
          {/* Card Carousel - for current phase */}
          <CardCarousel
            cards={phase.cards}
            currentCardIndex={currentCardIndex}
            onCardClick={(cardIndex) => onGoToCard(phaseIndex, cardIndex)}
            // Model type (from URL)
            modelType={modelType}
            // Data
            samples={samples}
            samplesLoading={samplesLoading}
            hardwareInfo={hardwareInfo}
            hardwareLoading={hardwareLoading}
            fetchHardware={fetchHardware}
            preflightInfo={preflightInfo}
            preflightLoading={preflightLoading}
            fetchPreflight={fetchPreflight}
            config={config}
            setConfig={setConfig}
            isGpu={isGpu}
            // Training
            serverPhase={serverPhase}
            statusMessage={statusMessage}
            currentEpoch={currentEpoch}
            epochHistory={epochHistory}
            bestAccuracy={bestAccuracy}
            bestPixelError={bestPixelError}
            datasetInfo={datasetInfo}
            loadingProgress={loadingProgress}
            result={result}
            error={error}
            // Summaries
            getCardSummary={getCardSummary}
            // Actions
            onProgress={onProgress}
            onStartTraining={onStartTraining}
            onCancel={onCancel}
            onStopAndSave={onStopAndSave}
            onTrainAgain={onTrainAgain}
            onRerunTraining={onRerunTraining}
            onSyncComplete={onSyncComplete}
            onDataWarningAcknowledged={onDataWarningAcknowledged}
            canStartTraining={canStartTraining}
          />

          {/* Step Progress - only for multi-card phases */}
          {phase.cards.length > 1 && (
            <StepProgress
              steps={phase.cards.map((cardId) => CARDS[cardId].title)}
              currentIndex={currentCardIndex}
            />
          )}
        </div>
      ) : (
        /* Collapsed summary for done/upcoming */
        <div className={css({ px: 4, py: 2 })}>
          {status === 'done' ? (
            <div
              className={css({
                display: 'flex',
                gap: 3,
                flexWrap: 'wrap',
                cursor: 'pointer',
              })}
              onClick={() => onGoToCard(phaseIndex, phase.cards.length - 1)}
              onKeyDown={(e) => {
                if (e.key === 'Enter' || e.key === ' ') {
                  onGoToCard(phaseIndex, phase.cards.length - 1)
                }
              }}
              role="button"
              tabIndex={0}
              title={`Go back to ${phase.title}`}
            >
              {phase.cards.map((cardId, cardIndex) => {
                const summary = getCardSummary(cardId)
                const cardDef = CARDS[cardId]
                return (
                  <div
                    key={cardId}
                    onClick={(e) => {
                      e.stopPropagation()
                      onGoToCard(phaseIndex, cardIndex)
                    }}
                    onKeyDown={(e) => {
                      if (e.key === 'Enter' || e.key === ' ') {
                        e.stopPropagation()
                        onGoToCard(phaseIndex, cardIndex)
                      }
                    }}
                    role="button"
                    tabIndex={0}
                    title={`Go back to ${cardDef.title}`}
                    className={css({
                      display: 'flex',
                      alignItems: 'center',
                      gap: 1.5,
                      px: 2,
                      py: 1,
                      bg: 'gray.800',
                      borderRadius: 'md',
                      fontSize: 'xs',
                      cursor: 'pointer',
                      transition: 'all 0.15s ease',
                      _hover: {
                        bg: 'gray.700',
                        transform: 'scale(1.02)',
                      },
                    })}
                  >
                    <span>{cardDef.icon}</span>
                    <span className={css({ color: 'gray.400' })}>{cardDef.title}:</span>
                    <span
                      className={css({
                        color: 'green.400',
                        fontWeight: 'medium',
                      })}
                    >
                      {summary?.value || '✓'}
                    </span>
                  </div>
                )
              })}
            </div>
          ) : (
            <div
              className={css({
                fontSize: 'xs',
                color: 'gray.500',
                fontStyle: 'italic',
              })}
            >
              {phase.id === 'results' ? 'Waiting for training to complete...' : 'Waiting...'}
            </div>
          )}
        </div>
      )}
    </div>
  )
}