Spaces:
Running
Running
Create src/app/page.tsx
Browse files- src/app/page.tsx +35 -0
src/app/page.tsx
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// src/app/page.tsx (Página principal do seu e-commerce)
|
| 2 |
+
|
| 3 |
+
import { useGetProducts } from '@/hooks/useGetProducts';
|
| 4 |
+
import { ProductCard } from '@/components/ProductCard';
|
| 5 |
+
|
| 6 |
+
export default function Home() {
|
| 7 |
+
// 1. Chamamos o "Garçom Inteligente" (TanStack Query)
|
| 8 |
+
const { data: products, isLoading, isError, error } = useGetProducts();
|
| 9 |
+
|
| 10 |
+
// 2. ⚡ [PERFORMANCE/INP]: Tratamento de estados da UI (early returns)
|
| 11 |
+
if (isLoading) {
|
| 12 |
+
// Exibimos um skeleton loading ou um spinner enquanto espera.
|
| 13 |
+
// Isso é crucial para o LCP (Largest Contentful Paint).
|
| 14 |
+
return <p className="text-center mt-8 text-lg text-gray-500">Carregando produtos...</p>;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
if (isError) {
|
| 18 |
+
// Tratamento de erro robusto. O erro é pego pelo Zod ou pela rede.
|
| 19 |
+
// Exibe a mensagem de erro para debug, mas em produção, exiba algo amigável.
|
| 20 |
+
return <p className="text-center mt-8 text-lg text-red-600">Erro ao carregar os produtos: {error.message}</p>;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// 3. Renderiza a lista de produtos (Vitrine)
|
| 24 |
+
return (
|
| 25 |
+
<main className="container mx-auto p-4">
|
| 26 |
+
<h1 className="text-3xl font-bold mb-6">Nossos Produtos</h1>
|
| 27 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
| 28 |
+
{products?.map((product) => (
|
| 29 |
+
// O key é importante para performance do React.
|
| 30 |
+
<ProductCard key={product.id} product={product} />
|
| 31 |
+
))}
|
| 32 |
+
</div>
|
| 33 |
+
</main>
|
| 34 |
+
);
|
| 35 |
+
}
|