const LivePage = ({ setPage, setSelectedProduct, onAddToCart, lang, liveStreamSettings, products, productsReady }) => { const t = (en, id) => lang === "id" ? id : en; const list = (Array.isArray(products) && products.length) ? products : (window.PRODUCTS || []); const activeProducts = list.filter(p => p.stock > 0); const [viewers, setViewers] = React.useState(80); const [live, setLive] = React.useState(liveStreamSettings); React.useEffect(() => { window.scrollTo(0,0); // Penonton REAL via heartbeat presence + baseline social proof. // Set VIEWER_BASE = 0 kalau mau angka MURNI real tanpa tambahan. const VIEWER_BASE = 80; let vid = sessionStorage.getItem("lpb_live_vid"); if (!vid) { vid = Math.random().toString(36).slice(2) + Date.now().toString(36); sessionStorage.setItem("lpb_live_vid", vid); } const beat = async () => { try { const r = await fetch("api/live_presence.php", { method:"POST", headers:{"Content-Type":"application/json"}, body: JSON.stringify({ id: vid }) }); const d = await r.json(); if (d && typeof d.count === "number") setViewers(VIEWER_BASE + d.count); } catch (e) {} }; beat(); const interval = setInterval(beat, 10000); return () => clearInterval(interval); }, []); // Sinkron kalau prop berubah + AUTO-REFRESH setting live tiap 15 detik → // spotlight/pin ganti REAL-TIME di layar penonton tanpa mereka refresh. React.useEffect(() => { setLive(liveStreamSettings); }, [liveStreamSettings]); React.useEffect(() => { const iv = setInterval(async () => { try { const r = await fetch("api/settings.php"); const d = await r.json(); if (d && d.liveStream) setLive(d.liveStream); } catch (e) {} }, 15000); return () => clearInterval(iv); }, []); // Simpan kode voucher live → CheckoutPage auto-apply pas penonton checkout. React.useEffect(() => { const vc = ((live && live.voucherCode) || "").trim().toUpperCase(); if (vc) { try { localStorage.setItem("lopobya_live_voucher", vc); } catch (e) {} } }, [live]); // Facebook: pakai SDK resmi (fb-video) — versi embed paling didukung FB, // peluang jalan di HP lebih besar drpd iframe polos. Load SDK sekali + parse ulang. React.useEffect(() => { if (!live || live.platform !== "facebook" || !live.facebookUrl) return; const parse = () => { try { if (window.FB && window.FB.XFBML) window.FB.XFBML.parse(); } catch (e) {} }; if (window.FB) { parse(); return; } if (!document.getElementById("facebook-jssdk")) { const js = document.createElement("script"); js.id = "facebook-jssdk"; js.src = "https://connect.facebook.net/id_ID/sdk.js#xfbml=1&version=v19.0"; js.async = true; js.defer = true; js.crossOrigin = "anonymous"; js.onload = parse; document.body.appendChild(js); } else { const iv = setInterval(() => { if (window.FB) { parse(); clearInterval(iv); } }, 300); setTimeout(() => clearInterval(iv), 6000); } }, [live && live.platform, live && live.facebookUrl]); if (!live || !live.active) { return (
{t("Checkout directly here to get our special live price.","Checkout langsung di website untuk mendapatkan harga spesial khusus penonton Live.")}