import { useEffect, useRef } from "react";
function ExperiencePage({ experienceId }: { experienceId: string }) {
const widgetRef = useRef<HTMLElement>(null);
useEffect(() => {
const widget = widgetRef.current;
if (!widget) return;
const handleJourneyChange = (e: CustomEvent) => {
const { snapshot } = e.detail;
console.log("Journey stage:", snapshot.journeyStage);
console.log("Sequence stage:", snapshot.sequenceStage);
// Track analytics
analytics.track("journey_stage_change", {
journeyStage: snapshot.journeyStage,
sequenceStage: snapshot.sequenceStage,
});
};
const handleAdmitted = (e: CustomEvent<{ token: string }>) => {
console.log("Admitted with token:", e.detail.token);
// Widget will auto-redirect if checkout-url is set
// Or handle manually:
window.location.href = `/checkout?token=${e.detail.token}`;
};
const handleError = (e: CustomEvent<{ error: string }>) => {
console.error("Journey error:", e.detail.error);
showErrorToast(e.detail.error);
};
widget.addEventListener("fanfare-journey-change", handleJourneyChange);
widget.addEventListener("fanfare-admitted", handleAdmitted);
widget.addEventListener("fanfare-error", handleError);
return () => {
widget.removeEventListener("fanfare-journey-change", handleJourneyChange);
widget.removeEventListener("fanfare-admitted", handleAdmitted);
widget.removeEventListener("fanfare-error", handleError);
};
}, []);
return (
<div className="experience-container">
<fanfare-experience-widget
ref={widgetRef}
experience-id={experienceId}
auto-start="true"
checkout-url="/checkout"
/>
</div>
);
}