"use client"; import { useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { useLocale } from "@/contexts/LocaleContext"; interface Project { id: string; title: string; category: string; description: string | null; coverImage: string | null; galleryImages: string[]; status: string; client: string | null; completionDate: string | null; } const FALLBACK_IMAGE = "https://images.unsplash.com/photo-1616401784845-180882ba9ba8?q=80&w=2070&auto=format&fit=crop"; export default function ProjetosPage() { const { t, locale } = useLocale(); const prefix = locale === 'pt' ? '' : `/${locale}`; const [projects, setProjects] = useState([]); const [loading, setLoading] = useState(true); const [selectedCategory, setSelectedCategory] = useState('todos'); useEffect(() => { let isMounted = true; const controller = new AbortController(); const fetchProjects = async () => { try { const response = await fetch('/api/projects?status=published', { method: 'GET', cache: 'no-store', credentials: 'same-origin', signal: controller.signal, }); if (!response.ok) { throw new Error(`Falha ao buscar projetos (status ${response.status})`); } const data = await response.json(); if (isMounted && Array.isArray(data)) { setProjects(data); } } catch (err) { if ((err as Error).name !== 'AbortError') { console.error('Erro ao carregar projetos:', err); } } finally { if (isMounted) { setLoading(false); } } }; fetchProjects(); return () => { isMounted = false; controller.abort(); }; }, []); const categories = useMemo(() => { const unique = new Set(); projects.forEach((project) => { if (project.category) { unique.add(project.category); } }); return Array.from(unique); }, [projects]); const filteredProjects = useMemo(() => { if (selectedCategory === 'todos') { return projects; } return projects.filter((project) => project.category === selectedCategory); }, [projects, selectedCategory]); return (
{/* Hero Section */}

{t('projects.hero.title')}

{t('projects.hero.subtitle')}

{/* Projects Grid */}
{/* Filters */}
{categories.map((category) => ( ))}
{loading ? (
{Array.from({ length: 6 }).map((_, index) => (
))}
) : filteredProjects.length === 0 ? (

{locale === 'pt' ? 'Ainda não temos projetos publicados nesta categoria.' : locale === 'es' ? 'Todavía no hay proyectos publicados en esta categoría.' : 'No projects published in this category yet.'}

) : (
{filteredProjects.map((project) => { const image = project.coverImage || project.galleryImages[0] || FALLBACK_IMAGE; const description = project.description || (locale === 'pt' ? 'Descrição disponível em breve.' : locale === 'es' ? 'Descripción disponible pronto.' : 'Description coming soon.'); return (
{project.category || t('projects.filters.all')}

{project.title}

{project.client || (locale === 'pt' ? 'Cliente confidencial' : locale === 'es' ? 'Cliente confidencial' : 'Confidential client')}

{description}

{t('projects.viewDetails')}
); })}
)}
); }