import React, { useState, useMemo } from 'react' import { PhotoLightbox } from './PhotoLightbox' import { PhotoUpload } from './PhotoUpload' import { Upload, Camera } from 'lucide-react' import Modal from '../shared/Modal' export default function PhotoGallery({ photos, onUpload, onDelete, onUpdate, places, days, tripId }) { const [lightboxIndex, setLightboxIndex] = useState(null) const [showUpload, setShowUpload] = useState(false) const [filterDayId, setFilterDayId] = useState('') const filteredPhotos = useMemo(() => { return photos.filter(photo => { if (filterDayId && String(photo.day_id) !== String(filterDayId)) return false return true }) }, [photos, filterDayId]) const handlePhotoClick = (photo) => { const idx = filteredPhotos.findIndex(p => p.id === photo.id) setLightboxIndex(idx) } const handleDelete = async (photoId) => { await onDelete(photoId) if (lightboxIndex !== null) { const newPhotos = filteredPhotos.filter(p => p.id !== photoId) if (newPhotos.length === 0) { setLightboxIndex(null) } else if (lightboxIndex >= newPhotos.length) { setLightboxIndex(newPhotos.length - 1) } } } return (
{/* Header */}

Fotos

{photos.length} Foto{photos.length !== 1 ? 's' : ''}

{filterDayId && ( )}
{/* Gallery Grid */}
{filteredPhotos.length === 0 ? (

Noch keine Fotos

Lade deine Reisefotos hoch

) : (
{filteredPhotos.map(photo => ( handlePhotoClick(photo)} /> ))} {/* Upload tile */}
)}
{/* Lightbox */} {lightboxIndex !== null && ( setLightboxIndex(null)} onUpdate={onUpdate} onDelete={handleDelete} days={days} places={places} tripId={tripId} /> )} {/* Upload Modal */} setShowUpload(false)} title="Fotos hochladen" size="lg" > { await onUpload(formData) setShowUpload(false) }} onClose={() => setShowUpload(false)} />
) } function PhotoThumbnail({ photo, days, places, onClick }) { const day = days?.find(d => d.id === photo.day_id) const place = places?.find(p => p.id === photo.place_id) return (
{photo.caption { e.target.style.display = 'none' e.target.nextSibling && (e.target.nextSibling.style.display = 'flex') }} /> {/* Fallback */}
🖼️
{/* Hover overlay */}
{photo.caption && (

{photo.caption}

)} {(day || place) && (

{day ? `Tag ${day.day_number}` : ''}{day && place ? ' · ' : ''}{place?.name || ''}

)}
) } function formatDate(dateStr) { if (!dateStr) return '' return new Date(dateStr + 'T00:00:00').toLocaleDateString('de-DE', { day: 'numeric', month: 'short' }) }