import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { ArrowLeft, Copy, Check, Image as ImageIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useToast } from "@/hooks/use-toast"; import { supabase } from "@/integrations/supabase/client"; import { User } from "@supabase/supabase-js"; interface Generation { id: string; image_url: string; alt_text: string; created_at: string; } export default function History() { const [user, setUser] = useState(null); const [generations, setGenerations] = useState([]); const [isLoading, setIsLoading] = useState(true); const [copiedId, setCopiedId] = useState(null); const navigate = useNavigate(); const { toast } = useToast(); useEffect(() => { supabase.auth.getSession().then(({ data: { session } }) => { if (session?.user) { setUser(session.user); fetchGenerations(session.user.id); } else { navigate("/auth"); } }); const { data: { subscription } } = supabase.auth.onAuthStateChange((event, session) => { if (session?.user) { setUser(session.user); fetchGenerations(session.user.id); } else { navigate("/auth"); } }); return () => subscription.unsubscribe(); }, [navigate]); const fetchGenerations = async (userId: string) => { setIsLoading(true); try { const { data, error } = await supabase .from('alt_text_generations') .select('*') .eq('user_id', userId) .order('created_at', { ascending: false }); if (error) throw error; setGenerations(data || []); } catch (error) { console.error('Error fetching generations:', error); toast({ title: "Error", description: "Failed to load your history", variant: "destructive", }); } finally { setIsLoading(false); } }; const copyToClipboard = async (text: string, id: string) => { try { await navigator.clipboard.writeText(text); setCopiedId(id); toast({ title: "Copied!", description: "Alt text copied to clipboard", }); setTimeout(() => setCopiedId(null), 2000); } catch (error) { toast({ title: "Error", description: "Failed to copy to clipboard", variant: "destructive", }); } }; if (!user) return null; return (
{/* Header */}

Your History

View all your generated alt texts. You have {generations.length} generations.

{isLoading ? (

Loading your history...

) : generations.length === 0 ? (

No generations yet

Upload your first image to get started

) : (
{generations.map((generation) => (
{generation.alt_text}

{new Date(generation.created_at).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' })}

Alt Text:

{generation.alt_text}

))}
)}
); }