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

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import { css } from '../../../../../styled-system/css'

type ModelType = 'column-classifier' | 'boundary-detector'

interface SyncHistoryEntry {
  id: string
  status: 'success' | 'failed' | 'cancelled'
  startedAt: string
  completedAt: string | null
  durationMs: number | null
  filesTransferred: number
  tombstonePruned: number | null
  error: string | null
}

interface SyncHistoryIndicatorProps {
  modelType: ModelType
  /** Trigger a refresh (e.g., increment after sync completes) */
  refreshTrigger?: number
}

/**
 * Compact sync history indicator
 *
 * Shows "Last sync: X ago" with expandable dropdown showing recent syncs.
 * Unobtrusive by default, informative on interaction.
 */
export function SyncHistoryIndicator({ modelType, refreshTrigger = 0 }: SyncHistoryIndicatorProps) {
  const [history, setHistory] = useState<SyncHistoryEntry[]>([])
  const [loading, setLoading] = useState(true)
  const [expanded, setExpanded] = useState(false)
  const containerRef = useRef<HTMLDivElement>(null)

  // Fetch history
  const fetchHistory = useCallback(async () => {
    try {
      const response = await fetch(
        `/api/vision-training/sync/history?modelType=${modelType}&limit=5`
      )
      if (response.ok) {
        const data = await response.json()
        setHistory(data.history || [])
      }
    } catch (error) {
      console.error('[SyncHistoryIndicator] Failed to fetch history:', error)
    } finally {
      setLoading(false)
    }
  }, [modelType])

  // Fetch on mount and when refreshTrigger changes
  useEffect(() => {
    fetchHistory()
  }, [fetchHistory, refreshTrigger])

  // Close dropdown when clicking outside
  useEffect(() => {
    if (!expanded) return

    const handleClickOutside = (event: MouseEvent) => {
      if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
        setExpanded(false)
      }
    }

    document.addEventListener('mousedown', handleClickOutside)
    return () => document.removeEventListener('mousedown', handleClickOutside)
  }, [expanded])

  // Don't render anything while loading or if no history
  if (loading || history.length === 0) {
    return null
  }

  const lastSync = history[0]
  const lastSyncTime = new Date(lastSync.startedAt)
  const timeAgo = formatTimeAgo(lastSyncTime)
  const isSuccess = lastSync.status === 'success'

  return (
    <div
      ref={containerRef}
      data-component="sync-history-indicator"
      className={css({ position: 'relative' })}
    >
      {/* Compact indicator - clickable */}
      <button
        type="button"
        onClick={() => setExpanded(!expanded)}
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: 1,
          px: 2,
          py: 1,
          bg: 'transparent',
          border: 'none',
          borderRadius: 'md',
          cursor: 'pointer',
          fontSize: 'xs',
          color: isSuccess ? 'gray.500' : 'orange.400',
          _hover: { bg: 'gray.800' },
          transition: 'background 0.15s',
        })}
      >
        <span>{isSuccess ? '✓' : '⚠'}</span>
        <span>{isSuccess ? `Last sync: ${timeAgo}` : `Sync failed ${timeAgo}`}</span>
        <span
          className={css({
            fontSize: '10px',
            opacity: 0.6,
            transform: expanded ? 'rotate(180deg)' : 'rotate(0deg)',
            transition: 'transform 0.15s',
          })}
        >

        </span>
      </button>

      {/* Expanded dropdown */}
      {expanded && (
        <div
          data-element="history-dropdown"
          className={css({
            position: 'absolute',
            top: '100%',
            right: 0,
            mt: 1,
            minWidth: '280px',
            bg: 'gray.850',
            border: '1px solid',
            borderColor: 'gray.700',
            borderRadius: 'lg',
            boxShadow: 'lg',
            zIndex: 50,
            overflow: 'hidden',
          })}
        >
          {/* Header */}
          <div
            className={css({
              px: 3,
              py: 2,
              borderBottom: '1px solid',
              borderColor: 'gray.700',
              fontSize: 'xs',
              fontWeight: 'medium',
              color: 'gray.400',
              textTransform: 'uppercase',
              letterSpacing: '0.05em',
            })}
          >
            Recent Syncs
          </div>

          {/* History entries */}
          <div className={css({ py: 1 })}>
            {history.map((entry) => (
              <SyncHistoryRow key={entry.id} entry={entry} />
            ))}
          </div>
        </div>
      )}
    </div>
  )
}

