import { useEffect, useRef, useState } from "react";
function AuctionPage() {
const widgetRef = useRef<HTMLElement>(null);
const [outbidAlert, setOutbidAlert] = useState(false);
useEffect(() => {
const widget = widgetRef.current;
if (!widget) return;
const handleBid = (e: CustomEvent<{ amount: number }>) => {
console.log("Bid placed:", e.detail.amount);
analytics.track("auction_bid", { amount: e.detail.amount });
};
const handleOutbid = (e: CustomEvent<{ newBid: number }>) => {
setOutbidAlert(true);
// Play notification sound
new Audio("/sounds/outbid.mp3").play();
setTimeout(() => setOutbidAlert(false), 5000);
};
const handleWin = (e: CustomEvent) => {
console.log("Auction won!");
showConfetti();
};
const handleProceed = (e: CustomEvent<{ token: string }>) => {
window.location.href = `/checkout?token=${e.detail.token}`;
};
widget.addEventListener("fanfare-auction-bid", handleBid);
widget.addEventListener("fanfare-auction-outbid", handleOutbid);
widget.addEventListener("fanfare-auction-win", handleWin);
widget.addEventListener("fanfare-auction-proceed", handleProceed);
return () => {
widget.removeEventListener("fanfare-auction-bid", handleBid);
widget.removeEventListener("fanfare-auction-outbid", handleOutbid);
widget.removeEventListener("fanfare-auction-win", handleWin);
widget.removeEventListener("fanfare-auction-proceed", handleProceed);
};
}, []);
return (
<div className="auction-container">
{outbidAlert && <div className="outbid-alert">You have been outbid! Place a higher bid to stay in the lead.</div>}
<h1>Exclusive Item Auction</h1>
<fanfare-auction-widget ref={widgetRef} auction-id="auction_123" />
</div>
);
}