All files / features/live/pages LiveEmbedPage.tsx

92.5% Statements 37/40
83.33% Branches 25/30
90% Functions 9/10
94.59% Lines 35/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              1x 14x 14x     14x 14x 14x   14x     14x 7x 1x         1x 1x 1x   1x 1x     1x     1x             14x 6x     6x 6x 6x         14x 14x 14x     14x 6x 1x   5x   6x 6x       14x             14x 12x               14x                                              
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useEvent } from '@features/events/context/EventContext'
import { useLiveSocket } from '@features/live/hooks/useLiveSocket'
import { GaugeClassic } from '../components/gauges/GaugeClassic'
import { DonationFeed, type Donation } from '../components/DonationFeed'
 
export const LiveEmbedPage = () => {
    const { t } = useTranslation('common')
    const { event, isLoading } = useEvent()
 
    // State mirroring LivePage logic
    const [donations, setDonations] = useState<Donation[]>([])
    const [totalRaisedCents, setTotalRaisedCents] = useState(0)
    const [prevTotal, setPrevTotal] = useState(0)
 
    const { lastEvent } = useLiveSocket(event?.id || 'default')
 
    // Handle incoming socket events
    useEffect(() => {
        if (lastEvent) {
            const newDonation: Donation = {
                ...lastEvent,
                timestamp: Date.now(),
            }
 
            setPrevTotal((prev) => prev) // In a real scenario, this should track previous total for animation
            setTotalRaisedCents((prev) => {
                const newTotal = prev + lastEvent.amount
                // Trigger Goal Celebration
                const goalCents = Number(event?.goalAmount || 0) * 100
                Iif (prev < goalCents && newTotal >= goalCents) {
                    import('@core/lib/confetti').then((mod) => mod.fireGoalCelebration())
                }
                return newTotal
            })
 
            setDonations((prev) => [newDonation, ...prev].slice(0, 8))
        }
    }, [lastEvent, event?.goalAmount])
 
    // Initial load sync (You might want to fetch initial donations here if API supports it,
    // but for now we rely on socket or initial state if provided by API/EventContext)
    // NOTE: EventContext provides aggregated stats, we should use them for initial state.
    useEffect(() => {
        Eif (event && event.raised !== undefined) {
            // event.raised is likely in dollars (float) based on service division by 100.
            // We need cents for the gauge/logic.
            const raisedCents = Math.round(Number(event.raised) * 100)
            setTotalRaisedCents(raisedCents)
            setPrevTotal(raisedCents) // Start gauge at current total
        }
    }, [event])
 
    // Use query param ?layout=gauge|feed|full & ?bg=transparent|green
    const searchParams = new URLSearchParams(window.location.search)
    const layout = searchParams.get('layout') || 'full'
    const bgMode = searchParams.get('bg') || 'transparent'
 
    // Apply background
    useEffect(() => {
        if (bgMode === 'green') {
            document.body.style.backgroundColor = '#00ff00'
        } else {
            document.body.style.backgroundColor = 'transparent'
        }
        return () => {
            document.body.style.backgroundColor = ''
        }
    }, [bgMode])
 
    Iif (isLoading || !event)
        return (
            <div className="flex h-screen items-center justify-center text-white/50">
                {t('common.loading')}
            </div>
        )
 
    const renderGauge = () => (
        <GaugeClassic
            totalRaisedCents={totalRaisedCents}
            prevTotal={prevTotal}
            goalAmount={Number(event.goalAmount)}
            totalLabel={t('live.total_raised')}
        />
    )
 
    return (
        <div className="min-h-screen w-full bg-transparent overflow-hidden p-4">
            {layout === 'full' && (
                <div className="grid grid-cols-1 gap-4 h-full">
                    <div className="flex justify-center">{renderGauge()}</div>
                    <div className="h-[200px] relative overflow-hidden mask-fade-bottom">
                        <DonationFeed donations={donations.slice(0, 3)} />
                    </div>
                </div>
            )}
 
            {layout === 'gauge' && (
                <div className="flex justify-center h-full items-center">{renderGauge()}</div>
            )}
 
            {layout === 'feed' && (
                <div className="h-full relative overflow-hidden">
                    <DonationFeed donations={donations} />
                </div>
            )}
        </div>
    )
}