import React, { useCallback, useEffect, useRef, useState } from 'react' import LevelBadge from '../../components/xp/LevelBadge' function ScrollBtn({ direction, onClick }) { const isLeft = direction === 'left' return ( {isLeft ? : } ) } export default function TrendingArtworks() { const [items, setItems] = useState([]) const [loading, setLoading] = useState(true) const scrollRef = useRef(null) const touchStartRef = useRef({ x: 0, y: 0 }) const draggedRef = useRef(false) const suppressClickRef = useRef(false) const suppressTimerRef = useRef(null) useEffect(() => { let cancelled = false async function load() { try { const response = await window.axios.get('/api/dashboard/trending-artworks') if (!cancelled) { setItems(Array.isArray(response.data?.data) ? response.data.data : []) } } finally { if (!cancelled) { setLoading(false) } } } load() return () => { cancelled = true } }, []) const scroll = useCallback((dir) => { const el = scrollRef.current if (!el) return el.scrollBy({ left: dir === 'left' ? -320 : 320, behavior: 'smooth' }) }, []) const onTouchStart = useCallback((e) => { if (!e.touches?.length) return touchStartRef.current = { x: e.touches[0].clientX, y: e.touches[0].clientY } draggedRef.current = false }, []) const onTouchMove = useCallback((e) => { if (!e.touches?.length) return const dx = Math.abs(e.touches[0].clientX - touchStartRef.current.x) const dy = Math.abs(e.touches[0].clientY - touchStartRef.current.y) if (dx > 10 && dx > dy) draggedRef.current = true }, []) const onTouchEnd = useCallback(() => { if (!draggedRef.current) return suppressClickRef.current = true clearTimeout(suppressTimerRef.current) suppressTimerRef.current = setTimeout(() => { suppressClickRef.current = false }, 260) }, []) const onClickCapture = useCallback((e) => { if (!suppressClickRef.current) return const link = e.target?.closest?.('a') if (link) { e.preventDefault(); e.stopPropagation() } }, []) return ( Discover Trending Artworks Explore more {loading ? ( {[...Array(4)].map((_, i) => ( ))} ) : null} {!loading && items.length === 0 ? ( No trending artworks available. ) : null} {!loading && items.length > 0 ? ( scroll('left')} /> scroll('right')} /> {items.map((item) => ( {item.title} {item.creator ? ( <> {item.creator.username ? `@${item.creator.username}` : item.creator.name} > ) : null} ))} ) : null} ) }
Discover
No trending artworks available.
{item.title}
{item.creator.username ? `@${item.creator.username}` : item.creator.name}