mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
59 lines
1.9 KiB
JavaScript
59 lines
1.9 KiB
JavaScript
"use client";
|
|
import { useState, useEffect } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
export function useHuntStatusCheck(checkIntervalMs = 5000) {
|
|
const [huntActive, setHuntActive] = useState(null); // Start with null to indicate unknown status
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
// Initial check
|
|
checkHuntStatus();
|
|
|
|
// Set up interval for periodic checks
|
|
const intervalId = setInterval(checkHuntStatus, checkIntervalMs);
|
|
|
|
// Clean up on unmount
|
|
return () => clearInterval(intervalId);
|
|
|
|
// Function to check hunt status
|
|
async function checkHuntStatus() {
|
|
try {
|
|
const response = await fetch("/api/hunt-status", {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
},
|
|
credentials: "include",
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
console.log("Hunt status check:", data); // Debug log
|
|
|
|
// If hunt was active but is now inactive, redirect to leaderboard
|
|
if (huntActive === true && data.active === false) {
|
|
console.log("Hunt has ended! Redirecting to leaderboard...");
|
|
window.location.href = "/leaderboard?huntEnded=true";
|
|
return;
|
|
}
|
|
|
|
// If hunt was not active but is now active, let user know they can go to map page
|
|
if (huntActive === false && data.active === true) {
|
|
console.log("Hunt has started! Access to map is now available");
|
|
// Can show a notification here if desired
|
|
}
|
|
|
|
// Update hunt status state
|
|
setHuntActive(data.active);
|
|
}
|
|
} catch (err) {
|
|
console.error("Failed to check hunt status:", err);
|
|
}
|
|
}
|
|
}, [huntActive, checkIntervalMs, router]);
|
|
|
|
return huntActive;
|
|
}
|