"use client"; import { useState, useEffect, useRef } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import Image from "next/image"; import styles from "./map.module.css"; import { useHuntStatusCheck } from "@/hooks/useHuntStatusCheck"; export default function MapPage() { const { data: session, status } = useSession(); const [loading, setLoading] = useState(true); const [mapError, setMapError] = useState(false); const [error, setError] = useState(null); const [teamData, setTeamData] = useState(null); const [elapsedTime, setElapsedTime] = useState("00:00:00"); const mapContainerRef = useRef(null); const router = useRouter(); // Use the custom hook to check if hunt is still active const huntActive = useHuntStatusCheck(); // Debug log for hunt status useEffect(() => { console.log("Hunt active status in map page:", huntActive); }, [huntActive]); useEffect(() => { // Only redirect if explicitly not authenticated if (status === "unauthenticated") { router.push("/login"); return; } // If hunt is not active, redirect to waiting page if (huntActive === false) { console.log("Hunt is not active, redirecting to waiting page"); router.push("/waiting"); return; } // Only fetch team data if authenticated and hunt is active or status unknown if (status === "authenticated" && huntActive !== false) { fetchTeamData(); } }, [status, huntActive, router]); // Update elapsed time every second useEffect(() => { if (!teamData?.team) return; const startTime = new Date(teamData?.huntStartTime || Date.now()).getTime(); const timer = setInterval(() => { const now = Date.now(); const elapsed = Math.floor((now - startTime) / 1000); const hours = Math.floor(elapsed / 3600) .toString() .padStart(2, "0"); const minutes = Math.floor((elapsed % 3600) / 60) .toString() .padStart(2, "0"); const seconds = Math.floor(elapsed % 60) .toString() .padStart(2, "0"); setElapsedTime(`${hours}:${minutes}:${seconds}`); }, 1000); return () => clearInterval(timer); }, [teamData]); const fetchTeamData = async () => { try { setLoading(true); setError(null); const response = await fetch("/api/user/team", { method: "GET", headers: { "Content-Type": "application/json", "Cache-Control": "no-cache, no-store, must-revalidate", }, credentials: "include", }); if (!response.ok) { // Handle specific error cases without immediate redirect const errorData = await response.json().catch(() => ({})); if (response.status === 401) { // Set error but don't redirect immediately - let the session handling take care of it setError( "Authentication error: " + (errorData.error || "Not logged in") ); setLoading(false); return; } else if ( response.status === 404 || errorData.error?.includes("not part of a team") ) { setError("You need to join a team before viewing the map"); setLoading(false); // Give user time to read the error message before redirect setTimeout(() => router.push("/team-selection"), 2000); return; } else { throw new Error(errorData.error || "Failed to fetch team data"); } } const data = await response.json(); setTeamData(data); setLoading(false); } catch (err) { console.error("Error fetching team data:", err); setError(err.message); setLoading(false); } }; if (status === "loading" || loading) { return (
Loading map...
); } // If hunt status is known to be inactive, show message if (huntActive === false) { return (

Hunt Not Active

The treasure hunt has not started yet.

); } if (error) { return (

Error

{error}

); } if (mapError) { return (

Error Loading Map

We couldn't load the treasure map. Please try again.

); } return (
{/* Map Banner with team info and timer */}
{teamData?.team?.name || "Your Team"}
{elapsedTime}
{teamData?.team?.code || ""}
{/* Main map content area */}
{/* Use Image component with fill layout for better SVG handling */} Treasure Map { // Fallback to a static image if SVG fails to load setMapError(true); }} />
{/* Navigation bar - now sticky */}
); }