"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { useFamily } from "@/app/FamilyProvider"; interface SavedOutfit { id: string; name: string; garment_ids: string[]; occasion_tags: string[]; created_at: string; } interface GarmentThumb { id: string; thumbUrl: string; name: string | null; category: string; } export default function SavedOutfitsPage() { const { childId } = useFamily(); const router = useRouter(); const [outfits, setOutfits] = useState([]); const [thumbs, setThumbs] = useState>({}); const [loading, setLoading] = useState(true); useEffect(() => { if (!childId) return; (async () => { try { const res = await fetch(`/api/garments/outfits?childId=${childId}`); const data = await res.json(); setOutfits(data.items || []); // Fetch thumbnails for garment IDs referenced by outfits const allIds = [...new Set((data.items || []).flatMap((o: SavedOutfit) => o.garment_ids))] as string[]; if (allIds.length > 0) { const garmentRes = await fetch(`/api/garments?childId=${childId}&status=active`); const garmentData = await garmentRes.json(); const map: Record = {}; for (const g of garmentData.items || []) { map[g.id] = { id: g.id, thumbUrl: g.thumbUrl, name: g.name, category: g.category }; } setThumbs(map); } } catch {} setLoading(false); })(); }, [childId]); const deleteOutfit = async (id: string) => { try { const res = await fetch(`/api/garments/outfits/${id}`, { method: "DELETE" }); if (res.ok) setOutfits(prev => prev.filter(o => o.id !== id)); } catch {} }; return (

💾 Saved Outfits

+ New
{loading ? (
{[0, 150, 300].map(d => ( ))}
) : outfits.length === 0 ? (
👗

No saved outfits yet

Save combinations from the outfit suggestion screen

Get suggestions
) : (
{outfits.map(outfit => (

{outfit.name}

{outfit.occasion_tags?.length > 0 && (
{outfit.occasion_tags.map(t => ( {t} ))}
)}
{outfit.garment_ids.slice(0, 4).map(gid => { const g = thumbs[gid]; if (!g) return (
👗
); return (
{g.name
); })} {outfit.garment_ids.length > 4 && (
+{outfit.garment_ids.length - 4}
)}

{new Date(outfit.created_at).toLocaleDateString("en-IN", { day: "numeric", month: "short", year: "numeric" })}

))}
)}
); }