All files / web/src/components/flowchart VersionHistoryPanel.tsx

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

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

import { css } from '../../../styled-system/css'
import { hstack, vstack } from '../../../styled-system/patterns'
import {
  useRestoreVersion,
  useVersionHistory,
  type FlowchartVersion,
} from '../../hooks/useVersionHistory'

interface VersionHistoryPanelProps {
  sessionId: string
  onRestore: () => void
  onPreview: (version: FlowchartVersion | null) => void
  previewingVersion: FlowchartVersion | null
}

export function VersionHistoryPanel({
  sessionId,
  onRestore,
  onPreview,
  previewingVersion,
}: VersionHistoryPanelProps) {
  const { data, isLoading, error } = useVersionHistory(sessionId)

  const restoreMutation = useRestoreVersion(sessionId)

  const versions = data?.versions ?? []
  const currentVersion = data?.currentVersion ?? 0

  // Handle restore
  const handleRestore = async (versionNumber: number) => {
    if (versionNumber === currentVersion) return

    try {
      await restoreMutation.mutateAsync(versionNumber)
      // Clear preview if we were previewing the version we just restored
      onPreview(null)
      // Notify parent to refresh session data
      onRestore()
    } catch (err) {
      console.error('Failed to restore version:', err)
    }
  }

  // Format timestamp
  const formatTime = (date: string | Date) => {
    const d = new Date(date)
    return d.toLocaleTimeString(undefined, {
      hour: '2-digit',
      minute: '2-digit',
    })
  }

  const formatDate = (date: string | Date) => {
    const d = new Date(date)
    const today = new Date()
    const yesterday = new Date(today)
    yesterday.setDate(yesterday.getDate() - 1)

    if (d.toDateString() === today.toDateString()) {
      return 'Today'
    } else if (d.toDateString() === yesterday.toDateString()) {
      return 'Yesterday'
    }
    return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' })
  }

  if (isLoading) {
    return (
      <div className={css({ padding: '4', textAlign: 'center' })}>
        <p className={css({ color: { base: 'gray.500', _dark: 'gray.400' } })}>
          Loading history...
        </p>
      </div>
    )
  }

  if (error) {
    return (
      <div className={css({ padding: '4', textAlign: 'center' })}>
        <p className={css({ color: { base: 'red.600', _dark: 'red.400' } })}>
          {error instanceof Error ? error.message : 'Failed to fetch versions'}
        </p>
      </div>
    )
  }

  if (versions.length === 0) {
    return (
      <div className={css({ padding: '4', textAlign: 'center' })}>
        <p className={css({ color: { base: 'gray.500', _dark: 'gray.400' } })}>
          No version history yet. Generate or refine the flowchart to create versions.
        </p>
      </div>
    )
  }

  return (
    <div
      data-component="version-history-panel"
      className={vstack({ gap: '3', alignItems: 'stretch' })}
    >
      <p
        className={css({
          fontSize: 'sm',
          color: { base: 'gray.600', _dark: 'gray.400' },
        })}
      >
        {versions.length} version{versions.length !== 1 ? 's' : ''} saved
      </p>

      <div className={vstack({ gap: '2', alignItems: 'stretch' })}>
        {versions.map((version) => {
          const isPreviewing = previewingVersion?.versionNumber === version.versionNumber

          return (
            <div
              key={version.id}
              data-element="version-item"
              data-version={version.versionNumber}
              data-current={version.isCurrent}
              data-previewing={isPreviewing}
              className={css({
                padding: '3',
                borderRadius: 'lg',
                border: '2px solid',
                borderColor: isPreviewing
                  ? { base: 'amber.400', _dark: 'amber.500' }
                  : version.isCurrent
                    ? { base: 'blue.300', _dark: 'blue.600' }
                    : { base: 'gray.200', _dark: 'gray.700' },
                backgroundColor: isPreviewing
                  ? { base: 'amber.50', _dark: 'amber.900/30' }
                  : version.isCurrent
                    ? { base: 'blue.50', _dark: 'blue.900/30' }
                    : { base: 'white', _dark: 'gray.800' },
              })}
            >
              <div className={hstack({ gap: '2', justifyContent: 'space-between' })}>
                <div className={hstack({ gap: '2', alignItems: 'center' })}>
                  {/* Version badge */}
                  <span
                    className={css({
                      padding: '0.5 2',
                      borderRadius: 'full',
                      fontSize: 'xs',
                      fontWeight: 'bold',
                      backgroundColor: version.isCurrent
                        ? { base: 'blue.500', _dark: 'blue.400' }
                        : { base: 'gray.200', _dark: 'gray.700' },
                      color: version.isCurrent ? 'white' : { base: 'gray.600', _dark: 'gray.400' },
                    })}
                  >
                    v{version.versionNumber}
                  </span>

                  {/* Source icon */}
                  <span title={version.source === 'generate' ? 'Generated' : 'Refined'}>
                    {version.source === 'generate' ? '✨' : '🔧'}
                  </span>

                  {/* Current indicator */}
                  {version.isCurrent && (
                    <span
                      className={css({
                        fontSize: 'xs',
                        color: { base: 'blue.600', _dark: 'blue.400' },
                        fontWeight: 'medium',
                      })}
                    >
                      Current
                    </span>
                  )}

                  {/* Previewing indicator */}
                  {isPreviewing && (
                    <span
                      className={css({
                        fontSize: 'xs',
                        color: { base: 'amber.600', _dark: 'amber.400' },
                        fontWeight: 'medium',
                      })}
                    >
                      Viewing
                    </span>
                  )}
                </div>

                {/* Action buttons */}
                <div className={hstack({ gap: '1' })}>
                  {/* Preview/Stop Preview button */}
                  <button
                    data-action={isPreviewing ? 'stop-preview' : 'preview-version'}
                    onClick={() => onPreview(isPreviewing ? null : version)}
                    className={css({
                      padding: '1 2',
                      fontSize: 'xs',
                      borderRadius: 'md',
                      backgroundColor: isPreviewing
                        ? { base: 'amber.100', _dark: 'amber.900' }
                        : { base: 'gray.100', _dark: 'gray.700' },
                      color: isPreviewing
                        ? { base: 'amber.700', _dark: 'amber.300' }
                        : { base: 'gray.700', _dark: 'gray.300' },
                      border: 'none',
                      cursor: 'pointer',
                      _hover: {
                        backgroundColor: isPreviewing
                          ? { base: 'amber.200', _dark: 'amber.800' }
                          : { base: 'gray.200', _dark: 'gray.600' },
                      },
                    })}
                  >
                    {isPreviewing ? 'Stop' : 'Preview'}
                  </button>

                  {/* Restore button - only for non-current versions */}
                  {!version.isCurrent && (
                    <button
                      data-action="restore-version"
                      onClick={() => handleRestore(version.versionNumber)}
                      disabled={restoreMutation.isPending}
                      className={css({
                        padding: '1 2',
                        fontSize: 'xs',
                        borderRadius: 'md',
                        backgroundColor: { base: 'blue.100', _dark: 'blue.900' },
                        color: { base: 'blue.700', _dark: 'blue.300' },
                        border: 'none',
                        cursor: 'pointer',
                        _hover: {
                          backgroundColor: { base: 'blue.200', _dark: 'blue.800' },
                        },
                        _disabled: {
                          opacity: 0.5,
                          cursor: 'not-allowed',
                        },
                      })}
                    >
                      {restoreMutation.isPending &&
                      restoreMutation.variables === version.versionNumber
                        ? 'Restoring...'
                        : 'Restore'}
                    </button>
                  )}
                </div>
              </div>

              {/* Source request (truncated) */}
              {version.sourceRequest && (
                <p
                  className={css({
                    marginTop: '2',
                    fontSize: 'sm',
                    color: { base: 'gray.600', _dark: 'gray.400' },
                    overflow: 'hidden',
                    textOverflow: 'ellipsis',
                    lineClamp: 2,
                  })}
                >
                  {version.sourceRequest}
                </p>
              )}

              {/* Validation status and timestamp */}
              <div
                className={hstack({
                  gap: '2',
                  marginTop: '2',
                  justifyContent: 'space-between',
                  flexWrap: 'wrap',
                })}
              >
                <div className={hstack({ gap: '2' })}>
                  {version.validationPassed !== null && (
                    <span
                      className={css({
                        fontSize: 'xs',
                        color: version.validationPassed
                          ? { base: 'green.600', _dark: 'green.400' }
                          : { base: 'red.600', _dark: 'red.400' },
                      })}
                    >
                      {version.validationPassed ? '✓ Valid' : '✗ Invalid'}
                    </span>
                  )}
                  {version.coveragePercent !== null && (
                    <span
                      className={css({
                        fontSize: 'xs',
                        color: { base: 'gray.500', _dark: 'gray.500' },
                      })}
                    >
                      {version.coveragePercent}% coverage
                    </span>
                  )}
                </div>

                <span
                  className={css({
                    fontSize: 'xs',
                    color: { base: 'gray.400', _dark: 'gray.500' },
                  })}
                >
                  {formatDate(version.createdAt)} {formatTime(version.createdAt)}
                </span>
              </div>
            </div>
          )
        })}
      </div>
    </div>
  )
}