"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; export default function CluePage({ params }) { const { clueId } = params; const [clue, setClue] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const router = useRouter(); useEffect(() => { async function checkHuntStatus() { try { const response = await fetch("/api/hunt-status", { credentials: "include", }); if (response.ok) { const data = await response.json(); // If hunt is not active, redirect to the home page if (!data.active) { router.push("/"); return false; } return true; } else { console.error("Failed to check hunt status"); return false; } } catch (err) { console.error("Error checking hunt status:", err); return false; } } async function fetchClueDetails() { try { // First verify the hunt is active const huntIsActive = await checkHuntStatus(); if (!huntIsActive) return; // Fetch the specific clue from the API with improved error handling const response = await fetch(`/api/clues/${clueId}`, { credentials: "include", }); // Better error handling for non-JSON responses if (!response.ok) { const contentType = response.headers.get("content-type"); if (contentType && contentType.includes("application/json")) { const errorData = await response.json(); throw new Error(errorData.error || "Failed to fetch clue details"); } else { throw new Error( `Failed to fetch clue details: ${response.status} ${response.statusText}` ); } } const data = await response.json(); // If we got back an error field, show it as an error 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 (
{clue.description}
{clue.location && ({clue.location}