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);
const [likes, setLikes] = React.useState(1240); // hitungan like (social proof + interaktif)
const [burst, setBurst] = React.useState(0); // trigger burst hati pas di-tap
const bumpLike = () => { setLikes(l => l + 1); setBurst(b => b + 1); };
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("No active live stream.","Tidak ada siaran langsung saat ini.")}
);
}
const vcode = ((live && live.voucherCode) || "").trim().toUpperCase();
const pinned = live.pinnedProductId ? list.find(p => String(p.id) === String(live.pinnedProductId)) : null;
const scrollToCatalog = () => { try { document.getElementById("lpb-live-catalog").scrollIntoView({ behavior: "smooth", block: "start" }); } catch (e) {} };
const shareLive = () => {
const url = (typeof window !== "undefined" ? window.location.href : "https://lopobya.shop");
if (navigator.share) { navigator.share({ title: "LOPOBYA LIVE ๐ด", text: t("Watch LOPOBYA live & shop!","Lagi LIVE nih, tonton & belanja bareng!"), url }).catch(()=>{}); }
else { try { navigator.clipboard.writeText(url); } catch (e) {} }
};
const railBtn = { background:"none", border:"none", cursor:"pointer", display:"flex", flexDirection:"column", alignItems:"center", gap:3, padding:0 };
const railLbl = { color:"#fff", fontSize:10, fontWeight:700, textShadow:"0 1px 3px rgba(0,0,0,.6)" };
return (
{/* ===== IMMERSIVE LIVE (TikTok-style) โ video full + overlay ===== */}
{/* VIDEO (isi penuh frame) */}
{(!live.platform || live.platform === "youtube") ? (
) : live.platform === "twitch" ? (
) : (
)}
{/* Gradient biar teks overlay kebaca (gak nangkep klik) */}
{/* LIVE + host (kiri atas) */}
LIVE
๐๏ธ
LOPOBYA
{/* Viewers (kanan atas) */}
๐๏ธ {Number(viewers).toLocaleString("id-ID")}
{/* Action rail kanan (like / belanja / bagikan) */}
{/* Hati melayang (re-trigger pas di-like) */}
{["โค๏ธ","โจ","๐ฅ","๐","๐"].map((h, i) => (
{h}
))}
{/* Overlay bawah: voucher + kartu produk lagi dibahas */}
{vcode && (
)}
{pinned && (

{ setSelectedProduct(pinned); setPage("product"); }} style={{ width: 52, height: 64, objectFit: "cover", borderRadius: 10, cursor: "pointer" }} />
๐ {t("LIVE","DIBAHAS")}
{pinned.name}
Rp {Number(pinned.price).toLocaleString("id-ID")}
{pinned.original_price > pinned.price && Rp {Number(pinned.original_price).toLocaleString("id-ID")}}
{pinned.stock > 0 && pinned.stock <= 15 &&
๐ฅ {t("Only "+pinned.stock+" left!","Sisa "+pinned.stock+"!")}
}
)}
{t("Checkout here for the special live price ๐ฅ","Checkout di sini buat harga spesial Live ๐ฅ")}
{/* Catalog below live */}
{t("Live Collection","Koleksi Spesial Live")}
{productsReady
? (() => {
// Kalau ada keranjang live โ tampilkan produk yang udah dibahas (terkurasi).
// Kalau kosong โ tampilkan semua produk ready.
const basketIds = Array.isArray(live.basketProductIds) ? live.basketProductIds : [];
const basketProducts = basketIds.map(id => list.find(p => String(p.id) === String(id))).filter(Boolean);
const collection = basketProducts.length ? basketProducts : activeProducts;
return collection.map(p =>
{setSelectedProduct(x);setPage("product");}} onAddToCart={onAddToCart} />);
})()
: [1,2,3,4].map(i => )}
);
};
Object.assign(window, { LivePage });