import React, { useRef, useState, useEffect } from 'react'; import { SubsonicAlbum } from '../api/subsonic'; import AlbumCard from './AlbumCard'; import { ChevronLeft, ChevronRight, ArrowRight } from 'lucide-react'; import { NavLink, useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; interface Props { title: string; titleLink?: string; albums: SubsonicAlbum[]; moreLink?: string; moreText?: string; onLoadMore?: () => Promise; showRating?: boolean; } export default function AlbumRow({ title, titleLink, albums, moreLink, moreText, onLoadMore, showRating }: Props) { const { t } = useTranslation(); const scrollRef = useRef(null); const navigate = useNavigate(); const [showLeft, setShowLeft] = useState(false); const [showRight, setShowRight] = useState(true); const [loadingMore, setLoadingMore] = useState(false); const loadingRef = useRef(false); const handleScroll = () => { if (!scrollRef.current) return; const { scrollLeft, scrollWidth, clientWidth } = scrollRef.current; setShowLeft(scrollLeft > 0); setShowRight(scrollLeft < scrollWidth - clientWidth - 5); // Auto-load trigger if (onLoadMore && !loadingRef.current && scrollLeft > 0 && scrollLeft + clientWidth >= scrollWidth - 300) { triggerLoadMore(); } }; const triggerLoadMore = async () => { if (!onLoadMore || loadingRef.current) return; loadingRef.current = true; setLoadingMore(true); await onLoadMore(); setLoadingMore(false); loadingRef.current = false; }; useEffect(() => { handleScroll(); window.addEventListener('resize', handleScroll); return () => window.removeEventListener('resize', handleScroll); }, [albums]); const scroll = (dir: 'left' | 'right') => { if (!scrollRef.current) return; const amount = scrollRef.current.clientWidth * 0.75; scrollRef.current.scrollBy({ left: dir === 'left' ? -amount : amount, behavior: 'smooth' }); }; if (albums.length === 0) return null; return (
{titleLink ? ( {title} ) : (

{title}

)}
{albums.map(a => )} {loadingMore && (
{t('common.loadingMore')}
)} {!loadingMore && moreLink && (
navigate(moreLink)}>
{moreText}
)}
); }