import React, { useState, useEffect, useCallback } from 'react' import { X, ChevronLeft, ChevronRight, Edit2, Trash2, Check } from 'lucide-react' import { useTranslation } from '../../i18n' export function PhotoLightbox({ photos, initialIndex, onClose, onUpdate, onDelete, days, places, tripId }) { const { t } = useTranslation() const [index, setIndex] = useState(initialIndex || 0) const [editCaption, setEditCaption] = useState(false) const [caption, setCaption] = useState('') const [isSaving, setIsSaving] = useState(false) const photo = photos[index] useEffect(() => { setIndex(initialIndex || 0) }, [initialIndex]) useEffect(() => { if (photo) setCaption(photo.caption || '') }, [photo]) const prev = useCallback(() => { setIndex(i => Math.max(0, i - 1)) setEditCaption(false) }, []) const next = useCallback(() => { setIndex(i => Math.min(photos.length - 1, i + 1)) setEditCaption(false) }, [photos.length]) useEffect(() => { const handleKey = (e) => { if (e.key === 'Escape') onClose() if (e.key === 'ArrowLeft') prev() if (e.key === 'ArrowRight') next() } window.addEventListener('keydown', handleKey) return () => window.removeEventListener('keydown', handleKey) }, [onClose, prev, next]) const handleSaveCaption = async () => { setIsSaving(true) try { await onUpdate(photo.id, { caption }) setEditCaption(false) } finally { setIsSaving(false) } } const handleDelete = async () => { if (!confirm('Foto löschen?')) return await onDelete(photo.id) if (photos.length <= 1) { onClose() } else { setIndex(i => Math.min(i, photos.length - 2)) } } if (!photo) return null const day = days?.find(d => d.id === photo.day_id) const place = places?.find(p => p.id === photo.place_id) return (
setEditCaption(true)} > {photo.caption || Beschriftung hinzufügen...}
> )}