"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import { formatElapsedTime, getHuntTimer } from "@/utils/timerService"; export default function CluePage({ params }) { const [clue, setClue] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [foundNow, setFoundNow] = useState(false); const [elapsedTime, setElapsedTime] = useState("00:00:00"); const router = useRouter(); const { status } = useSession(); const { clueId } = params; useEffect(() => { if (status === "unauthenticated") { router.push("/login"); return; } if (status === "loading") { return; } // Fetch clue data async function fetchClue() { try { setLoading(true); const response = await fetch(`/api/clues/${clueId}`, { method: "GET", headers: { "Content-Type": "application/json", "Cache-Control": "no-cache, no-store, must-revalidate", }, credentials: "include", }); if (!response.ok) { if (response.status === 404) { throw new Error("Clue not found"); } else { const data = await response.json(); throw new Error(data.error || "Failed to fetch clue"); } } const data = await response.json(); setClue(data); setFoundNow(data.foundNow); setLoading(false); } catch (err) { console.error("Error fetching clue:", err); setError(err.message); setLoading(false); } } fetchClue(); }, [clueId, router, status]); // Update timer display useEffect(() => { // Set up timer display updates const timerData = getHuntTimer(); if (!timerData || !timerData.isActive) { return; } const timer = setInterval(() => { setElapsedTime(formatElapsedTime(timerData.startTime)); }, 1000); // Initial set setElapsedTime(formatElapsedTime(timerData.startTime)); return () => clearInterval(timer); }, []); if (loading) { return (
Loading clue...
{clue.description}
{clue.location && ({clue.location}