const { useState, useEffect, useCallback, useRef } = React; const FlipPriceItem = ({ metal, name, unit, sellPrice, buyPrice, showBuy, isFlipping, delay = 0 }) => { const formatPrice = (price) => price ? `$${Math.round(price).toLocaleString()}` : '—'; const currentLabel = showBuy ? '回收' : '賣出'; const currentLabelClass = showBuy ? 'buy' : 'sell'; const currentPrice = showBuy ? buyPrice : sellPrice; const currentPriceClass = showBuy ? 'buy' : metal; const nextLabel = showBuy ? '賣出' : '回收'; const nextLabelClass = showBuy ? 'sell' : 'buy'; const nextPrice = showBuy ? sellPrice : buyPrice; const nextPriceClass = showBuy ? metal : 'buy'; const cardClass = ['flip-card', isFlipping ? 'flipping' : ''].filter(Boolean).join(' '); return (
{name} / {unit}
{currentLabel}
{nextLabel}
{formatPrice(currentPrice)}
{formatPrice(nextPrice)}
); }; const PriceBanner = ({ prices }) => { const { user, handleLogin, authReady } = useApp(); const [showBuyPrice, setShowBuyPrice] = useState(false); const [isFlipping, setIsFlipping] = useState(false); const flipTimeoutRef = useRef(null); const autoFlipRef = useRef(null); const handleFlip = useCallback(() => { if (isFlipping) return; setIsFlipping(true); flipTimeoutRef.current = setTimeout(() => { setShowBuyPrice(prev => !prev); setIsFlipping(false); }, 700); if (autoFlipRef.current) { clearInterval(autoFlipRef.current); autoFlipRef.current = setInterval(() => { handleFlip(); }, 5000); } }, [isFlipping]); useEffect(() => { if (!user || !prices?.has_prices) return; autoFlipRef.current = setInterval(() => { handleFlip(); }, 5000); return () => { if (autoFlipRef.current) clearInterval(autoFlipRef.current); }; }, [user, prices?.has_prices, handleFlip]); useEffect(() => { return () => { if (flipTimeoutRef.current) clearTimeout(flipTimeoutRef.current); if (autoFlipRef.current) clearInterval(autoFlipRef.current); }; }, []); if (!prices?.has_prices) return null; const p = prices.prices || {}; if (!user) { return (
登入查看即時報價
); } return (
); };