|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { AnimatePresence, motion } from "motion/react"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | + |
| 6 | +import { LikeButton } from "@/components/likes/LikeButton"; |
| 7 | +import { PlaceholderShader } from "@/components/ui/PlaceholderShader"; |
| 8 | +import type { GoodWebsiteItem } from "@/lib/goodWebsites"; |
| 9 | +import { useLikes } from "@/lib/hooks/useLikes"; |
| 10 | +import { imageCache } from "@/lib/imageCache"; |
| 11 | + |
| 12 | +interface GoodWebsiteGalleryItemProps { |
| 13 | + item: GoodWebsiteItem; |
| 14 | +} |
| 15 | + |
| 16 | +const hoverAnimationProps = { |
| 17 | + initial: { opacity: 0, y: 4, scale: 0.96 }, |
| 18 | + animate: { opacity: 1, y: 0, scale: 1 }, |
| 19 | + exit: { opacity: 0, y: 4, scale: 0.96 }, |
| 20 | + transition: { duration: 0.15, ease: "easeOut" as const }, |
| 21 | +}; |
| 22 | + |
| 23 | +export function GoodWebsiteGalleryItem({ item }: GoodWebsiteGalleryItemProps) { |
| 24 | + // Check if image is already cached (from previous view or browser cache) |
| 25 | + const isCached = item.previewImage ? imageCache.has(item.previewImage) : false; |
| 26 | + const [imageStatus, setImageStatus] = useState<"loading" | "loaded" | "error">( |
| 27 | + isCached ? "loaded" : "loading", |
| 28 | + ); |
| 29 | + const [isHovered, setIsHovered] = useState(false); |
| 30 | + const { hasLiked } = useLikes(item.id); |
| 31 | + |
| 32 | + // Preload image using Image API to avoid broken image flicker |
| 33 | + useEffect(() => { |
| 34 | + if (!item.previewImage || isCached) return; |
| 35 | + |
| 36 | + const img = new Image(); |
| 37 | + img.onload = () => { |
| 38 | + imageCache.add(item.previewImage!); |
| 39 | + setImageStatus("loaded"); |
| 40 | + }; |
| 41 | + img.onerror = () => setImageStatus("error"); |
| 42 | + img.src = item.previewImage; |
| 43 | + |
| 44 | + return () => { |
| 45 | + img.onload = null; |
| 46 | + img.onerror = null; |
| 47 | + }; |
| 48 | + }, [item.previewImage, isCached]); |
| 49 | + |
| 50 | + const handleClick = () => { |
| 51 | + if (item.url?.trim()) window.open(item.url, "_blank", "noopener,noreferrer"); |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <div |
| 56 | + className="group rounded-px relative cursor-pointer overflow-hidden" |
| 57 | + onClick={handleClick} |
| 58 | + onMouseEnter={() => setIsHovered(true)} |
| 59 | + onMouseLeave={() => setIsHovered(false)} |
| 60 | + > |
| 61 | + {/* Thumbnail */} |
| 62 | + <div className="bg-tertiary relative aspect-40/21 w-full overflow-hidden"> |
| 63 | + {item.previewImage && imageStatus !== "error" ? ( |
| 64 | + <> |
| 65 | + {imageStatus === "loading" && <PlaceholderShader />} |
| 66 | + {imageStatus === "loaded" && ( |
| 67 | + <picture className="absolute inset-0 transition-transform duration-300 group-hover:scale-[1.02]"> |
| 68 | + {item.previewImageDark && ( |
| 69 | + <source srcSet={item.previewImageDark} media="(prefers-color-scheme: dark)" /> |
| 70 | + )} |
| 71 | + <img |
| 72 | + src={item.previewImage} |
| 73 | + alt={`Preview of ${item.name}`} |
| 74 | + className="h-full w-full object-cover object-top" |
| 75 | + /> |
| 76 | + </picture> |
| 77 | + )} |
| 78 | + </> |
| 79 | + ) : ( |
| 80 | + <PlaceholderShader /> |
| 81 | + )} |
| 82 | + |
| 83 | + {/* Site name pill - always visible on mobile, animated on desktop */} |
| 84 | + <div className="pointer-events-none absolute inset-0 flex flex-col justify-end p-4"> |
| 85 | + {/* Mobile: always visible, no animation */} |
| 86 | + <div className="flex h-7 min-w-0 items-center self-start rounded-full bg-black/50 px-2.5 saturate-150 backdrop-blur-3xl sm:hidden"> |
| 87 | + <span className="truncate text-sm font-medium text-white">{item.name}</span> |
| 88 | + </div> |
| 89 | + {/* Desktop: animated on hover */} |
| 90 | + <AnimatePresence> |
| 91 | + {isHovered && ( |
| 92 | + <motion.div |
| 93 | + className="pointer-events-auto hidden h-7 min-w-0 items-center self-start rounded-full bg-black/50 px-2.5 saturate-150 backdrop-blur-3xl hover:bg-black/90 sm:flex" |
| 94 | + {...hoverAnimationProps} |
| 95 | + > |
| 96 | + <span className="truncate text-sm font-medium text-white">{item.name}</span> |
| 97 | + </motion.div> |
| 98 | + )} |
| 99 | + </AnimatePresence> |
| 100 | + </div> |
| 101 | + |
| 102 | + {/* Like button - always visible on mobile, or when liked, otherwise animated on hover */} |
| 103 | + <div className="absolute right-4 bottom-4" onClick={(e) => e.stopPropagation()}> |
| 104 | + {/* Mobile: always visible */} |
| 105 | + <div className="sm:hidden"> |
| 106 | + <LikeButton pageId={item.id} variant="ghost-light" /> |
| 107 | + </div> |
| 108 | + {/* Desktop: permanently visible if liked, otherwise animated on hover */} |
| 109 | + <div className="hidden sm:block"> |
| 110 | + {hasLiked ? ( |
| 111 | + <LikeButton pageId={item.id} variant="ghost-light" /> |
| 112 | + ) : ( |
| 113 | + <AnimatePresence> |
| 114 | + {isHovered && ( |
| 115 | + <motion.div {...hoverAnimationProps}> |
| 116 | + <LikeButton pageId={item.id} variant="ghost-light" /> |
| 117 | + </motion.div> |
| 118 | + )} |
| 119 | + </AnimatePresence> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
0 commit comments