"use client"
import React, { useEffect } from 'react'
interface ScreenshotGalleryProps {
screenshots: string[]
symbol: string
timeframes: string[]
enlargedImage: string | null
onImageClick: (src: string) => void
onClose: () => void
}
export default function ScreenshotGallery({
screenshots,
symbol,
timeframes,
enlargedImage,
onImageClick,
onClose
}: ScreenshotGalleryProps) {
// Debug logging
console.log('ScreenshotGallery props:', {
screenshots,
symbol,
timeframes,
enlargedImage,
screenshotsLength: screenshots?.length
})
// Handle ESC key to close enlarged image
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape' && enlargedImage) {
onClose()
}
}
if (enlargedImage) {
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}
}, [enlargedImage, onClose])
if (screenshots.length === 0) {
console.log('ScreenshotGallery: No screenshots to display')
return null
}
// Helper function to format screenshot URL
const formatScreenshotUrl = (screenshot: string) => {
// Extract just the filename from the full path
const filename = screenshot.split('/').pop() || screenshot
// Return the Next.js API route format
return `/screenshots/${filename}`
}
return (
<>
{/* Gallery Grid */}
📸
Chart Screenshots
{screenshots.length} captured • Click to enlarge
{screenshots.map((screenshot, index) => {
const filename = screenshot.split('/').pop() || ''
// Extract timeframe from filename (e.g., SOLUSD_5_ai_timestamp.png -> "5m")
const extractTimeframeFromFilename = (filename: string) => {
const match = filename.match(/_(\d+|D)_/)
if (!match) return 'Unknown'
const tf = match[1]
if (tf === 'D') return '1D'
if (tf === '5') return '5m'
if (tf === '15') return '15m'
if (tf === '60') return '1h'
if (tf === '240') return '4h'
return `${tf}m`
}
const timeframe = timeframes[index] || extractTimeframeFromFilename(filename)
const imageUrl = formatScreenshotUrl(screenshot)
console.log(`Screenshot ${index}:`, { screenshot, filename, timeframe, imageUrl })
return (
onImageClick(imageUrl)}
>
{/* Preview Image */}

console.log(`Image loaded successfully: ${imageUrl}`)}
onError={(e: any) => {
console.error(`Failed to load image: ${imageUrl}`, e)
const target = e.target as HTMLImageElement
target.style.display = 'none'
const fallback = target.nextElementSibling as HTMLElement
if (fallback) fallback.classList.remove('hidden')
}}
/>
📊
Chart Preview
{filename}
{/* Overlay */}
{/* Image Info */}
{symbol}
{timeframe} Timeframe
Click to view
)
})}
{/* Enlarged Image Modal */}
{enlargedImage && (
e.stopPropagation()}>
{/* Close Button */}
{/* Image */}

{
console.error('Failed to load enlarged image:', enlargedImage)
const target = e.target as HTMLImageElement
target.alt = 'Failed to load image'
}}
/>
{/* Image Info Overlay */}
{symbol} Chart Analysis
AI analyzed screenshot • High resolution view
ESC to close • Click outside to close
)}
>
)
}