"use client"; import { useState, useEffect } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Link from "next/link"; export default function HuntNotStarted() { const [countdown, setCountdown] = useState(10); const router = useRouter(); const searchParams = useSearchParams(); const teamId = searchParams.get("teamId"); // Add aggressive hunt status checking useEffect(() => { let redirectAttempted = false; // Function to check hunt status async function checkHuntStatus() { if (redirectAttempted) return; try { const timestamp = new Date().getTime(); const response = await fetch(`/api/hunt-status?t=${timestamp}`, { method: "GET", headers: { "Content-Type": "application/json", "Cache-Control": "no-store, no-cache, must-revalidate", Pragma: "no-cache", }, credentials: "include", }); if (response.ok) { const data = await response.json(); // If hunt becomes active while on this page, immediately redirect to map if (data.active) { console.log("Hunt now active! Redirecting to map page"); redirectAttempted = true; window.location.replace("/map"); } } } catch (err) { console.error("Error checking hunt status:", err); } } // Check immediately and every 500ms checkHuntStatus(); const intervalId = setInterval(checkHuntStatus, 500); return () => clearInterval(intervalId); }, []); // Existing countdown effect useEffect(() => { // Countdown timer if (countdown > 0) { const timer = setTimeout(() => { setCountdown(countdown - 1); }, 1000); return () => clearTimeout(timer); } else { // Redirect back to team page when countdown reaches 0 if (teamId) { router.push(`/${teamId}`); } else { router.push("/team-selection"); } } }, [countdown, router, teamId]); return (
The treasure hunt has not been activated by an administrator yet. Please check back later or contact an admin to start the hunt.
Redirecting back in {countdown} seconds...