File size: 7,381 Bytes
ebde876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"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}
            />
        </>
    )
}