'use client'; import { useState, useEffect } from 'react'; import { useSession } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import Image from 'next/image'; import { Photo, Person } from '@prisma/client'; import { ChevronLeft, ChevronRight, X } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { isUrl, getImageSrc } from '@/lib/photo-utils'; interface PhotoWithDetails extends Photo { faces?: Array<{ person: Person | null; }>; photoTags?: Array<{ tag: { tagName: string; }; }>; } interface PhotoViewerProps { photo: PhotoWithDetails; previousId: number | null; nextId: number | null; } export function PhotoViewer({ photo, previousId, nextId }: PhotoViewerProps) { const router = useRouter(); const [loading, setLoading] = useState(false); const { data: session } = useSession(); const isLoggedIn = Boolean(session); // Keyboard navigation useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'ArrowLeft' && previousId) { navigateToPhoto(previousId); } else if (e.key === 'ArrowRight' && nextId) { navigateToPhoto(nextId); } else if (e.key === 'Escape') { router.back(); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [previousId, nextId, router]); const navigateToPhoto = (photoId: number) => { setLoading(true); router.push(`/photo/${photoId}`); }; const handlePrevious = () => { if (previousId) { navigateToPhoto(previousId); } }; const handleNext = () => { if (nextId) { navigateToPhoto(nextId); } }; const handleClose = () => { // Use router.back() to return to the previous page without reloading // This preserves filters, pagination, and scroll position router.back(); }; const peopleNames = (photo as any).faces ?.map((face: any) => face.Person) .filter((person: any): person is Person => person !== null) .map((person: Person) => `${person.first_name} ${person.last_name}`.trim()) || []; const tags = (photo as any).PhotoTagLinkage?.map((pt: any) => pt.Tag.tag_name) || []; return (
{new Date(photo.date_taken).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', })}
)} {peopleNames.length > 0 && (People: {peopleNames.join(', ')}
)} {tags.length > 0 && (Tags: {tags.join(', ')}
)}