retest / src /components /gallery-masonry.tsx
veltrixcode's picture
Upload 75 files
ebde876 verified
Raw History Blame Contribute Delete
7.38 kB
"use client"
import { useState } from "react"
import Link from "next/link"
import Image from "next/image"
import { Button } from "@/components/ui/button"
import { GalleryModal } from "@/components/gallery-modal"
import { Search } from "lucide-react"
interface GalleryMasonryProps {
generations: any[]
className?: string
enableInfiniteScroll?: boolean
}
export function GalleryMasonry({ generations, className, enableInfiniteScroll = true }: GalleryMasonryProps) {
const [selectedGen, setSelectedGen] = useState<any | null>(null)
const [displayGenerations, setDisplayGenerations] = useState<any[]>(generations)
const [cursor, setCursor] = useState<string | null>(generations.length > 0 ? generations[generations.length - 1].id : null)
const [isLoading, setIsLoading] = useState(false)
const [hasMore, setHasMore] = useState(enableInfiniteScroll)
const loadMore = async () => {
if (!enableInfiniteScroll || !cursor || isLoading || !hasMore) return
setIsLoading(true)
try {
const res = await fetch(`/api/gallery?cursor=${cursor}&limit=20`)
if (!res.ok) throw new Error("Failed to fetch")
const data = await res.json()
if (data.data.length === 0) {
setHasMore(false)
} else {
setDisplayGenerations(prev => [...prev, ...data.data])
setCursor(data.nextCursor)
if (!data.nextCursor) setHasMore(false)
}
} catch (error) {
console.error("Error loading more:", error)
} finally {
setIsLoading(false)
}
}
return (
<>
{/* Masonry Layout using Columns */}
<div className={className || "columns-1 sm:columns-2 lg:columns-3 xl:columns-4 gap-4 space-y-4"}>
{displayGenerations.map((gen) => (
<div
key={gen.id}
className="break-inside-avoid group relative neo-brutalist bg-card rounded-none overflow-hidden border-2 border-border cursor-pointer mb-4"
onClick={() => setSelectedGen(gen)}
>
{/* Username Overlay */}
<div className="absolute top-3 left-3 z-20 opacity-0 group-hover:opacity-100 transition-opacity duration-300">
<div className="bg-black/70 backdrop-blur-md text-white px-3 py-1 text-xs font-bold uppercase tracking-wider rounded-full border border-white/20 flex items-center gap-2 shadow-lg">
<div className="h-2 w-2 rounded-full bg-green-500 animate-pulse" />
@{gen.user?.username || "user"}
</div>
</div>
<div className="relative w-full">
<Image
src={gen.imageUrl}
alt={gen.prompt}
width={gen.width || 1024}
height={gen.height || 1024}
className="w-full h-auto object-cover transition-transform duration-700 group-hover:scale-105"
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
unoptimized
/>
{/* Hover Overlay */}
<div className="absolute inset-0 bg-background/20 backdrop-blur-[2px] opacity-0 group-hover:opacity-100 transition-all duration-300 flex flex-col justify-end p-4 border-2 border-transparent group-hover:border-primary/50">
<div className="bg-background/90 p-3 border-2 border-border neo-brutalist transform translate-y-4 group-hover:translate-y-0 transition-transform duration-300">
<p className="font-bold line-clamp-2 text-xs md:text-sm text-foreground leading-tight">
"{gen.prompt}"
</p>
</div>
</div>
</div>
</div>
))}
</div>
{/* Loading / End of List Indicators */}
<div className="py-8 flex justify-center w-full">
{isLoading && (
<div className="flex items-center gap-2 text-primary font-bold uppercase tracking-widest animate-pulse">
<div className="h-2 w-2 bg-primary rounded-full animate-bounce" style={{ animationDelay: "0ms" }} />
<div className="h-2 w-2 bg-primary rounded-full animate-bounce" style={{ animationDelay: "150ms" }} />
<div className="h-2 w-2 bg-primary rounded-full animate-bounce" style={{ animationDelay: "300ms" }} />
Loading More...
</div>
)}
{!isLoading && hasMore && (
<button
onClick={loadMore}
className="text-sm text-muted-foreground hover:text-primary uppercase tracking-widest font-bold border-b-2 border-transparent hover:border-primary transition-colors"
>
Load More Art
</button>
)}
{!hasMore && displayGenerations.length > 0 && (
<p className="text-muted-foreground text-sm font-bold uppercase tracking-widest">
You've reached the end
</p>
)}
</div>
{/* Intersection Element for auto-loading */}
{hasMore && !isLoading && (
<div ref={(node) => {
if (node) {
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
loadMore();
}
},
{ threshold: 0.1, rootMargin: "200px" }
);
observer.observe(node);
// Cleanup happens automatically when node unmounts usually, but for strictness we rely on React lifecycle if we used useEffect.
// Since this is a ref callback, it's slightly different. A useEffect is safer.
}
}} className="h-4 w-full" />
)}
{generations.length === 0 && (
<div className="glass-panel p-12 rounded-none border-2 border-dashed border-border text-center">
<p className="text-xl font-bold text-muted-foreground uppercase tracking-widest">No Masterpieces Yet</p>
<div className="mt-6">
<Button className="neo-brutalist" asChild><Link href="/create">Be The First</Link></Button>
</div>
</div>
)}
<GalleryModal
isOpen={!!selectedGen}
onClose={() => setSelectedGen(null)}
generation={selectedGen}
/>
</>
)
}