"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import styles from "./waiting.module.css"; export default function WaitingPage() { const router = useRouter(); const { status } = useSession(); const [huntActive, setHuntActive] = useState(false); const [huntEnded, setHuntEnded] = useState(false); const [loading, setLoading] = useState(true); const [lastHuntData, setLastHuntData] = useState(null); // Check hunt status VERY aggressively useEffect(() => { let redirectAttempted = false; let lastKnownHuntStatus = false; async function checkHuntStatus() { if (redirectAttempted) return; try { // First check if we have a team ID from the session const userResponse = await fetch("/api/user", { method: "GET", headers: { "Cache-Control": "no-store, no-cache, must-revalidate", Pragma: "no-cache", }, credentials: "include", }); if (userResponse.ok) { const userData = await userResponse.json(); // If user has a team, check if that team has an active hunt session if (userData.teamId) { const teamHuntResponse = await fetch( `/api/teams/${userData.teamId}/hunt-session`, { method: "GET", headers: { "Cache-Control": "no-store, no-cache, must-revalidate", Pragma: "no-cache", }, credentials: "include", } ); // If team has an active hunt session, redirect to map if (teamHuntResponse.ok) { const teamHuntData = await teamHuntResponse.json(); if (teamHuntData.startTime) { // Store the start time for the map page localStorage.setItem( "teamHuntStartTime", teamHuntData.startTime ); console.log("Team hunt is active! Redirecting to map..."); redirectAttempted = true; lastKnownHuntStatus = true; // Show loading screen and redirect document.body.innerHTML = '
The treasure hunt has ended. You can view the results on the leaderboard.
Hunt Complete!
The treasure hunt has not started yet. Please wait for the administrator to start the hunt.
The hunt has started!