All files / web/src/components DownloadCard.tsx

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

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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
'use client'

import { Download, ExternalLink, FileText, Globe, Image, Sparkles } from 'lucide-react'
import { useState } from 'react'
import { css } from '../../styled-system/css'
import { hstack, stack } from '../../styled-system/patterns'

interface GenerationResult {
  id: string
  downloadUrl: string
  metadata: {
    cardCount: number
    numbers: number[]
    format: string
    filename: string
    fileSize: number
  }
}

interface DownloadCardProps {
  result: GenerationResult
  onNewGeneration: () => void
}

export function DownloadCard({ result, onNewGeneration }: DownloadCardProps) {
  const [isDownloading, setIsDownloading] = useState(false)

  const handleDownload = async () => {
    setIsDownloading(true)

    try {
      // Trigger download
      const link = document.createElement('a')
      link.href = result.downloadUrl
      link.download = result.metadata.filename
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)

      // Optional: Track download success
      console.log('📥 Download started:', result.metadata.filename)
    } catch (error) {
      console.error('Download failed:', error)
    } finally {
      setIsDownloading(false)
    }
  }

  const handlePreview = () => {
    window.open(result.downloadUrl, '_blank')
  }

  const formatFileSize = (bytes: number): string => {
    if (bytes === 0) return '0 B'
    const k = 1024
    const sizes = ['B', 'KB', 'MB', 'GB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return `${Math.round((bytes / k ** i) * 100) / 100} ${sizes[i]}`
  }

  const getFormatIcon = (format: string) => {
    switch (format.toLowerCase()) {
      case 'pdf':
        return <FileText size={20} />
      case 'html':
        return <Globe size={20} />
      case 'svg':
      case 'png':
        return <Image size={20} />
      default:
        return <FileText size={20} />
    }
  }

  const getFormatColor = (format: string) => {
    switch (format.toLowerCase()) {
      case 'pdf':
        return 'red'
      case 'html':
        return 'blue'
      case 'svg':
        return 'purple'
      case 'png':
        return 'green'
      default:
        return 'gray'
    }
  }

  const formatColor = getFormatColor(result.metadata.format)

  return (
    <div
      className={css({
        bg: 'gradient-to-r from-green.50 to-emerald.50',
        border: '2px solid',
        borderColor: 'green.200',
        rounded: '2xl',
        p: '8',
        position: 'relative',
        overflow: 'hidden',
      })}
    >
      {/* Success decoration */}
      <div
        className={css({
          position: 'absolute',
          top: '-20px',
          right: '-20px',
          w: '40px',
          h: '40px',
          bg: 'green.100',
          rounded: 'full',
          opacity: 0.6,
        })}
      />

      <div className={stack({ gap: '6' })}>
        {/* Header */}
        <div className={hstack({ gap: '4', alignItems: 'center' })}>
          <div
            className={css({
              w: '12',
              h: '12',
              bg: 'green.100',
              rounded: 'full',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
            })}
          >
            <Sparkles size={24} className={css({ color: 'green.600' })} />
          </div>

          <div className={stack({ gap: '1', flex: 1 })}>
            <h3
              className={css({
                fontSize: 'xl',
                fontWeight: 'bold',
                color: 'gray.900',
              })}
            >
              ✨ Your Flashcards Are Ready!
            </h3>
            <p
              className={css({
                color: 'gray.600',
              })}
            >
              Generated {result.metadata.cardCount} beautiful soroban flashcards
            </p>
          </div>
        </div>

        {/* File Details */}
        <div
          className={css({
            p: '4',
            bg: 'white',
            rounded: 'xl',
            border: '1px solid',
            borderColor: 'gray.200',
          })}
        >
          <div className={hstack({ gap: '3', alignItems: 'center', mb: '3' })}>
            <div
              className={css({
                p: '2',
                bg: `${formatColor}.100`,
                color: `${formatColor}.600`,
                rounded: 'lg',
              })}
            >
              {getFormatIcon(result.metadata.format)}
            </div>

            <div className={stack({ gap: '0', flex: 1 })}>
              <div
                className={css({
                  fontSize: 'sm',
                  fontWeight: 'semibold',
                  color: 'gray.900',
                  fontFamily: 'mono',
                })}
              >
                {result.metadata.filename}
              </div>
              <div
                className={css({
                  fontSize: 'xs',
                  color: 'gray.600',
                })}
              >
                {result.metadata.format.toUpperCase()} • {formatFileSize(result.metadata.fileSize)}
              </div>
            </div>

            <div
              className={css({
                px: '3',
                py: '1',
                bg: `${formatColor}.100`,
                color: `${formatColor}.800`,
                fontSize: 'xs',
                fontWeight: 'medium',
                rounded: 'full',
              })}
            >
              {result.metadata.cardCount} cards
            </div>
          </div>

          {/* Sample Numbers */}
          <div className={stack({ gap: '2' })}>
            <div
              className={css({
                fontSize: 'xs',
                fontWeight: 'medium',
                color: 'gray.700',
              })}
            >
              Sample numbers:
            </div>
            <div
              className={css({
                display: 'flex',
                flexWrap: 'wrap',
                gap: '1',
              })}
            >
              {result.metadata.numbers.slice(0, 8).map((number, i) => (
                <span
                  key={i}
                  className={css({
                    px: '2',
                    py: '1',
                    bg: 'gray.100',
                    color: 'gray.700',
                    fontSize: 'xs',
                    fontFamily: 'mono',
                    rounded: 'md',
                  })}
                >
                  {number}
                </span>
              ))}
              {result.metadata.numbers.length > 8 && (
                <span
                  className={css({
                    px: '2',
                    py: '1',
                    color: 'gray.500',
                    fontSize: 'xs',
                  })}
                >
                  +{result.metadata.numbers.length - 8} more
                </span>
              )}
            </div>
          </div>
        </div>

        {/* Action Buttons */}
        <div className={hstack({ gap: '3' })}>
          <button
            onClick={handleDownload}
            disabled={isDownloading}
            className={css({
              flex: 1,
              px: '6',
              py: '4',
              bg: 'green.600',
              color: 'white',
              fontSize: 'lg',
              fontWeight: 'semibold',
              rounded: 'xl',
              shadow: 'card',
              transition: 'all',
              cursor: isDownloading ? 'not-allowed' : 'pointer',
              opacity: isDownloading ? '0.7' : '1',
              _hover: isDownloading
                ? {}
                : {
                    bg: 'green.700',
                    transform: 'translateY(-1px)',
                    shadow: 'modal',
                  },
            })}
          >
            <span className={hstack({ gap: '3', justify: 'center' })}>
              {isDownloading ? (
                <>
                  <div
                    className={css({
                      w: '5',
                      h: '5',
                      border: '2px solid',
                      borderColor: 'white',
                      borderTopColor: 'transparent',
                      rounded: 'full',
                      animation: 'spin 1s linear infinite',
                    })}
                  />
                  Downloading...
                </>
              ) : (
                <>
                  <Download size={20} />
                  Download
                </>
              )}
            </span>
          </button>

          {result.metadata.format === 'html' && (
            <button
              onClick={handlePreview}
              className={css({
                px: '4',
                py: '4',
                bg: 'white',
                color: 'blue.600',
                fontSize: 'lg',
                fontWeight: 'semibold',
                rounded: 'xl',
                shadow: 'card',
                border: '2px solid',
                borderColor: 'blue.200',
                transition: 'all',
                _hover: {
                  borderColor: 'blue.400',
                  transform: 'translateY(-1px)',
                },
              })}
            >
              <ExternalLink size={20} />
            </button>
          )}
        </div>

        {/* New Generation Button */}
        <div
          className={css({
            pt: '4',
            borderTop: '1px solid',
            borderColor: 'green.200',
          })}
        >
          <button
            onClick={onNewGeneration}
            className={css({
              w: 'full',
              px: '4',
              py: '3',
              bg: 'transparent',
              color: 'green.700',
              fontSize: 'sm',
              fontWeight: 'medium',
              rounded: 'lg',
              border: '1px solid',
              borderColor: 'green.300',
              transition: 'all',
              _hover: {
                bg: 'green.100',
                borderColor: 'green.400',
              },
            })}
          >
            Create Another Set
          </button>
        </div>
      </div>

      {/* Success celebration effect */}
      <div
        className={css({
          position: 'absolute',
          top: '4',
          right: '4',
          fontSize: '2xl',
          opacity: 0.3,
        })}
      >
        🎉
      </div>
    </div>
  )
}