import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { useSession } from '@supabase/auth-helpers-react'; import { supabase } from '@/integrations/supabase/client'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'; // Adjust import based on your UI library import { Button } from '@/components/ui/button'; // Adjust import based on your UI library import { Download } from 'lucide-react'; // Icon for download button, adjust as needed import { Skeleton } from '@/components/ui/skeleton'; // Adjust import based on your UI library interface GeneratedImage { id: string; created_at: string; user_id: string | null; prompt: string; image_url: string; } interface MyGeneratedLogoProps { userId?: string; } const MyGeneratedLogo: React.FC = ({ userId }) => { const { t } = useTranslation(); const session = useSession(); const [images, setImages] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [isDownloading, setIsDownloading] = useState(false); useEffect(() => { const fetchImages = async () => { setLoading(true); setError(null); try { const { data, error } = await supabase .from('generated_logos') .select('*') .eq('user_id', userId ?? '') .order('created_at', { ascending: false }); if (error) { console.error('Error fetching images:', error); setError(error.message); } else { setImages(data || []); } } catch (err: any) { console.error('Unexpected error fetching images:', err); setError(err.message); } finally { setLoading(false); } }; if (userId) { fetchImages(); } }, [userId]); const handleDownloadImage = async (imageUrl: string) => { setIsDownloading(true); try { const response = await fetch(imageUrl); const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `generated-image-${Date.now()}.png`; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); } catch (err) { console.error('Error downloading image:', err); } finally { setIsDownloading(false); } }; return ( {t('My Generated Logos')}

{t('View and download your previously generated logos.')}

{error ? (
{error}
) : loading ? (
{[...Array(6)].map((_, i) => ( ))}
) : images.length === 0 ? (
{t('No images created yet.')}
) : (
{images.map((image) => (
{`Generated e.preventDefault()} />
))}
)}
); }; export default MyGeneratedLogo;