"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import { useHuntStatusCheck } from "@/hooks/useHuntStatusCheck"; import Link from "next/link"; import { use } from "react"; // Add this import for React.use() export default function CluePage({ params }) { const { data: session, status } = useSession(); // Unwrap the params Promise using React.use() const unwrappedParams = use(params); const { clueId } = unwrappedParams; const [clue, setClue] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const router = useRouter(); // Add this line to use the hunt status hook const huntActive = useHuntStatusCheck(); useEffect(() => { async function fetchClueDetails() { try { // Try to check hunt status first with robust error handling try { const statusResponse = await fetch("/api/hunt-status", { credentials: "include", headers: { Accept: "application/json" }, }); // Read as text first const statusText = await statusResponse.text(); try { const statusData = JSON.parse(statusText); if (!statusData.active && statusData.authenticated) { router.push("/"); return; } } catch (parseError) { console.error("Failed to parse hunt status response:", parseError); // Continue anyway to try fetching clue details } } catch (statusErr) { console.error("Error checking hunt status:", statusErr); // Continue anyway to try fetching clue details } // Now fetch clue details with robust error handling const response = await fetch(`/api/clues/${clueId}`, { credentials: "include", headers: { Accept: "application/json" }, }); // Read as text first const responseText = await response.text(); // Try to parse as JSON let data; try { data = JSON.parse(responseText); } catch (parseError) { console.error("Failed to parse clue details response:", parseError); console.log("Response received:", responseText.slice(0, 200)); throw new Error("Invalid response from server. Please try again."); } // Check for error in the response if (data.error) { throw new Error(data.error); } setClue(data.clue); } catch (err) { console.error("Error fetching clue details:", err); setError(err.message); } finally { setLoading(false); } } if (clueId) { fetchClueDetails(); } }, [clueId, router]); if (loading) { return (
Loading clue details...
); } if (error) { return (
Error: {error}
); } if (!clue) { return (
Clue not found
); } return (

{clue.title}

Description

{clue.description}

{clue.location && (

Location Hint

{clue.location}

)}
); }