import { useState, useEffect } from 'react'; import { Camera, Upload, LogIn, Settings } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { useAuth } from '@/hooks/useAuth'; import { useNavigate } from 'react-router-dom'; import { UppyPhotoSubmissionUpload } from '@/components/upload/UppyPhotoSubmissionUpload'; import { PhotoManagementDialog } from '@/components/upload/PhotoManagementDialog'; import { supabase } from '@/integrations/supabase/client'; import { EntityPhotoGalleryProps } from '@/types/submissions'; import { useUserRole } from '@/hooks/useUserRole'; interface Photo { id: string; url: string; caption?: string; title?: string; user_id: string; created_at: string; } export function EntityPhotoGallery({ entityId, entityType, entityName, parentId }: EntityPhotoGalleryProps) { const { user } = useAuth(); const navigate = useNavigate(); const { isModerator } = useUserRole(); const [photos, setPhotos] = useState([]); const [showUpload, setShowUpload] = useState(false); const [showManagement, setShowManagement] = useState(false); const [loading, setLoading] = useState(true); useEffect(() => { fetchPhotos(); }, [entityId, entityType]); const fetchPhotos = async () => { try { // Fetch photos directly from the photos table const { data: photoData, error } = await supabase .from('photos') .select('id, cloudflare_image_url, title, caption, submitted_by, created_at, order_index') .eq('entity_type', entityType) .eq('entity_id', entityId) .order('order_index', { ascending: true }) .order('created_at', { ascending: false }); if (error) throw error; // Map to Photo interface const mappedPhotos: Photo[] = photoData?.map((photo) => ({ id: photo.id, url: photo.cloudflare_image_url, caption: photo.caption || undefined, title: photo.title || undefined, user_id: photo.submitted_by, created_at: photo.created_at, })) || []; setPhotos(mappedPhotos); } catch (error) { console.error('Error fetching photos:', error); } finally { setLoading(false); } }; const handleUploadClick = () => { if (!user) { navigate('/auth'); return; } setShowUpload(true); }; const handleSubmissionComplete = () => { setShowUpload(false); fetchPhotos(); // Refresh photos after submission }; if (showUpload) { return (

Upload Photos for {entityName}

); } if (loading) { return (
Loading photos...
); } return (
{/* Header with Upload and Management Buttons */}

Photo Gallery

Share your photos of {entityName}

{isModerator && photos.length > 0 && ( )}
{/* Photo Management Dialog */} {/* Photo Grid */} {photos.length > 0 ? (
{photos.map((photo) => ( {photo.title {(photo.title || photo.caption) && (
{photo.title && (

{photo.title}

)} {photo.caption && (

{photo.caption}

)}
)}
))}
) : (

No Photos Yet

Be the first to share photos of {entityName}!

)}
); }