import React from 'react'; import { useLocation, useNavigate, useParams } from 'react-router-dom'; import { Dialog, DialogContent } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Heart } from 'lucide-react'; import { likePhoto } from '../services/photosApi'; type Photo = { id: number; file_path?: string; thumbnail_path?: string; likes_count?: number; created_at?: string }; export default function PhotoLightbox() { const nav = useNavigate(); const { state } = useLocation(); const { photoId } = useParams(); const [photo, setPhoto] = React.useState((state as any)?.photo ?? null); const [likes, setLikes] = React.useState(null); const [liked, setLiked] = React.useState(false); React.useEffect(() => { if (photo) return; (async () => { const res = await fetch(`/api/v1/photos/${photoId}`); if (res.ok) setPhoto(await res.json()); })(); }, [photo, photoId]); React.useEffect(() => { if (photo && likes === null) setLikes(photo.likes_count ?? 0); }, [photo, likes]); async function onLike() { if (liked || !photo) return; setLiked(true); const c = await likePhoto(photo.id); setLikes(c); } function onOpenChange(open: boolean) { if (!open) nav(-1); } return (
{photo ? ( Foto ) : (
Lade…
)}
); }