/**
 * Single row in the sync history dropdown
 */
function SyncHistoryRow({ entry }: { entry: SyncHistoryEntry }) {
  const time = new Date(entry.startedAt)
  const isSuccess = entry.status === 'success'
  const isFailed = entry.status === 'failed'

  return (
    <div
      className={css({
        display: 'flex',
        alignItems: 'center',
        gap: 2,
        px: 3,
        py: 2,
        fontSize: 'sm',
        _hover: { bg: 'gray.800' },
      })}
    >
      {/* Status icon */}
      <span
        className={css({
          flexShrink: 0,
          width: '16px',
          textAlign: 'center',
          color: isSuccess ? 'green.400' : isFailed ? 'red.400' : 'gray.500',
        })}
      >
        {isSuccess ? '✓' : isFailed ? '✗' : '○'}
      </span>

      {/* Time */}
      <span
        className={css({
          flexShrink: 0,
          width: '80px',
          color: 'gray.400',
          fontSize: 'xs',
        })}
        title={time.toLocaleString()}
      >
        {formatTimeAgo(time)}
      </span>

      {/* Details */}
      <span
        className={css({
          flex: 1,
          color: isFailed ? 'red.300' : 'gray.300',
          overflow: 'hidden',
          textOverflow: 'ellipsis',
          whiteSpace: 'nowrap',
        })}
        title={isFailed && entry.error ? entry.error : undefined}
      >
        {isFailed ? (
          truncateError(entry.error || 'Unknown error')
        ) : (
          <>
            {entry.filesTransferred} file
            {entry.filesTransferred !== 1 ? 's' : ''}
            {entry.durationMs !== null && (
              <span className={css({ color: 'gray.500', ml: 1 })}>
                · {formatDuration(entry.durationMs)}
              </span>
            )}
          </>
        )}
      </span>
    </div>
  )
}

/**
 * Format a date as relative time (e.g., "2h ago", "Yesterday")
 */
function formatTimeAgo(date: Date): string {
  const now = new Date()
  const diffMs = now.getTime() - date.getTime()
  const diffSecs = Math.floor(diffMs / 1000)
  const diffMins = Math.floor(diffSecs / 60)
  const diffHours = Math.floor(diffMins / 60)
  const diffDays = Math.floor(diffHours / 24)

  if (diffSecs < 60) return 'Just now'
  if (diffMins < 60) return `${diffMins}m ago`
  if (diffHours < 24) return `${diffHours}h ago`
  if (diffDays === 1) return 'Yesterday'
  if (diffDays < 7) return `${diffDays}d ago`
  return date.toLocaleDateString()
}

/**
 * Format duration in milliseconds to human-readable string
 */
function formatDuration(ms: number): string {
  if (ms < 1000) return `${ms}ms`
  const secs = ms / 1000
  if (secs < 60) return `${secs.toFixed(1)}s`
  const mins = Math.floor(secs / 60)
  const remainingSecs = Math.round(secs % 60)
  return `${mins}m ${remainingSecs}s`
}

/**
 * Truncate error message for display
 */
function truncateError(error: string): string {
  // Extract key part of common errors
  if (error.includes('Cannot connect')) return 'Connection failed'
  if (error.includes('SSH')) return 'SSH error'
  if (error.includes('rsync')) return 'Sync error'
  if (error.includes('timeout')) return 'Timeout'

  // Generic truncation
  if (error.length > 30) return error.substring(0, 27) + '...'
  return error
}