"use client" import { useLocale, useTranslations } from "next-intl" import { useEffect, useState, useCallback } from "react" import InfiniteScroll from "react-infinite-scroll-component" import HeroCarousel from "./Carousel" import CategoryGrid from "./CategoryGrid" import CollectionSection from "./ProductGrid" import { useCategories, useCarousels, useCollections } from "@/lib/hooks" import type { Collection } from "@/lib/types/api" export default function HomePage() { const locale = useLocale() const t = useTranslations("common") const [mounted, setMounted] = useState(false) const [visibleCollections, setVisibleCollections] = useState([]) const [hasMore, setHasMore] = useState(true) const itemsPerPage = 10 const { data: categories, isLoading: categoriesLoading, isError: categoriesError } = useCategories() const { data: carousels, isLoading: carouselsLoading } = useCarousels() const { data: collections, isLoading: collectionsLoading, isError: collectionsError } = useCollections() useEffect(() => setMounted(true), []) // Initialize visible collections when data first loads useEffect(() => { console.log("=== Collections Data Change ===") console.log("Collections:", collections) console.log("Collections length:", collections?.length) console.log("Visible collections length:", visibleCollections.length) if (collections && collections.length > 0 && visibleCollections.length === 0) { console.log("🟢 Initializing first batch of collections") const initial = collections.slice(0, itemsPerPage) console.log("Initial collections to show:", initial.length) setVisibleCollections(initial) setHasMore(collections.length > itemsPerPage) console.log("Has more after init:", collections.length > itemsPerPage) } }, [collections, visibleCollections.length]) const loadMoreCollections = useCallback(() => { console.log("=== loadMoreCollections Called ===") console.log("Collections available:", collections?.length) console.log("Visible collections:", visibleCollections.length) console.log("Has more:", hasMore) if (!collections) { console.log("❌ No collections data") return } const currentLength = visibleCollections.length const nextCollections = collections.slice( currentLength, currentLength + itemsPerPage ) console.log("Current length:", currentLength) console.log("Next batch size:", nextCollections.length) console.log("Next batch:", nextCollections.map(c => c.id)) if (nextCollections.length > 0) { console.log("🟢 Adding", nextCollections.length, "more collections") setVisibleCollections((prev) => { const updated = [...prev, ...nextCollections] console.log("Updated visible collections count:", updated.length) return updated }) } else { console.log("⚠️ No more collections to load") } // Check if we've loaded all collections const newTotal = currentLength + nextCollections.length const shouldHaveMore = newTotal < collections.length console.log("New total:", newTotal, "/ Total available:", collections.length) console.log("Should have more:", shouldHaveMore) if (!shouldHaveMore) { console.log("🔴 Setting hasMore to false") setHasMore(false) } }, [collections, visibleCollections.length, itemsPerPage, hasMore]) useEffect(() => { console.log("=== State Update ===") console.log("Visible collections count:", visibleCollections.length) console.log("Has more:", hasMore) }, [visibleCollections.length, hasMore]) if (!mounted) return
Loading...
// Transform carousel data to match component props const carouselItems = carousels?.map(carousel => ({ title: carousel.title || "", image: carousel.image || carousel.thumbnail, url: carousel.link || null })) || [] console.log("=== Render ===") console.log("Collections loading:", collectionsLoading) console.log("Visible collections for render:", visibleCollections.length) console.log("Has more for InfiniteScroll:", hasMore) return (
{/* Hero Carousel with API data */} {!carouselsLoading && carouselItems.length > 0 && ( )} {/* Categories Grid */} {/* Collections Sections with Infinite Scroll */} {collectionsError ? (

Failed to load collections. Please try again.

) : collectionsLoading ? (
{Array.from({ length: 3 }).map((_, i) => (
{Array.from({ length: 4 }).map((_, j) => (
))}
))}
) : ( <>
Debug Info:
Total Collections: {collections?.length || 0}
Visible: {visibleCollections.length}
Has More: {hasMore ? "Yes" : "No"}

Loading more collections...

} endMessage={

✓ You've reached the end

} scrollThreshold={0.8} >
{visibleCollections.map((collection, index) => (
Collection #{index + 1}
))}
)}
) }